Answer to Question #226633 in Python for Dion Martins

Question #226633

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).


1
Expert's answer
2021-08-17T12:55:24-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