Subprocess analysis generated by the fork () function in python

Source: Internet
Author: User

In the python OS module, the fork () function is used to generate sub-processes. The generated sub-processes are the mirror of the parent process, but they have their own address space, the sub-process replicates the parent process memory to itself. The execution between the two processes is independent of each other, and the execution sequence can be uncertain, random, and unpredictable, this is similar to the execution sequence of multiple threads. Import OS def child (): print 'a new child: ', OS. getpid () print 'parent id is: ', OS. getppid () OS. _ exit (0) def parent (): while True: newpid = OS. fork () print newpid if newpid = 0: child () else: pids = (OS. getpid (), newpid) print "parent: % d, child: % d" % pids print "parent:", OS. getppid () if raw_input () = 'q': break parent () after we load the OS module, the fork () function in our parent function generates a sub-process, the return value newpid has two values. One value is 0, which indicates the sub-process. The other value is an integer greater than 0, which is used for the table. Shows the parent process. This constant is the pid of the child process. We can clearly see two return values through the print statement. If fork () returns a negative value, it indicates that the sub-process is not successfully generated (this is not considered in this simple program ). If newpid = 0, it indicates that we enter the child process, that is, the child () function. In the child process, we output our own id and the parent process id. If the else statement is entered, it indicates that newpid> 0, we enter the parent process, in the parent process OS. getpid () gets its own id. fork () returns newpid, which indicates the id of the child process. At the same time, we output the id of the parent process. through the experiment, we can see that the execution sequence of if and else statements is uncertain. The execution sequence of sub-and parent processes is determined by the scheduling algorithm of the operating system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.