Answer to Question #227126 in Python for Lesley

Question #227126
To fork a process is to create a second exact replica of the given process. i.e., When we fork something.
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.
1
Expert's answer
2021-08-18T06:51:40-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS