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

Output:

Line 1: 802

Line 2: 801

Line 3: 0

Line 4: 802

Explanation:

Parent Process Id = 801 (Given)

Child Process Id = 802 (Given)

Flow of the program and initialisation of all pid variables:

  • The "else if block" (for pid > 0) executes for the parent process, it stays on hold until child process terminates because of invocation of wait system call.
  • The control moves to the child process i.e. the "else block" (i.e. pid == 0) in it and calls getpid() which makes pid1 = Child Process id = 802.
  • After that, the wait ends and the parent process resumes i.e. its "else if block" again. It has its variable pid = 802 (the process id of the child process returned from fork()) and pid1 = 801 (i.e. process id of the parent process' itself got from calling getpid() function).

Hope it resolved your query. Consider hitting a like if it did.

For any doubt, feel free to ask me back, would be happy to help! :)

Have a nice day ahead! Cheers!


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