Question

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 statuses to other processes.

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)

{ pid_t fork_returned_pid;
int count;
int return_status;

fork_returned_pid = fork();

if (fork_returned_pid < 0)
{ printf("The fork() didn't work! Terminate\n");
exit (0); }

if (fork_returned_pid != 0)
{ wait(&return_status); // This line is new
if (WIFEXITED(return_status))
printf("\nChild Terminated Normally with a return value of %d\n",
WEXITSTATUS(return_status));
}

for (count = 0; count < 10; count++)
{ if (fork_returned_pid == 0)
printf(" child says: count = %d\n", count);
else
printf("parent says: count = %d\n", count);
}

printf("\n");

if (fork_returned_pid == 0)
exit(10);
else
exit(20);
}

Homework Answers

Answer #1

In this program , we get output as

 ./main
child says: count = 0
child says: count = 1
child says: count = 2
child says: count = 3
child says: count = 4
child says: count = 5
child says: count = 6
child says: count = 7
child says: count = 8
child says: count = 9


Child Terminated Normally with a return value of 10
parent says: count = 0
parent says: count = 1
parent says: count = 2
parent says: count = 3
parent says: count = 4
parent says: count = 5
parent says: count = 6
parent says: count = 7
parent says: count = 8
parent says: count = 9

/* We get output as given above as in this program we are using fork system call which creates one child process . So there are two processes ,one is parent and other is child process. Child process execcutes the code after the statement fork system call. How we can distinguish between child and parent is using the return value .When we call fork system call.In this program variable fork_returned_pid . If fork_returned_pid is equal to zero then it's child process and if it is greater than zero (in parent process , fork_returned_pid value will be PID of child process) then it's parent process. At this point we don't know which process is given time slice to execute the code. Suppose say parent process got time slot to get executed, in parent process , we have wait system call called as first statemt after fork . Parent process waits until it's child process exits or signal is recieved. So child process starts executing and it prints the message

child says: count = 0 , these messages are printed till child says: count = 9 , after that chld exists and parent process receives this status and starts executing from where it has left and we see the message from parent process as

parent says: count = 0
parent says: count = 1
parent says: count = 2

|

|

so on till parent says: count = 9

Hope it helps !!

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
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”,...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A child is created and prints a message five times. The original process (parent) prints a message only three times. How would this code look on the compiler if you could please post the code and the compiler results. Need Reference Thank You #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t pid; char *message; int n; printf("fork program starting\n"); pid =...
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();       ...
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); } } } } }
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,...
currently the code contacts the server 1 time and exits. Modify the code so that the...
currently the code contacts the server 1 time and exits. Modify the code so that the program contacts the server five times before exiting. /* client.c - code for example client program that uses TCP */ #ifndef unix #define WIN32 #include <windows.h> #include <winsock.h> #else #define closesocket close #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #endif #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #define PROTOPORT 5193 /* default protocol port number */ extern int errno; char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...