return value in vfork() system call

前端 未结 1 1058
时光取名叫无心
时光取名叫无心 2021-01-18 08:00

Considering the below code :

int main()
{
  int pid;
  pid=vfork();
  if(pid==0)
     printf(\"child\\n\");
  else
     printf(\"parent\\n\");
  return 0;
           


        
1条回答
  •  清酒与你
    2021-01-18 08:29

    There aren't 2 copies. When you cal vfork the parent freezes while the child does its thing (until it calls _exit(2) or execve(2)). So at any single moment, there's only a single pid variable.

    As a side note, what you are doing is unsafe. The standard spells it clearly:

    The vfork() function shall be equivalent to fork(), 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(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions.

    As a second side note, vfork has been removed from SUSv4 - there's really no point in using it.

    0 讨论(0)
提交回复
热议问题