In: Computer Science
how to run child process and parent process in parallel/at the same time in python using os
**CODE STARTS**
#library to use os functions for processes.
import os
#os.getpid() is used to pid of this process.
print("Process id before forking: {}".format(os.getpid()))
try:
#os.fork() is used to create a new child of
parent
pid = os.fork()
except OSError:
exit("Could not create a child process")
if pid == 0:
#if pid==0, means we are inside child's program.
print("In the child process that has the PID
{}".format(os.getpid()))
exit()
print("In the parent process after forking the child
{}".format(pid))
#os.waitpid() is used so that parent wait for child process to
die
finished = os.waitpid(0, 0)
print(finished)
**CODE END**
**CODE SNIPPET END**