How to use vfork to create multiple process?

448 views Asked by At

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

There are 1 answers

4
Duck On

(1) This creates a single child.

(2) With vfork the child shares memory with the parent until either exec 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 fast fork mechanism that will immediately exec another program. The need for this has been vastly diminished with the copy-on-write behavior of regular fork.