Question

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;

}

Homework Answers

Answer #1

Here is the solution. Please do upvote thank you.

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).

Know the answer?
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
Similar Questions
Using the program below, identify the values of pid at lines A, B, C, and D....
Using the program below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)   #include <sys/types.h>   #include <stdio.h>   #include <unistd.h>      int main()   {   pid_t pid, pid1;        /* fork a child process */     pid = fork();        if (pid lt; 0) { /* error occurred */       fprintf(stderr, "Fork Failed");       return 1;     }     else if (pid == 0) { /* child process */       pid1 = getpid();       ...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to be output immediately. (For space reasons, we are not checking error return codes, so assume that all functions return normally.) int main () {    if (fork() == 0) {        if (fork() == 0) {           printf("3"); fflush(stdout);        }        else {           pid_t pid; int status;           if ((pid = wait(&status)) > 0) {                  printf("4"); fflush(stdout);           }        }...
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT