Question

C programming .

Create a tree of processes in the form of an inverted V with two process chains (A and B) as shown in
next figure:

AB A2 B2 An Bn

- To create processes, the program must use the fork () system function and test if
error occurs, not being allowed to use the system () function.

- Each process must wait for its child processes to finish before it finishes itself
(process Xi waits for process X (i + 1) in the chains and process AB waits for processes A1 and
B1).

- The following is an example of program execution, which in addition to being a reference also
shows the messages and output data to be printed by the program:

Inverted V process chain with n = 5:
AB process has PID = 12931


Process A1 has PID = 12932 and PPID = 12931
Process B1 has PID = 12933 and PPID = 12931
Process B2 has PID = 12934 and PPID = 12933
Process A2 has PID = 12935 and PPID = 12932
Process B3 has PID = 12936 and PPID = 12934
Process B4 has PID = 12938 and PPID = 12936
Process A3 has PID = 12937 and PPID = 12935
Process B5 has PID = 12939 and PPID = 12938
Process A4 has PID = 12940 and PPID = 12937
Process A5 has PID = 12941 and PPID = 12940

Example of Tree Diagram in this case :

АВ ( 81 В2 B3 В4 В5

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

Here is the C code for the above task:

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

int main() {

   int n;

   int i = 1; // loop variable

   printf("Enter value of n: ");

   scanf("%d", &n); // input n

   printf("AB process has pid = %d\n\n", getpid()); // print for AB process

   pid_t pid = fork(); // fork a child process

   if(pid == -1) { // error

       printf("Error!\n");

       return 0;

   }

   else if(pid == 0) { // A1 child process

       printf("Process A%d has PID = %d and PPID = %d\n", i, getpid(), getppid());

       for(i = 2; i <= n; ++i) {

           pid = fork(); // fork child for Ai process

           if(pid > 0) {

               wait(NULL); // make parent process wait for child to finish

               break; // make parent break out of the loop

           }

           printf("Process A%d has PID = %d and PPID = %d\n", i, getpid(), getppid());

       }

   }

   else {

       pid = fork(); // fork another child process

       if(pid == 0) { // B1 child process

           printf("Process B%d has PID = %d and PPID = %d\n", i, getpid(), getppid());

           for(i = 2; i <= n; ++i) {

               pid = fork(); // fork child for Ai process

               if(pid > 0) {

                   wait(NULL); // make parent process wait for child to finish

                   break; // make parent break out of the loop

               }

               printf("Process B%d has PID = %d and PPID = %d\n", i, getpid(), getppid());

           }

       }

       else {

           // make AB process wait for both child to finish

           while (wait(NULL) > 0);

       }

   }

}

Code screenshot:

C main.c )... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int maielse { pid = fork(); // fork another child process, if(pid == 0) { // B1 child process printf(Process B%d has PID = %d and P

Sample IO:
[unseen@x510unr]07:01 pm_$ -/prog/ -> gcc main.c [unseen@x510unr]07:01 pm_$ -/prog/ -> ./a.out Enter value of n: 2 AB process

Add a comment
Know the answer?
Add Answer to:
C programming . Create a tree of processes in the form of an inverted V with...
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
  • Excel is allowed! For this lab, we will create a spreadsheet that allows somebody to type...

    Excel is allowed! For this lab, we will create a spreadsheet that allows somebody to type in a loan amount, interest rate, and length of the loan in years. The spreadsheet will then calculate the monthly payment required and the actual amount paid on the loan. First, setup your spreadsheet: • In Cell A1, put the label Loan Amount:. The corresponding value would be input in Cell B1. • In Cell A2, put the label Interest Rate:. The corresponding value...

  • please help. thank you!! The Gilster Company, a machine tooling firm, has several plants. One plant,...

    please help. thank you!! The Gilster Company, a machine tooling firm, has several plants. One plant, located in St. Cloud, Minnesota, uses a job order costing system for its batch production processes. The St. Cloud plant has two departments through which most jobs pass. Plant-wide overhead, which includes the plant manager's salary, accounting personnel, cafeteria, and human resources, is budgeted at $250,000. During the past year, actual plantwide overhead was $232,000. Each department's overhead consists primarily of depreciation and other...

  • Using Table 2 (average data of 10 brands x 10 attributes), (a) create a positioning map...

    Using Table 2 (average data of 10 brands x 10 attributes), (a) create a positioning map by running SPSS, (b) put the overall mean (mean of all attributes per brand) on each brand, and (c) discuss which position must be appropriate for a new brand into the market. Table 2. Brand Positioning Mean Scores and Dimension Coordinates Mean scores (B = Brand and A = Attribute) BRAND A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 B1 3.87 3.13...

  • Here I am asked to create a program using functions and files in python 3.7. I...

    Here I am asked to create a program using functions and files in python 3.7. I am unfortunately not able to use lists in the project. Thanks CSE 231 Fall 2019 Programming Project 05 This assignment is worth 45 points (4.5% of the course grade) and must be completed and turned in before 11:59 PM on Monday, October 14, 2019. Assignment Overview 1. Functions 2. File input and output 3. try-except Computational facial recognition is growing. Starting with the iPhone...

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