What will be the output of the following c program?(you may enter and run the pr
ID: 3761748 • Letter: W
Question
What will be the output of the following c program?(you may enter and run the program on a unix/linux system if you like).
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
int i =5;
pid_t childpid;
printf("the parent says i=%i ",i);
/* fork-spawn a child process */
switch(childpid =fork())
{
case-1:
/* error forking */
printf("error during fork() command ");
return(1);
case 0:
/* child process */
i=20;
printf("the child says i = %i ",i);
break;
default:
/*parent process */
wait(NULL);
printf("the parent says i = %i ",i);
break;
}
return 0;
}
Explanation / Answer
//The output of this code will be of 2 possibilities.
//1. The child is not created. In this case the output is:
// the parent says i=5
// error during fork() command.
//2. The child is created, successfully, and 2 processes runs simultaneously.
//In this case the output is:
// the parent says i = 5
// the child says i = 20
// the parent says i = 5.
//Note that the second possibility is high, as in most of the cases,
//the child process will be created, unless in some extreme cases.
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
int i =5; //Initial i value is 5.
pid_t childpid;
printf("the parent says i=%i ",i); //this statement will be evaluated for sure.
/* fork-spawn a child process */
//Trying to fork the parent for a child.
//The point here to note is, the parent is trying to create a new child process,
//with the statement childpid = fork().
//There are 2 possibilities here.
//1. The child is unable to be created, in this case -1 will be returned by the fork() to this process.
//2. The child is created successfully. Here 2 processes, both the parent(the one which called the fork()),
// and the child(the one which is created by the fork()) will come into existence.
// And incidentally, the fork() returns to both the processes, in this case.
// A 0 will be returned to the child process, and the newly created process id will be returned to the parent.
// Note that, both the processes will be run simultaneously, in some order.
switch(childpid =fork()) //Calls for creation of child process.
{
case-1: //If unable to fork, i.e., the child can't be created due to some reasons.
/* error forking */
printf("error during fork() command "); //This statement will be executed.
return(1); //Stop execution.
//If the child has been created, both the case 0, and default case will be executed.
//case 0 by the child process, and default case by the parent process.
case 0: //This will be executed by the child process, as this is what returned by the fork statement to the child.
/* child process */
i=20; //i is assigned a value of 20.
printf("the child says i = %i ",i); //This value is printed.
break;
default:
/*parent process */
wait(NULL); //Wait for some period.
printf("the parent says i = %i ",i); //This statement prints the value of i, which is 5.
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.