1. Use the template below to write a C-program that creates two processes: "Process A" (the process with whích the program starts execution) and "Process B" (A's child). The Process A forks Process B. To identify which process writes the output, each process puts its id at the beginning of each line (see the sample output). Each process performs the following operations. Process A: After spawning Process B, it waits for Process B to terminate. After Process B's termination, it will write your home directory (use HOME environment variable) and its process ID, and terminates. Process B: After being spawned from Process A, it sleeps for three seconds (using sleep0)- Then, it writes process IDs of itself and Process A immediately. After that, executes ps command (using system library) and terminates. Sample output: -fa.out 9181:1 am the parent, I will create child process 9182:1 am the child, I will sleep for 3 seconds 9182:My parent pid is 9181 9182:the ps command PID TTY TIME CMD 8931 pts/0 00:00:00 bash 9181 pts/0 00:00:00 a.out 9182 pts/0 00:00:00 a.out 9183 pts/0 00:00:00 ps 9181: My home directory is /home/user Template: 78 |Page #include #include /* contains fork prototype / main(void) int pid,status; //printf the first message //fork B process if (pid == 0) //child process // print message // sleep 3seconds // print message // print message // executes ps command printf("\n"); else // parent waits for child to finish // print message system("echo SHOME"); printf("\n"):

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
EXCERSICES
1. Use the template below to write a C-program that creates two processes: "Process A" (the
process with which the program starts execution) and "Process B" (A's child). The Process
A forks Process B. To identify which process writes the output, each process puts its id at
the beginning of each line (see the sample output). Each process performs the following
operations.
Process A: After spawning Process B, it waits for Process B to terminate. After Process B's
termination, it will write your home directory (use HOME environment variable) and its
process ID, and terminates.
Process B: After being spawned from Process A, it sleeps for three seconds (using sleep().
Then, it writes process IDs of itself and Process A immediately. After that, executes ps
command (using system library) and terminates.
Sample output:
-/a.out
9181:1 am the parent, I will create child process
9182:1 am the child, I will sleep for 3 seconds
9182:My parent pid is 9181
9182:the ps command
PID TTY
TIME CMD
8931 pts/0 00:00:00 bash
9181 pts/0 00:00:00 a.out
9182 pts/0 00:00:00 a.out
9183 pts/0 00:00:00 ps
9181: My home directory is
/home/user
Template:
78 |Page
#include <stdio.h>
#include <unistd.h> /* contains fork prototype */
main(void)
{
int pid,status;
//printf the first message
//fork B process
if (pid == 0) //child process
{
// print message
// sleep 3seconds
// print message
// print message
// executes ps command
printf("\n");
}
else
// parent waits for child to finish
// print message
system("echo SHOME");
printf("\n");
Transcribed Image Text:EXCERSICES 1. Use the template below to write a C-program that creates two processes: "Process A" (the process with which the program starts execution) and "Process B" (A's child). The Process A forks Process B. To identify which process writes the output, each process puts its id at the beginning of each line (see the sample output). Each process performs the following operations. Process A: After spawning Process B, it waits for Process B to terminate. After Process B's termination, it will write your home directory (use HOME environment variable) and its process ID, and terminates. Process B: After being spawned from Process A, it sleeps for three seconds (using sleep(). Then, it writes process IDs of itself and Process A immediately. After that, executes ps command (using system library) and terminates. Sample output: -/a.out 9181:1 am the parent, I will create child process 9182:1 am the child, I will sleep for 3 seconds 9182:My parent pid is 9181 9182:the ps command PID TTY TIME CMD 8931 pts/0 00:00:00 bash 9181 pts/0 00:00:00 a.out 9182 pts/0 00:00:00 a.out 9183 pts/0 00:00:00 ps 9181: My home directory is /home/user Template: 78 |Page #include <stdio.h> #include <unistd.h> /* contains fork prototype */ main(void) { int pid,status; //printf the first message //fork B process if (pid == 0) //child process { // print message // sleep 3seconds // print message // print message // executes ps command printf("\n"); } else // parent waits for child to finish // print message system("echo SHOME"); printf("\n");
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY