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).
3 the parent() function, first prints out the PID of the process that we are in before calling the os.fork() method to fork the current running process. This creates a brand new process, which receives its own unique PID. Then call the child() function, which prints out the current PID. This PID, should be different from the original PID that was printed out at the start of the script's execution. This different PID represents a successful forking and a completely new process being created.
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