I have written a code for checking vfork in unix. But I am getting the ambiguous behavious.
When I am using exit(0) in child process , output is fine i.e;
output :-
Before fork
I am a child
My name is parent child PID=> 23133 kValue => 2
]
But when I am using "return 0" in place of exit(0) , program keeps on executing and each time a new process is created infinitely.
output :-
Before fork
I am a child
My name is parent child PID=> 23143 kValue => 2
Before fork
I am a child
My name is parent child PID=> 23144 kValue => 2
Before fork
I am a child
..... so on
Can anyone explain me the reason of this behavior please ? I know in case of vfork same memory space is shared between parent and child but I am not able to understand this behavior. Thanks a lot in advance.
Here is the code that I have written -->
#include <stdio.h>
#include <stdlib.h>
int main()
{
int pid,k=1;
printf("Before fork \n");
pid = vfork();
if(pid == 0) {
printf("I am a child \n");
k++;
return 0; // On exit(0) works fine.
} else
sleep(3);
printf("My name is parent child PID=> %d kValue => %d\n",pid,k);
return 0;
}