To fork a process is to create a second exact replica of the given process. i.e., When we fork something, we effectively clone it and then run it as a child process of the process that we just cloned from. The newly created process gets its own address space as well as an exact copy of the parent's data and the code executing within the parent process. The new cloned process receives its own unique (PID) Process identifier, and is independent of the parent process from which it was cloned.
Now, you’re required to write a Python script for forking existing processes by following this algorithm:
1.start by importing the os (operating systems) Python module.
2.then define two distinct functions, one called child() and another called parent(). Both the child() and the parent() just print out the process identifier(PID).
import os
def parent(pid_par):
print('Parent process PID: ', pid_par)
def child(pid_ch):
print('Child process PID: ', pid_ch)
def main():
rs = os.fork()
if rs > 0:
parent(os.getpid())
if rs == 0:
child(os.getpid())
if __name__ == '__main__':
main()
Comments
Leave a comment