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

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,...
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...
Use fork() Create a program that reads characters from an input file. Use fork to create...
Use fork() Create a program that reads characters from an input file. Use fork to create parent and child processes. The parent process reads the input file. The input file only has letters, numbers. The parent process calculates and prints the frequency of the symbols in the message, creates the child processes, then prints the information once the child processes complete their execution. The child processes receives the information from the parent, generates the code of the assigned symbol by...
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...
Modify the bellow program by using only system calls to perform the task. These system calls...
Modify the bellow program by using only system calls to perform the task. These system calls are open,read,write, and exit .Use text and binary files to test your programs. #include <stdio.h> void main(int argc,char **argv) { FILE *fp1, *fp2; char ch, buf[20], *pos; int n; pos = buf; if ((fp1 = fopen(argv[1],"r")) == NULL) { puts("File cannot be opened"); exit(1); } fp2 = fopen(argv[2], "w"); while ((n = fread(pos, sizeof(char), 20, fp1)) == 20) { fwrite(pos, sizeof(char), 20, fp2); }...
How is the kill() system call different from all other system calls?
How is the kill() system call different from all other system calls?
Create a c++ program to compute the product of two integers. call create the following functions:...
Create a c++ program to compute the product of two integers. call create the following functions: 1. getNum - to accept only positive numbers && will call computeProd. 2.computeProd - to compute the product of the numbers & will call the function displayProduct. 3. displayProduct - to display the product. Call the function getNum in the main function. Compute the product w/o using the multiplication operator(*). using #include <iostream> only
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT