When I run the following code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = vfork();
printf("hello world\n");
}
Output:
hello world
hello world
hello world
Segmentation fault
I know that unless exec() or _exit() is called then vfork() can behave in strange manner if we try to modify any variable but can someone please explain what exactly is happening?? why hello world is getting printed 3 times? Is it because printf() is getting buffered? and finally why a seg fault is occuring just when parent is trying to return?
Seems like you violate all the conditions for using vfork. So then it doesn't work.