Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this recitation is to write two short C programs showing the parent-child process relationship in Linux using the fork() system call. Specifically,

The purpose of this recitation is to write two short C programs showing the parent-child process relationship in Linux using the fork() system call. Specifically, we will look at how a zombie and orphan process are created as well as handled by the operating system. 1. Zombie Process Recall that when a child terminates, but its parent is not currently waiting, the terminated child process becomes a zombie process. Use the following algorithm and specific instructions to develop a short, simple C program called rec06A.c to create a zombie process. a. Add the include directives for the stdlib.h and unistd.h libraries. b. Declare a variable called pid with the data type pid_t and invoke the fork() system call, assigning the result to pid. c. Write an if else if else branching statement to handle the child process, parent process, and error case, respectively. i. In the child process, exit immediately. ii. In the parent process, sleep for 3 seconds, then issue the system() system call with "ps -e -o pid,ppid,stat,user,cmd | grep $USER". This prints out the current PID, parent PID, status, user ID, and command process information. iii. In the error case, call the perror() system call with "fork error". Now, compile and run your program. After 3 seconds, you should see the result of the ps command. Specifically, look for the process with the Z+ status. This is an indication that it is a zombie process. This process should also be the child process of your parent ./a.out executable that contains the indicator. Now, on the command line, enter the ps -u $USER command to see your existing processes. You should notice that this zombie process no longer exists as it has been reaped by the init process. You will turn in this rec06A.c file to Canvas.

2. Orphan Process Recall that a child process whose parent has terminated is referred to as an orphan process. This means that the child is still executing, but its parent has terminated and that some process is then needed to handle the childs exit status. Use the 2 following algorithm and specific instructions to develop a short, simple C program called rec06B.c to create an orphan process. a. Add the include directives for the stdio.h, stdlib.h and unistd.h libraries. b. Declare a pointer to a char called name. c. Declare a variable called pid with the data type pid_t and invoke the fork() system call, assigning the result to pid. d. Write an if-else if-else branching statement to handle the child process, parent process, and error case, respectively. i. In the child process, print "child: %d started " using the getpid() system call for the %d. Then, print "child: parent = %d " using the getppid() system call for the %d. Now, print another child status (use "child: ") to indicate the child is about to go to sleep. Then, sleep for 25 seconds. Finally, print a child status to indicate that the child just woke up. ii. In the parent process, print "parent: %d started " using the getpid() system call for the %d. Then print "parent: parent = %d " using the getppid() system call for the %d. iii. In the error case, call the perror() system call with "fork error". e. Outside of the if-else if-else branching statement, use the ternary operator (condition) ? (if_true) : (if_false) to assign "child" to the name character pointer if pid is 0 and "parent" otherwise. Then, print "%s: terminating " using the name character pointer for the %s. Now, compile and run your program. After your program initially runs, but before the child process has terminated, enter the ps -ef|grep $USER command to see your existing processes. You should notice your child process is still running, but has 1 for its parent process this means that the init process has taken over as the parent (since our parent terminated) and will handle the child once it has terminated.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Manga Guide To Databases

Authors: Mana Takahashi, Shoko Azuma, Co Ltd Trend

1st Edition

1593271905, 978-1593271909

More Books

Students also viewed these Databases questions

Question

Explain the generic target costing process.

Answered: 1 week ago

Question

Differentiate 3sin(9x+2x)

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago

Question

Compare the advantages and disadvantages of external recruitment.

Answered: 1 week ago

Question

Describe the typical steps in the selection process.

Answered: 1 week ago