Your program creates a single child process, printing at least one message from
ID: 3822502 • Letter: Y
Question
Your program creates a single child process, printing at least one message from both the parent and child process indicating the FIDs of those processes Your parent process should wait for the child to terminate and print a message once the child has completed Your program creates multiple child processes without using a loop, printing messages at the start and end of each process as described in part A. Your program uses a loop to create ten (10) child processes, printing messages at the start and end of each process as described in part A Your program is almost identical to part C, but the number of child processes is based on a command line argument passed to your executable (For example, if your executable is named "pro j 1". executing the command ./pro j 1 6 will run a version of your program that creates 6 child processes Assume the maximum number of child processes is 25. Your program is almost identical to part D, but the program is able to discern when each of its child processes completes and print an appropriate message (For example, when the first child process completes, print a message saying, "Child 1 (PID xxxxx) finished", where xxxxx would be replaced by the actual PID Your program is almost identical to Part F., but each child process starts a new program, replacing the address space of the parent process with that of the new program For this part, all child processes should start the same program.Explanation / Answer
A)code:
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid, status;
/* Creates Child process */
pid=fork();
if(pid == 0)
{
printf(" I am Child Process and my process id is %d", getpid());
exit(0);
}
else if(pid<0) {
printf(" Error");
exit(1);
}
else
{
/* parent waits for child to terminate */
wait(&status);
printf(" I am Parent Process and my process id is %d", getpid());
exit(1);
}
}
output:
I am Child Process and my process id is 25744
I am Parent Process and my process id is 25741
C) Code:
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>
void DoWorkInChild() {
sleep(10);
}
int main()
{
pid_t pid[10];
pid_t pid1;
int i, noc = 10, status;
/* Creating child processes */
for(i=0;i<noc;i=i+1) {
if((pid[i] = fork())< 0) {
printf(" Error");
exit(1);
}
else if (pid[i] == 0) {
DoWorkInChild();
printf(" child created with pid %d",getpid());
exit(0);
}
}
/* wait for child to exite and remove pid values ne by on from the list */
while(noc>0) {
pid1 = wait(&status);
printf(" child with process id %d exited",pid1);
noc=noc - 1;
}
}
output:
child created with pid 9411
child created with pid 9409
child created with pid 9412
child created with pid 9413
child created with pid 9408
child created with pid 9407
child created with pid 9406
child created with pid 9405
child created with pid 9410
child created with pid 9404
child with process id 9407 exited
child with process id 9408 exited
child with process id 9409 exited
child with process id 9412 exited
child with process id 9413 exited
child with process id 9405 exited
child with process id 9406 exited
child with process id 9410 exited
child with process id 9404 exited
child with process id 9411 exited
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.