Question

In: Computer Science

Write a C program to create a series of processes, as shown below in the diagram....

Write a C program to create a series of processes, as shown below in the diagram.

P |------ ---- > c1 |------------>c2

                             |------- ------>c3 ---------->c4

                           

In other words, the parent process p creates process c1, c1 creates c2 and c3, and c3 creates c4.

A sample run of the program shows

Your program should output something similar to what is shown above. You could optionally use wait/waitpid/sleep/exit in your program.

Comment your code to show where you created p, c1, c2, c3, c4, etc.

Solutions

Expert Solution

We use the fork() call, which returns a value of type pid_t. The distinguishing factor is :

  • If the return value returned is equal to 0, it means we are in the child-process's context.
  • If the return value returned is not equal to 0, it means we are in the parent-process's context.

Following is the code in multiple_forks.c :

#include <unistd.h>
#include <stdio.h>

int main()
{
    /*
     * P
     */
    printf("P : [0x%x]\n", getpid());

    {
        pid_t pid = fork();

        if(pid == 0)
        {
            /*
             * Context: c1
             */
            printf("c1 : [0x%x], c1's parent : [0x%x]\n",
                   getpid(), getppid());

            {
                pid_t pid = fork();

                if(pid == 0)
                {
                    /*
                     * Context: c2
                     */
                    printf("c2 : [0x%x], c2's parent : [0x%x]\n",
                           getpid(), getppid());
                }
                else
                {
                    /*
                     * Context c1
                     */
                    {
                        pid_t pid = fork();

                        if(pid == 0)
                        {
                            /*
                             * Context: c3
                             */
                            printf("c3 : [0x%x], c3's parent : [0x%x]\n",
                                   getpid(), getppid());

                            {
                                pid_t pid = fork();

                                if(pid == 0)
                                {
                                    /*
                                     * Context: c4
                                     */
                                    printf("c4 : [0x%x], c4's parent : [0x%x]\n",
                                           getpid(), getppid());         
                                }
                            }

                        }
                    }
                }
            }
        }
    }


    /*
     * Loop idefinitely, so that no process dies.
     */
    while(1)
    {
        sleep(1000);
    }


    return 0;
}

Following is a sample run :

./multiple_forks 
P : [0x1f10]
c1 : [0x1f11], c1's parent : [0x1f10]
c2 : [0x1f12], c2's parent : [0x1f11]
c3 : [0x1f13], c3's parent : [0x1f11]
c4 : [0x1f14], c4's parent : [0x1f13]

As seen,

  • The parent-process ID for c1 (0x1f10) is equal to the process ID of P.
  • The parent-process IDs for c2 and c3 are equal (0x1f11) to the process ID of c1.
  • The parent-process ID for c4 (0x1f13) is equal to the process ID of c3.

Related Solutions

Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program,...
The cos(x) function can be represented in a Taylor series shown below: Write a Matlab program, and use a while loop, to calculate cos(150) (the input is in degrees) by adding terms of the series and stopping when the absolute value of the term that was added last is smaller than 0.0001. Make sure to make the required degree <-> radian conversions. Use fprintf to print the cos(150) (up to 2 decimal places) and the number of terms used to...
Write a program in MATLAB that processes a series of employees to determine the days until/since...
Write a program in MATLAB that processes a series of employees to determine the days until/since a given employee’s birthday as follows: To populate the employees' data, read in a comma separated file containing First Name, Last Name, Birthday Month, Birthday Day, and Salary. Use the file uploaded in Loud Cloud as an example. Build up an array of employee records (i.e., structures) where each record contains the employee's first name, last name, birthday (stored as an integer representing days...
PROBLEM: Write a C program that will produce the EXACT output shown below. Using initialization lists,...
PROBLEM: Write a C program that will produce the EXACT output shown below. Using initialization lists, create 5 one-dimensional arrays, one for each of these: hold the names of the people running in the election (see note below) hold the names of the subdivision (see note below) hold the vote counts in the Aberdeen subdivision for each candidate hold the vote counts in the Brock subdivision for each candidate hold the vote counts in the Sahali subdivision for each candidate...
Write a C++ program for storing information on on a series of balls collected by a...
Write a C++ program for storing information on on a series of balls collected by a person. The balls should have these characteristics: 1. Diameter in mm 2. Color 3. if the texture of the surface is smooth or rough 4. an identification id/number The program should allow the person to enter the values for the balls' attributes as they are entered in a database. The program should then offer the choice to save all new data into a file....
Write a C++ program for storing information on on a series of balls collected by a...
Write a C++ program for storing information on on a series of balls collected by a person. The balls should have these characteristics: 1. Diameter in mm 2. Color 3. if the texture of the surface is smooth or rough 4. an identification id/number The program should allow the person to enter the values for the balls' attributes as they are entered in a database. The program should then offer the choice to save all new data into a file....
Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Create a memory diagram for the following C program when the program reaches point 1. void...
Create a memory diagram for the following C program when the program reaches point 1. void foo (int *a); int bar(int *x); int search(int *c); int main(void) { int q, p=10; q= search(&p); return 0; } void foo(int *a) { *a += 2; //point 1 *f *= 10; } int bar(int *x) { return 2* *x; } int search(int *c) { int z= *c + bar(c); foo(c); return z; }
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT