SysCW
SysCW

Reputation: 1

Return type of vfork()

From the GNU manual:

The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(),

What does it mean? Does it mean the return value of vfork() cannot be assigned to a non-pid_t type variable?

Upvotes: 0

Views: 127

Answers (1)

RKou
RKou

Reputation: 5221

The manual is quite confusing on this. Actually, both processes (the child and the father) shared the same address space, even the stack!

vfork() returns twice:

  1. In the child process, returning 0
  2. When the child is either finished or executed some other program, the second return is done in the father process with the child's process identifier. Meanwhile, the father process was suspended.

The return code of fork()/vfork() is typically stored in a variable (of type pid_t to follow the synopsis of the system calls):

pid_t pid = vfork();

As the address spaces are shared between the father and the child when we are running vfork(), the same variable is modified in both the father and the child! But it is set sequentially to 0 in the child process and after the latter either exits or executes a program, the variable is set a second time but with the child's pid in the father process.

NB: The manual says:

vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _exit(2), or abnormally, after delivery of a fatal signal), or it makes a call to execve(2).

Upvotes: 0

Related Questions