Reputation: 1383
Iam programing in C language and trying to learn the concepts of forking a process , bt iam getting confused with the output of the following program. So i need some explanation regarding this to proceed.
int main() {
pid_t pid;
24 int status, died;
25 switch(pid = fork()){
26 case -1: printf("Can't fork\n");
27 exit(-1);
28
29 case 0 : printf(" Child is sleeping ...\n");
30 sleep(5); // this is the code the child runs
31 //exit(3);
32
33 default:
34 printf("Process : parent is waiting for child to exit\n");
35 died = wait(&status); // this is the code the parent runs
36 printf("Child's process table cleared...\n");
37 }
38 return 0;
39 }
The output of the above program is :
Process : parent is waiting for child to exit
Child is sleeping ...
Process : parent is waiting for child to exit
Child's process table cleared...
Child's process table cleared...
Here iam not getting why this "Child's process table cleared..." is coming twice. Pls explain.
Platform : Linux , gcc compiler
Upvotes: 0
Views: 304
Reputation: 1383
i got it what i was missing ... it is because of the absence of the break statement there.If i would have used break or exit(), output wouldn't have been like this. Thanks.
Upvotes: 0
Reputation: 28525
There is no break
in the child's case
statement and hence the child too executes the default
statement
You seem to have commented out exit(3)
. It would have been better if it were there.
Upvotes: 6