Question

Write separate programs, as described below, that create 7 processes according to the parent/child hierarchy shown...

Write separate programs, as described below, that create 7 processes according to the parent/child hierarchy shown on the right, where P0 is a parent to P1 and P2, etc.Before each parent terminates, it must wait until all of its children have terminated.When created, every process should print its index (0 – 6), its PID, and its parent’s PID. Each process does no work and prints a message indicating when it is done. In your report, include your program output and a brief description of how you designed the program.

  1. Use a full, nested if-else structure after every fork() instruction such that one branch handles the parent and one branch handles the child.

  2. Use if statements without “else” and no more than 2 if-levels deep (maximum of one if inside another if). Note that P3 – P6 can also call “wait” even though they do not have children (has no effect).

The only requirement for output ordering is that it should demonstrate the necessary creation and termination of parents relative to children. For example, P1 should clearly be created some time before P3 and P4, and it should clearly terminate some time after they do. However, P1 output does not need a particular order relative to P2 or its children. OPTIONAL: If you wish, you may create a helper function to perform tedious tasks, such as printing (or more!). Note that defining a C function after main() requires a prototype (https://computer.howstuffworks.com/c13.htm).

Below are two examples of possible output. Theoretically, both are possible for both programs. You may get slightly different ordering every time you run your progra

Example #1:

Created P0 (PID 207). Parent is PID 4.
Created P1 (PID 208). Parent is PID 207.
Created P2 (PID 209). Parent is PID 207.
Created P3 (PID 210). Parent is PID 208.
P3 is done.
Created P4 (PID 211). Parent is PID 208.
P4 is done.
P1 is done.
Created P5 (PID 212). Parent is PID 209.
Created P6 (PID 213). Parent is PID 209.
P5 is done.
P6 is done.
P2 is done.
P0 is done.

Example #2:

Created P0 (PID 382). Parent is PID 4.
Created P1 (PID 383). Parent is PID 382.
Created P3 (PID 384). Parent is PID 383.
Created P2 (PID 385). Parent is PID 382.
Created P4 (PID 386). Parent is PID 383.
P3 is done.
P4 is done.
P1 is done.
Created P5 (PID 387). Parent is PID 385.
Created P6 (PID 388). Parent is PID 385.
P5 is done.
P6 is done.
P2 is done.
P0 is done.

Tip (output order): Don’t forget to use fflush(stdout) to get reasonable output ordering. This was demonstrated in sleep.c in HW #1 and discussed at the end of a lecture exercise on using fork().

Tip (self identity): In part (b), processes can decide what to do based on their own identities. You can use several if statements, where each if statement checks its process’s identity. Rather than using the PID assigned by the OS, an easier way for a process to track its own identity can be to use a variable that stores a value from 0 to 6. For example, if process #0 makes child process #2, that child process can immediately set its identity to be 2. Then any actions that P2 needs to perform can be done after checking for this custom identity value of 2. In this way, you don’t need to worry about keeping track of the PID assigned by the OS.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int parent_pid;
int main(int argc, char const *argv[])
{
char *tto = strdup(argv[1]);
int n = atoi(tto);
int a[n];
parent_pid=getpid();
printf("Parent is : %d\n",getpid());
printf("Number of children: %d\n",n);
for (int i = 0; i < n; ++i) {
pid_t pid;
if ((pid = fork()) == -1) {
fprintf(stderr, "Error in fork()\n");
abort();
}
if(pid==0)
{
sleep(rand() % 20 + 1);
printf("Child %d exited \n", getpid());
fflush(stdout);
exit(0);
}
else{
a[i]=pid;
printf("Child %d is created \n", pid);
}
}
for (int i=0 ; i< n; i++)
{
int status;
waitpid(a[i], &status, 0);
{
}
}   
if(getpid()==parent_pid) printf("Parent exited\n");
return EXIT_SUCCESS;
}


Add a comment
Know the answer?
Add Answer to:
Write separate programs, as described below, that create 7 processes according to the parent/child hierarchy shown...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT