What does Exit 0 do in Python

Python course

Fork and processes


Computer science was concerned with cloning long before biology. But it was not called cloning but forking.
fork means fork, fork, branch and as a verb fork, split and branch. But it also means splitting or as a verb splitting. In the latter sense, it is used in operating systems, especially Unix and Linux. Processes are split up using the "fork" command - one would say cloned today - and then lead an independent "life".

In computer science, the term fork is used to designate various situations:

  • A system call provided by the operating system that splits the existing process - and creates an exact copy of the process - and then lets both processes run in parallel, so to speak.
  • In software development, a fork describes a split from a (main) project.
  • The ability of some file systems to divide files

Fork in Python

When the fork () system call is made, the current process creates a copy of itself, which then runs as a child process of the creating program. The child process takes over the data and the code from the parent process and receives its own process number, the PID ("Process IDentifier"), from the operating system. The child process runs as an independent instance of the program, independent of the parent process. The return value of fork () shows which process you are in. 0 indicates the child process and a positive return value stands for the parent process. In the event of an error, fork () returns a value less than 0 and no child process is created.

To be able to fork processes, we have to import the os module into Python.

The following example script shows a parent process that can fork itself as often as required, as long as the user of the script does not enter a q at the prompt. Both the child process and the parent process continue with the if statement after the fork. In the parent process, newpid has a value other than 0, while newpid has the value 0 in the child process, so that the child () function is called in the child process. The exit statement in the child function is necessary, otherwise the child process would return to the parent process, namely to raw_input ().
import os def child (): print ('\ nA new child', os.getpid ()) os._exit (0) def parent (): while True: newpid = os.fork () if newpid == 0: child () else: pids = (os.getpid (), newpid) print ("parent:% d, child:% d \ n"% pids) reply = input ("q for quit / c for new fork") if reply == 'c': continue else: break parent ()

Start independent programs with fork ()

So far, the child processes in the examples have called a function within the script themselves and then terminated.

Forks are often used to start independently running programs. The exec * () functions are available in the os module for this purpose.

They run a new program by replacing the current process with it. You do not return to the calling program. Under Unix / Linux you even get the same process ID as the calling program.

The exec * () functions

The exec * () functions are available in different variations:
  • os.execl (path, arg0, arg1, ...)
  • os.execle (path, arg0, arg1, ..., env)
  • os.execlp (file, arg0, arg1, ...)
  • os.execlpe (file, arg0, arg1, ..., env)
  • os.execv (path, args)
  • os.execve (path, args, env)
  • os.execvp (file, args)
  • os.execvpe (file, args, env)
We will explain the functions in the following with many examples, as they have only been described very sparsely in books and on the Internet.

For our examples we want to use the following bash shell script that we save under test.sh. We saved it in the directory / home / bernd / bin2. To understand the following examples, it is only important that test.sh is not in a directory that is in $ PATH. Test.sh should also be executable. So execute the following shell command:

#! / bin / bash script_name = $ 0 arg1 = $ 1 current = `pwd` echo $ script_name, $ arg1 echo" XYZ: "$ XYZ echo" PATH: "$ PATH echo" current directory: $ current "In another directory, e.g. / home / bernd / python, we have a python script execvp.py that calls this bash script: #! / usr / bin / python import os args = ("test", "abc") os.execvp ( "test.sh", args) Since test.sh is not in $ PATH, there is an error message when execvp is called in the command line: $ ./execvp.py Traceback (most recent call last): File "./execvp .py ", line 6, in os.execvp (" test.sh ", args) File" /usr/lib/python2.6/os.py ", line 344, in execvp _execvpe (file, args) File "/usr/lib/python2.6/os.py", line 380, in _execvpe func (fullname, * argrest) OSError: [Errno 2] No such file or directory So that this error message does not appear and our shell script is found we extend the PATH environment variable to include the directory in which test.sh is located, in our case / home / bernd / bin2: $ PATH = $ PATH: / home / bernd / bin2 $ ./execvp.py /home/bernd/bin2/test.sh, abc XYZ: PATH: / usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin: / usr / games: / bin: / home / bernd / bin: / home / bernd / bin2 current directory: / home / bernd / python Another elegant option is the path the execvpe () function can be expanded. A dictionary with environment variables can be given as a third argument. The environment variables are replaced by the values ​​in this dictionary: import os env = {"PATH": "/ home / bernd / bin2", "XYZ": "BlaBla"} args = ("test", "abc") os. execvpe ("test.sh", args, env) If we save the above script in execvpe.py, we get the following results on the shell when starting: $ ./execvpe.py /home/bernd/bin2/test.sh, abc XYZ : BlaBla PATH: / home / bernd / bin2 / current directory: / home / bernd / python / $ In the previous example, the value of the shell environment variable $ PATH is overwritten by the new value in the dictionary. If you want to avoid this, ie just append the new directory, you have to change the code as follows: import os path = os.environ ["PATH"] + ": / home / bernd / bin2 /" env = {"PATH": path, "XYZ": "BlaBla"} os.execlpe ("test.sh", "test", "abc", env) If you want to use execlpe () instead of execvpe (), you have to use the Python script as follows change: import os env = {"PATH": "/ home / bernd / bin2 /", "XYZ": "BlaBla"} os.execlpe ("test.sh", "test", "abc", env)

Overview of the exec functions