user2799508
user2799508

Reputation: 848

The behaviour of fork() is not clear in this example

#include<stdio.h>

#include<stdlib.h>

#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main(void)

{

pid_t pid;
switch(pid =fork())

 {


case -1:

perror("fork"); /* something went wrong */
exit(1);
/* parent exits */


case 0:
printf(" This is the child process mention in case 0 of switch!, but why case 0 reached?\n");


default:
printf("default case: pid returned by fork is %d\n",pid );

 }
return 0;
}

Output

root@heena:/home/heena/fork# ./a.out 
default case: pid returned by fork is 4640
root@heena:/home/heena/fork#  This is the child process mention in case 0 of switch!, but why case 0 reached?
default case: pid returned by fork is 0

after that i had to press enter to get to the prompt.

Why case 0 is working when the case is default. Also how the command prompts comes automatically second time and executes the case 0? and then why default comes again in the last?

Upvotes: 2

Views: 104

Answers (3)

paxdiablo
paxdiablo

Reputation: 881103

This has little to do with fork and much to do with your switch statement.

Your child process returns from fork with a 0 return code so it goes into the case 0. Unfortunately, it then carries on down to default since you do not have a break statement at the end of the case 0. That's because the default behaviour at the end of a case is simply to fall through to the next case if there is one.

It's no different to:

int i = 1;
switch (i) {
    case 1:  puts ("It was one");
    case 2:  puts ("It was two");
    case 3:  puts ("It was three");
    default: puts ("It was something else");
}

which will end up with the seemingly psychotic output:

It was one
It was two
It was three
It was something else

To fix it (your code, not my sample above), use:

switch (pid =fork()) {
    case -1:
        perror ("fork");
        exit (1);  // will exit rather than fall through.
    case 0:
        printf (" This is the child process\n");
        break;     // will end switch rather than fall through.
    default:
        printf ("default case: pid returned by fork is %d\n",pid );
                   // nothing needed here since switch is ending anyway.
 }

Upvotes: 4

Dipto
Dipto

Reputation: 2738

You are confused by the output:
for the Parent Process, it goes to default and prints the pid, then exits as you are not waiting for the child to finish. And you see the command prompt.
You see this:

root@heena:/home/heena/fork# ./a.out
default case: pid returned by fork is 4640
root@heena:/home/heena/fork#

for the Child Process, case 0 is executed, and prints the line, then as there is no break statement after this case, it goes to the default case and prints the pid of this (i.e. child) process which is 0, and then exits.
So you see this:

This is the child process mention in case 0 of switch!, but why case 0 reached?
default case: pid returned by fork is 0

Just insert break; after the case 0: and the output will be clear to you.

Upvotes: 3

tristan
tristan

Reputation: 4312

you are missing a break in case 0

Upvotes: 1

Related Questions