Question

In: Computer Science

The following program calls the fork() system call to create a child process. Suppose that the...

The following program calls the fork() system call to create a child process.

Suppose that the actual pids of the parent process and child process are as follows:

Parent process: 801

Child process:   802 (as returned by fork)

Assume all supporting libraries have been included.

=> Use the answer text field to write the values of the pids printed at lines 1, 2, 3, and 4 (indicate each line number and the value printed).

int main()

{

     pid_t pid, pid1;

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

     if (pid < 0) {

          fprintf(stderr, "Error creating child.\n");

          exit(-1);

     }

     else if (pid > 0) {

          wait(NULL); /* move off the ready queue */

          pid1 = getpid();

          printf(pid); /* Line 1 */

          printf(pid1); /* Line 2 */

     }

     else {

          pid1 = getpid();

          printf(pid); /* Line 3 */

          printf(pid1); /* Line 4 */

     }

     return 0;

}

Solutions

Expert Solution

Please find the below explanations.

Please provide your feedback

Thanks and Happy Learning!


A 'pid_t pid = fork()' call can have 2 scenarios

 1. On success, the PID of the child process is returned to the parent, and 0 is returned in the child.  

     That means on a successful fork() call the value in the 'pid' variable will be the PID of the child process.

 2. On failure, -1 is returned in the parent, and no child process is created

That means 

If the PID value is zero(0), then that indicates a child process

If the  PID value is greater than zero(0), then that indicates a parent process

If the  PID value is equal to zero(0), then that indicates creation of child process failed.

Also the getpid() function will return the PID of the process which is invoking that call. That means if the getpid() is invoked by the parent then the call returns the parents PID, and if it is invoked by the child then the call returns childs PID.

With the above points in mind, please see the valus of PID's printed in each line below

Line 1 : PID = 802 (Child PID)

Line 2 : PID = 801 (Parent PID, because parent process invoked getpid())

Line 3: PID = 802 (Child PID)

Line 4: PID = 802 (Child PID, because child process invoked getpid())



Related Solutions

You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
11. A fork() function call is a system call. true or false 12. A fork() function...
11. A fork() function call is a system call. true or false 12. A fork() function call in a user program is executed in user mode, not in kernel mode. true or false 13. Alice writes a high-level program using some programming language on her home desktop computer and using compilation it transforms into an executable file. Bob's home desktop computer will not execute Alice's executable file. What language was Alice's program written with? 14. Alice rewrites her high-level program...
Write a C program which fork the process and have the child execute a process.You have...
Write a C program which fork the process and have the child execute a process.You have to implement void forkexec(char** argv).This takes in an array of strings representing arguments.The first argument is the filename of an executable (which will be given as a "./a").The remaining terms would be arguments for said executable.The array is null terminated and you need to fork your process.The child needs to call exec/execvp to execute the specified file with the specified arguments. Also have the...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
How is the kill() system call different from all other system calls?
How is the kill() system call different from all other system calls?
6) Create the following in a Java Program: Create payroll system Prompt the user to enter...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter payroll information Employee name and create variable for scanner Hours worked Hourly pay rate Federal tax rate State tax rate Declare variable doubles for gross pay, federal, state and total deduction Display payroll statement example: Name of employee Hours worked Hourly pay rate Gross pay which is equal hours worked multiply hourly pay rate Federal withholding which is equal of gross pay multiply federal...
Calculate relative frequencies for; Overall Type of Calls, Call Quality and Call Errors. Calculate the following...
Calculate relative frequencies for; Overall Type of Calls, Call Quality and Call Errors. Calculate the following probabilities: CLM Error and AM Shift COV Error and PM Shift SAV Error or AM Shift Given that the call comes in the morning, what is the probability of a CLM Error NARRATIVE: Based upon the probabilities in step 2, which error(s) should the company focus on for improvement? Should they direct their attention to a particular shift? If a sample size of 15...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
When a process executes a "sleep" system call, from the perspective of the process, it simply...
When a process executes a "sleep" system call, from the perspective of the process, it simply pauses for a specified amount of time and then resumes where it left off. Describe, in detail, what a sleep system call looks like from the perspective of the operating system. Regarding system calls: What is a system call? For each of the following system calls, give a condition that would cause it to fail: 1) open; 2) fork; and 3) exec.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT