This is program for vfork(). This program creates multiple parent and child processes and return -1 at the end (mean OS cannot create another process). Why such behaviour happens?
#include<stdio.h>
void main()
{
int pid;
pid=vfork();
printf("pid=%d\n",pid);
if(pid==0)
{
printf("hello\n");
}
}
(1) This creates a single child.
(2) With
vfork
the child shares memory with the parent until eitherexec
or_exit
are called. You call neither.(3) The parent's execution is suspended until the child calls
exec
or_exit
.So basically your example is FUBAR. The point of
vfork
(if there really is one these days) is to provide a fastfork
mechanism that will immediatelyexec
another program. The need for this has been vastly diminished with the copy-on-write behavior of regularfork
.