Question: This assignment involves using LINUX system functions such as fork(), getpid(), getppid(), wait() and system(). --------------------------------------------------------------------- Write a program on the turing system using C
This assignment involves using LINUX system functions such as fork(), getpid(), getppid(), wait() and system().
---------------------------------------------------------------------
Write a program on the turing system using C or C++. Your executable file should be named Assign1. It should do the following:
1. Print a message identifying the process as the original process
and providing its PID and its parent's PID.
2. Call fork() after printing a message saying you are about to do
so.
3. If fork() fails, print an error message ("The first fork failed.")
and exit with a status of -1.
4. If fork() succeeds, we now have two processes: parent and child.
In the child process:
(a) Print a message identifying it as the child and providing its
PID and its parent's PID.
(b) Call fork() a second time after printing a message saying you
are about to do so.
(c) If fork() fails, print an error message ("The second fork
failed.") and exit as above.
(d) If fork() succeeds, we now have three processes: one parent,
one child and one grandchild.
In the grandchild process:
(i) Print a message identifying it as the grandchild and
providing its PID and its parent's PID.
(ii) Print a message saying it is about to exit.
(iii) Exit with a status of 0.
In the child process (after the second fork):
(i) Print a message identifying it as the child and
providing its PID and its parent's PID.
(ii) Use wait(0) to wait for the grandchild to terminate.
(iii) Print a message saying it is about to exit.
(iv) Exit with a status of 0.
In the parent process:
(a) Print a message identifying it as the parent and providing
its PID and its parent's PID.
(b) Call the sleep() function to sleep for 2 seconds.
(c) Print a message saying it is about to call ps.
(d) Use system() to execute the "ps" command.
(e) Use wait(0) to wait for the child to terminate.
(f) Print a message saying it is about to terminate.
(g) Exit with a status of 0.
5. The overall program should return a value of 0.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
