Question

In: Computer Science

how to run child process and parent process in parallel/at the same time in python using...

how to run child process and parent process in parallel/at the same time in python using os

Solutions

Expert Solution

**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**


Related Solutions

Using pipes for inter-process communications, write a C program to produce a parent/child relationship in which...
Using pipes for inter-process communications, write a C program to produce a parent/child relationship in which the parent process repeatedly counts and displays on the screen the number of characters for any user password the child process inputs to the pipe. The communications should cease once the string “Done” is inputted.
How could you describe this a sensitive child to a parent that feels their child is...
How could you describe this a sensitive child to a parent that feels their child is just shy (a parent that overlooks their child's sensitive behavior)? Without telling the parent they are wrong about their child's behavior?
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then...
How does the parent-child relationship change during emerging adulthood?
How does the parent-child relationship change during emerging adulthood?
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t =...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t = 0, the first airplane is 10 km ahead of the second one. Suppose the speed of the first plane (km/hr) is normally distributed with mean 550 and standard deviation 9 and the second plane's speed is also normally distributed with mean and standard deviation 535 and 9, respectively.   a) What is the probability that after 2 hr of flying, the second plane has...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t =...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t = 0, the first airplane is 10 km ahead of the second one. Suppose the speed of the first plane (km/hr) is normally distributed with mean 530 and standard deviation 8 and the second plane's speed is also normally distributed with mean and standard deviation 510 and 8, respectively. (a)What is the probability that after 2 hr of flying, the second plane has not caught...
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child...
Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child class. // File name: Person.h // Person is the base, or parent for chapter11 #pragma once #include <iostream> #include <string> using namespace std; class Person { private:    string fName;    string lName;    int areaCode;    int phone; public:    Person();    Person(string, string);    void setFirst(string);    void setLast(string);    void setPhoneNumber(int, int);    string getFirstlast();    string getLastFirst();    string getPhoneNumber();...
Explain how an economy can experience long-run economic growth and deflation at the same time?
Explain how an economy can experience long-run economic growth and deflation at the same time?
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t = 0, the...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t = 0, the first airplane is 10 km ahead of the second one. Suppose the speed of the first plane (km/hr) is normally distributed with mean 535 and standard deviation 9 and the second plane's speed is also normally distributed with mean and standard deviation 510 and 9, respectively. (a) What is the probability that after 2 hr of flying, the second plane has not caught...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t 5...
Two airplanes are flying in the same direction in adjacent parallel corridors. At time t 5 0, the first airplane is 10 km ahead of the second one. Suppose the speed of the first plane (km/hr) is normally distributed with mean 520 and standard deviation 10 and the second plane’s speed is also normally distributed with mean and standard deviation 500 and 10, respectively. What is the probability that after 2 hr of flying, the second plane has not caught...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT