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
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 =...
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”,...
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();       ...
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,...
Debug code to get it to only count non alpha numeric values. Right now it always...
Debug code to get it to only count non alpha numeric values. Right now it always outputs zero. Why? please explain /*Problem 1*/ #include <stdio.h> #include <string.h> #include <stdlib.h> int loop_count; char scanned_line[100]; int non_alpha_num_checker = 0; int total; int main() { printf("Enter a line: "); fgets(scanned_line,sizeof scanned_line, stdin); for(loop_count = 0; loop_count != '\0'; loop_count++){ if(scanned_line[loop_count] >= 'A' && scanned_line[loop_count]<='Z') { continue; }    else if(scanned_line[loop_count] >= 'a' && scanned_line[loop_count]<='z') { continue; } else if(scanned_line[loop_count] >= '0' && scanned_line[loop_count]<='9')...
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); } } } } }
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...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line...
Write a C/C++ program that performs the tasks described below. If there is just 1 command-line argument and it is -hw you should simply print hello world and then exit(0). Otherwise, the program should provide a function named mm_alloc. This is the function prototype: void *mm_alloc(int num_bytes_to_allocate) mm_alloc function should obtain space for the user from a block which you obtain via mmap. You should obtain the block on the first invocation and the block size should be some number...
"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...