简体   繁体   English

父进程中的Fork()

[英]Fork() in parent process

What happens if I were to call fork() in a parent process? 如果我在父进程中调用fork()会发生什么?

A general example: 一般例子:

int ret;
int fd[2];

ret = pipe(fd);
pid = fork();

if (pid == -1){
    perror("fork failed");
    exit(1);
}

else if (pid > 0){    //PARENT 
    //writes or reads
    fork();

} }

Does this mean that fork() will create a new process of the parent? 这是否意味着fork()将创建父进程的新进程?

I'm pretty new to this, so help would be much appreciated. 我对此很陌生,所以非常感谢帮助。 Thanks. 谢谢。

What happens if I were to call fork() in a parent process? 如果我在父进程中调用fork()会发生什么?

Every process in which you call fork() can be considered a parent process. 您调用fork() 每个进程都可以被视为父进程。

From the Linux man page fork(2) : 从Linux手册页fork(2)

   fork() creates a new process by duplicating the calling process.  The
   new process, referred to as the child, is an exact duplicate of the
   calling process, referred to as the parent, except for the following
   points:

   *  The child has its own unique process ID, and this PID does not
      match the ID of any existing process group (setpgid(2)).

   *  The child's parent process ID is the same as the parent's process
      ID.
   ...

The result of fork() is two identical copies of the original process running. fork()的结果是原始进程运行的两个相同副本。 The one who originally called fork() is the parent , and the resulting process is the child . 最初调用fork()父级 ,结果进程是子级 The child's PPID (parent process ID) is equal to the parent's PID. 子的PPID(父进程ID)等于父进程的PID。

original process
    |
    |    fork() called
    |\
    | \
    |  \
    |   \
parent  child

Every time you call fork , it "returns twice" -- once in the parent, once in the child. 每次调用fork ,它都会“返回两次” - 一次在父级中,一次在子级中。 The terms "parent" and "child" in that description are largely for convenience of disambiguation. 该描述中的术语“父母”和“儿童”主要是为了便于消除歧义。 (Though there are some differences when you start getting into discussions about waitpid .) They are both "perfectly good processes" which can go on to fork other processes, or exec , or just both continue on their merry way doing something totally different. (虽然当你开始讨论waitpid时会有一些差异。)它们都是“非常好的过程”,可以继续fork其他进程,或者exec ,或者只是继续他们快乐的方式做一些完全不同的事情。

In your example, let's suppose we start with a process with pid 1. Here's my attempt at a diagram of what happens, since tables aren't supported :\\ 在你的例子中,让我们假设我们从一个带有pid 1的进程开始。这是我尝试发生的事情的图表,因为不支持表:

  • We fork the first time. 我们第一次分叉。 Process 2 is created. 创建过程2。
    • In process 1, fork returns 2. 在进程1中, fork返回2。
    • We continue on to the else if , which is true. else if这是真的,我们继续对其else if We call fork again. 我们再次调用fork Process 3 is created. 创建过程3。
      • In process 1, fork returns 3. 在进程1中, fork返回3。
      • Process 1 continues off the end of your code snippet. 流程1继续在代码段的末尾。
      • In process 3, fork return 0. 在进程3中, fork返回0。
      • Process 3 continues off the end of your code snippet. 流程3继续在代码段的末尾。
    • In process 2, fork returns 0. 在进程2中, fork返回0。
    • Process 2 continues on to the else if , which is false. 进程2继续到else if ,这是错误的。
    • Process 2 continues off the end of your code snippet. 过程2继续在代码段的末尾。

So you end up with 3 processes; 所以你最终得到3个过程; process 1 is the parent of both 2 and 3. Any of those three could go on to fork again, creating more children, etc etc. 进程1是2和3的父进程。这三者中的任何一个都可以再次进行fork ,创建更多子进程等。

Does this mean that fork() will create a new process of the parent? 这是否意味着fork()将创建父进程的新进程?

fork() creates a new process whose memory is a duplicate (although not shared) of the one it was called from. fork()创建一个新进程,其内存与调用它的内存重复(尽管不是共享)。

Keep in mind that processes form a tree, where each successive process is the child of the one it was forked from (the parent ). 请记住,进程形成一个树,其中每个连续的进程是它所分叉的进程( 进程)的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM