Details of process branches fork and exec in Python

  • 2020-05-09 18:47:07
  • OfStack

In python, task concurrency 1 is achieved by process branching. In linux system, process branching is achieved by fork() method.

1.fork() creates a new child process that is a copy of the original parent process. The child process can be run independently of the parent process.
2.fork() is a special method, called once and returned twice.
3.fork() it will return 2 values,1 value is 0, indicating that it is returned in the child process; The other value is non-0, indicating that the child process ID is returned in the parent process.

The following can only be run in linux, not window.

Process branch fork()

Examples are 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 you print the string , That call child() error
    else:
        print('hello from parent', os.getpid(), pid) parent()

The results are as follows:


('hello from parent', 29888, 29889)
('hello from child', 29889)

As you can see from the results, the print character after child() is not printed, indicating that the call to child() did not return.

A combination of fork and exec

In the above example, the method child() is called and then exits. However, in practice, we would like the child process to run another new program independently. In this case, we need to use the exec method to replace the child process, and the pid of the replaced process will not change. The exec method will not return.

First of all, explain 1 the eight method groups related to exec:

os.execv(program, cmdargs)

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

os. execl (program, cmdarg1, cmdarg2,... , cmdargN)

The basic "l" form of execution requires an executable program name to be passed in, along with multiple character arguments on the command line to run the program.

os.execvp(program, args)

In "p" mode, the basic "v" execution requires an executable program name to be passed in, along with a list or tuple of command-line argument characters used to run the program. The search path to run the new program is the search path of the current file.

os. execlp (program, cmdarg1, cmdarg2,... , cmdargN)

"p" mode, the basic "l" form of execution, requires the passing of the executable program name, as well as the command line character parameters used to run the program. The search path to run the new program is the search path of the current file.

os.execve(program, args, env)

In "e" mode, the basic "v" form of execution needs to pass in the executable program name, as well as a list or tuple of command-line argument characters used to run the program, and finally the required environment variable env dictionary parameter to run the new program.

os. execle (program, cmdarg1, cmdarg2,... , cmdargN env)

In "e" mode, the basic "l" form of execution needs to pass in the executable program name, as well as the command line character parameters to run the program, and finally, the environment variable env dictionary parameters needed to run the new program.

os.execvpe(program, args, env)

p "in" and "e combination mode, the" basic "v" form, the need to executable program name, and used to run the program command line parameter list or a tuple of the characters. Finally, to run a new program need environment variable parameter env dictionary. Run a new program of the search path for the current file search path.

os. execlpe (program, cmdarg1, cmdarg2,... , cmdargN env)

In the combination mode of "p" and "e", the basic "l" execution form needs to pass in the executable program name, as well as the command line character parameters to run the program. Finally, it needs to pass in the required environment variable env dictionary parameters to run the new program.

newprocess. py 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:


#!/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.execlp('python', 'python', 'newprocess.py')
        assert False, 'fork child process error!'
    else:
        print('hello from parent', os.getpid(), pid)
parent()

The output is as follows:

$ python TestFork.py
('hello from parent', 30791, 30792)
$ ('hello from child', 30792)


Related articles: