Python os.fork() Method
Example
Use os.fork():
#Import os Library
import os
# Fork a child process
processid
= os.fork()
print(processid)
# processid > 0 represents the parent
process
if processid > 0 :
print("\nParent Process:")
print("Process ID:", os.getpid())
print("Child's process ID:",
processid)
# processid = 0 represents the created child process
else :
print("\nChild Process:")
print("Process ID:",
os.getpid())
print("Parent's process ID:", os.getppid())
Try it Yourself »
Definition and Usage
The os.fork()
method forks a child
process.
The os.fork()
method creates a copy of the
process that has called it.
Executing os.fork()
creates two processes:
A parent process and a child process. It returns 0 in the child process, and the child's process id in the parent
process.
Use os.getpid() to get the process id within the child, and os.getppid() to get the process id of the parent process (within the child).
Note: Available only on UNIX platforms.
Syntax
os.fork()
Technical Details
Return Value: | An int value, returns 0 in the child and
the child's process id in the parent
|
---|---|
Python Version: | pre-2.7 |
Change Log: | 3.8: Calling fork() in a subinterpreter is no longer supported |