Detailed descriptions of process branches fork and exec in Python

Source: Internet
Author: User
This article mainly introduces the fork and exec processes in Python. This article uses examples to explain how to use fork () and describes eight exec-related methods, for more information about job concurrency in python, you can use process branches. in linux, the fork () method is used to implement process branches.

1. after fork () is called, a new sub-process is created, which is a copy of the original parent process. The sub-process can run independently outside the parent process.
2. fork () is a special method. it is called once and returned twice.
3. fork (): it returns two values. one value is 0, indicating that the subprocess returns the data. the other value is not 0, indicating that the subprocess ID is returned in the parent process.

The following statements can only be run in linux, but cannot be run in Windows.

Process branch fork ()

Example:

The code is as follows:


#! /Usr/bin/python
# Coding = UTF-8
Import OS

Def child ():
Print ('Hello from child ', OS. getpid ())
OS. _ exit (0)
Def parent ():
Pid = OS. fork ()
If pid = 0:
Child ()
Print 'fork child process error! '# If this string is printed, an error occurred while calling child ().
Else:
Print ('Hello from parent', OS. getpid (), pid)

Parent ()

The running result is as follows:

The code is as follows:


('Hello from parent', 29888,298 89)
('Hello from child ', 29889)


It is not difficult to see from the results that the print characters after child () are not printed, indicating that no response is returned when child () is called.

Combination of fork and exec

The Objective c method does not return.

First, explain the eight method groups related to exec:

OS .exe cv (program, cmdargs)

The basic "v" execution form requires passing in the executable program name and the list or tuples of the command line parameter characters used to run the program.

OS .exe cl (program, cmdarg1, cmdarg2 ,..., CmdargN)

The basic "l" execution form requires the name of the executable program and multiple character parameters of the command line used to run the program.

OS .exe cvp (program, args)

In "p" mode, the basic "v" execution form requires passing in the executable program name and the list or tuples of the command line parameter characters used to run the program. the search path for running the new program is the search path for the current file.

OS .exe clp (program, cmdarg1, cmdarg2 ,..., CmdargN)

In "p" mode, the basic "l" execution form requires passing in the executable program name and multiple character parameters of the command line used to run the program. the search path for running the new program is the search path for the current file.

OS .exe cve (program, args, env)

In "e" mode, the basic "v" execution form requires passing in the executable program name and the list or tuples of the command line parameter characters used to run the program. finally, you need to input the required environment variable env dictionary parameter to run the new program.

OS .exe cle (program, cmdarg1, cmdarg2 ,..., CmdargN, env)

In "e" mode, the basic "l" execution form requires passing in the executable program name and multiple character parameters of the command line used to run the program. finally, you need to input the required environment variable env dictionary parameter to run the new program.

OS .exe cvpe (program, args, env)

In the combination of "p" and "e", the basic "v" execution form requires passing in executable program names, and the list or tuples of the command line parameter characters used to run the program. finally, you need to input the required environment variable env dictionary parameter to run the new program. the search path for running the new program is the search path for the current file.

OS .exe clpe (program, cmdarg1, cmdarg2 ,..., CmdargN, env)

In the combination of "p" and "e", the basic "l" execution form requires passing in executable program names, and the command line parameters used to run the program. finally, you need to input the required environment variable env dictionary parameter to run the new program. the search path for running the new program is the search path for the current file.

The newprocess. py code is as follows:

The code is as follows:


#! /Usr/bin/python
# Coding = UTF-8
Import OS

Def child ():
Print ('Hello from child ', OS. getpid ())
OS. _ exit (0)

Child ()

The main code is as follows:

The code is as follows:


#! /Usr/bin/python
# Coding = UTF-8
Import OS

Def child ():
Print ('Hello from child ', OS. getpid ())
OS. _ exit (0)

Def parent ():
Pid = OS. fork ()
If pid = 0:
OS .exe clp ('Python', 'Python', 'newprocess. py ')
Assert False, 'fork child process error! '
Else:
Print ('Hello from parent', OS. getpid (), pid)
Parent ()


The output is as follows:

The code is as follows:


$ Python TestFork. py
('Hello from parent', 30791,307 92)
$ ('Hello from child ', 30792)

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.