Today, we can see that there are fork sub-processes in others' source code to operate data. However, as fork does not exit in time, the number of Python processes in the system increases and the number of sub-processes increases. I wrote the test code for the fork process under Python (to illustrate this problem is not necessarily appropriate ):
Def fork (a): def now (): import datetime return datetime. datetime. now (). strftime ("% S. % f ") import OS import time print now (), a if OS. fork () = 0: print 'sub-process [% s]: % s' % (now (), OS. getpid () while 1: a-= 10 print 'sub-process a value [% s]: % s' % (now (), a) if a <1: break print 'Prepare to exit the subprocess' # OS. _ exit (0) # Here you can exit the sub-process else: print 'parent process [% s]: % s' % (now (), OS. getpid () while 1: a-= 1 print 'a value of the parent process [% s]: % s' % (now (), a) if a < 0: break time. sleep (1) print 'Wait until the child process ends... 'Try: result = OS. wait () if result: print 'sub-process: ', result [0], result [1] else: print' no data! 'Failed T: print' exception... 'print 'parent process... the final value of 'print ':', a # exit (0) # You can also exit here. Note that this is where both the parent process and child process are shared, exit here will cause the parent process to exit together.
TIPS:
OS. fork () has two returned values: the returned values of the parent process and the child process.
In the parent process, fork returns the PID of the child process;
In the sub-process, the returned value is 0.
The sub-process copies the context of the parent process.
The execution sequence of the Parent and Child processes cannot be determined.
After OS. fork (), the sub-process must use exit () or OS. _ exit () to exit the sub-process environment. We recommend that you use OS. _ exit ()
The code for operating system. fork () to create sub-processes is not very suitable. Linux is no problem and cannot be used in Windows. The official documentation also has a similar statement:
Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have known issues when using fork() from a threadAvailability: Unix.