Search code examples
clinuxforksystemvfork

File descriptor table for vfork vs. fork


I am a newbie in System Programming and I encounter some misunderstanding in fork and vfork.

  • From my knowledge, fork duplicate parent process's process and child process has its own VM and own file descriptor table.
  • As for vfork, it shared parent process's VM but child process will has its own file descriptor table.

So here comes the problem:

  1. As child process share parent process' address, why does it need its own file descriptor table?
  2. Where will the variable be stored if I declare one in the child process? (Will it use the space of parent process) Thanks a lot.

Solution

    1. Because it's common to redirect stdin and/or stdout before calling exec in the child process. If they shared the same file descriptor table, this would modify the parent process's I/O.

    2. You shouldn't store any variables in the child process. vfork() should only be used if you're going to immediately call an exec function.

    Note that vfork() is obsolete on modern operating systems. Instead of copying the address space they use copy-on-write.

    For more information see What is the difference between fork() and vfork()?