Question

Implement the C code that would generate the following sequence diagram for process creation. Your code should output the pid of each process (print the return value from getpid once in each process) as it is created. Make sure you keep track of what process you're in at any given time, and that your calls to fork are in the correct spot. (Also, ensure that you are checking if each call to fork succeeded or not and printing an appropriate error message.)

Process 2 Process 3 Process1 fork fork) return

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

Program:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

void main()
{
   pid_t pid;
  
   if( (pid = fork()) == -1 )
   {
       printf("\n fork failed \n");
       exit(1);
   }
  
   else if( pid == 0 )
   {
       printf("\nProcess 1, pid : %d\n",getpid());
      
       if( (pid = fork()) == -1 )
       {
           printf("\n fork failed \n");
           exit(1);
       }
      
       else if( pid == 0 )
       {
           printf("\nProcess 2, pid : %d\n",getpid());
          
           if( (pid = fork()) == -1 )
           {
               printf("\n fork failed \n");
               exit(1);
           }
      
           else if( pid == 0 )
           {
               printf("\nProcess 3, pid : %d\n\n",getpid());
           }
      
           else
           {
               wait(NULL);
           }
      
       }
      
       else
       {
           wait(NULL);
       }
   }
  
   else
   {
       wait(NULL);
   }
}

Execution and Output:

gcc o processtree processtree.c $./processtree Process 1, pid :4826 Process 3, pid :4828

Add a comment
Know the answer?
Add Answer to:
Implement the C code that would generate the following sequence diagram for process creation. You...
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
  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • 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...

  • Part 2. System Programming and Process Management You are asked to develop basic system programmi...

    Can someone help me create this program for Linux. Part 2. System Programming and Process Management You are asked to develop basic system programming including process creation and termination on a Linux platform in this part of programming project. You are asked to create multiple children processes to work under one parent process. In theory, children processes can do their own work separately or cooperatively to accomplish a task. In this assignment, these children processes simply print out a "hello"...

  • In C++, no code necessary just need to describe Describe how you would solve such a...

    In C++, no code necessary just need to describe Describe how you would solve such a problem: When a resident of a small town calls the 911 center , the address must be located from the phone number immediately. This town gets many calls but there are times when there are not enough police forces for each call received. So a queue is formed for getting help. But this queue is based on the importance of the calls. The 911...

  • In this module, you learned about Arrays in C++ and how to implement arrays within your...

    In this module, you learned about Arrays in C++ and how to implement arrays within your C++ programs. For this assignment, you will implement your knowledge of arrays for Michigan Popcorn Company’s sales management system. Write a program that lets the Michigan Popcorn Company keep track of their sales for seven different types of popcorn they produce: plain, butter, caramel, cheese, chocolate, turtle and zebra. It should use two parallel seven-element arrays: an array of strings that holds the seven...

  • I want to this question solution by programming c code. I need this solution as soon...

    I want to this question solution by programming c code. I need this solution as soon as possible i have to submit this code after 24 hours. please someone help this question thanks :D Programming Question: (15pts) Assume a finite number of resources of a single resource type must be managed. Processes may ask for a number of these resources and will return them once finished. If all resources are in use the request is denied and the process terminates....

  • in C++ Code should work for all cases In this assignment you are requested to implement...

    in C++ Code should work for all cases In this assignment you are requested to implement insert, search, and delete operations for an open-addressing hash table with double hashing. Create an empty hash table of size m= 13. Each integer of the input will be a key that you should insert into the hash table. Use the double hashing function h{k, i) = (hı(k) + ih2(k)) mod 13 where hi(k)= k mod 13 and h2(k) = 1+(k mod 11). The...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

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