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

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

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

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

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

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

  • Java Concurrency! You have been given a simple API to buy and print postage. Write a...

    Java Concurrency! You have been given a simple API to buy and print postage. Write a program that submits each incoming order to the API and then prints the shipping label. Unfortunately each call to the shipping label API is SLOW… To compensate for this, use concurrency so that your program can be making several calls to the API at one time. We also need to keep track of our expenses, so make sure to calculate how much money is...

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
ADVERTISEMENT