What does Exit 0 do in Python
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 PythonWhen 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 * () functionsThe 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)
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
Overview of the exec functions
- How do I treat my hypoglycemia
- What time of year are the Walking Dead out and about
- How to Compare Thailand and Laos
- What makes hard water soft
- How do I master design skills
- How long are periscopes on submarines
- What mixtures can you find in your home
- Why is organic milk better than normal
- Which consensus model does Ethereum use
- What is the greatest teaching in Christianity
- How can I download Gangster Vegas 1?
- How can we start to trust ourselves
- What are aqueous solvents
- Why is pyruvate converted to lactic acid
- Should I study international relations or finance?
- Is the Vivo V11 Pro waterproof
- What is the chemical name of HClO4
- What is a spaceship
- Who is stronger than Thanos or Goku
- Deliberately lies to Sanders
- Why are trees planted symmetrically on the roadside
- Is it possible to create warm ice
- How do I play an electric guitar
- The world would be better without weapons