Process Creation & zombies & orphaned

First, create and open a child process in two ways

1.1 Method 1

#单个进程
from multiprocessing import Process
import time

def Text(): print('我是一个子进程') print('我的进程开始了') time.sleep(2) print('我的进程结束了') if __name__ == '__main__': #windows下必须要写这一句,不然会报错 p = Process(target = Text) #实例化产生一个对象 p.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 time.sleep(5) print('我是主进程,我要结束了')
#多个进程
from multiprocessing import Process
import time

def Text(x): print(f'我是子进程{x}') print(f'{x}的进程开始了') time.sleep(2) print(f'{x}的进程结束了') if __name__ == '__main__': #windows下必须要写这一句,不然会报错 p1 = Process(target = Text,args=('yjy',)) #实例化产生一个对象 子进程1 p2 = Process(target = Text,args=('ypp',)) #实例化产生一个对象 子进程2 p1.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 p2.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 time.sleep(5) print('我是主进程,我要结束了')

1.2 Second way

from  multiprocessing import Process
import time

class Text(Process): #定义一个类,让他继承Process def __init__(self,name): #重写__init__ super().__init__() #继承父类的__init__方法 self.name = name def run(self): #定义一个函数 print(f'子进程的姓名为{self.name},子进程开始') time.sleep(2) print(f'{self.name}进程结束') if __name__ == '__main__': p = Text('yjy') p.start() #通知操作系统子进程开始 print('主进程结束')

Second, the memory space verification process isolation


from multiprocessing import Process
import time

x = 0  #定义一个变量x def Check(): global x x = 100 print(f'子进程的x:{x}') if __name__ == '__main__': p = Process(target=Check) p.start() print(f'父进程的x:{x}') ----------------------------------------------------------------------------------- 父进程的x:0 子进程的x:100

x x and child processes the parent process is not a value, indicating that the two processes them independently of each other!

Third, the zombie process and orphaned


Zombie process (harmful): When the end of the child process does not release everything out, he will leave his pid (similar identification number) for the parent to see that this so-called process dead did not die clean, this is the zombie process, he will wait for the parent process and finally put him to dispose of

Orphaned (harmless): a parent process to exit, but it's one or more child process is still running, so that the child will become orphaned. Orphan process will be init process (process 1) the adoption by the init process completion status of collection to them.

1.1 Method 1

#单个进程
from multiprocessing import Process
import time

def Text(): print('我是一个子进程') print('我的进程开始了') time.sleep(2) print('我的进程结束了') if __name__ == '__main__': #windows下必须要写这一句,不然会报错 p = Process(target = Text) #实例化产生一个对象 p.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 time.sleep(5) print('我是主进程,我要结束了')
#多个进程
from multiprocessing import Process
import time

def Text(x): print(f'我是子进程{x}') print(f'{x}的进程开始了') time.sleep(2) print(f'{x}的进程结束了') if __name__ == '__main__': #windows下必须要写这一句,不然会报错 p1 = Process(target = Text,args=('yjy',)) #实例化产生一个对象 子进程1 p2 = Process(target = Text,args=('ypp',)) #实例化产生一个对象 子进程2 p1.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 p2.start() # 告诉操作系统我要开子进程,告诉完了这行代码就算执行完了,接着往下走,具体操作系统什么时候开子,开多长时间跟你没关系 time.sleep(5) print('我是主进程,我要结束了')

1.2 Second way

from  multiprocessing import Process
import time

class Text(Process): #定义一个类,让他继承Process def __init__(self,name): #重写__init__ super().__init__() #继承父类的__init__方法 self.name = name def run(self): #定义一个函数 print(f'子进程的姓名为{self.name},子进程开始') time.sleep(2) print(f'{self.name}进程结束') if __name__ == '__main__': p = Text('yjy') p.start() #通知操作系统子进程开始 print('主进程结束')

Second, the memory space verification process isolation


from multiprocessing import Process
import time

x = 0  #定义一个变量x def Check(): global x x = 100 print(f'子进程的x:{x}') if __name__ == '__main__': p = Process(target=Check) p.start() print(f'父进程的x:{x}') ----------------------------------------------------------------------------------- 父进程的x:0 子进程的x:100

x x and child processes the parent process is not a value, indicating that the two processes them independently of each other!

Third, the zombie process and orphaned


Zombie process (harmful): When the end of the child process does not release everything out, he will leave his pid (similar identification number) for the parent to see that this so-called process dead did not die clean, this is the zombie process, he will wait for the parent process and finally put him to dispose of

Orphaned (harmless): a parent process to exit, but it's one or more child process is still running, so that the child will become orphaned. Orphan process will be init process (process 1) the adoption by the init process completion status of collection to them.

Guess you like

Origin www.cnblogs.com/lulingjie/p/11529097.html