叉子究竟返回了什么?

com*_*ler 21 c linux fork

成功时,子进程的PID在父进程的执行中返回,并在子进程的执行中返回0.

p = fork();
Run Code Online (Sandbox Code Playgroud)

我对它的手册页感到困惑,p等于0PID

Oli*_*rth 51

我不确定手册如何更清晰! fork()创建一个新流程,因此您现在有两个相同的流程.为了区分它们,返回值fork()不同.在原始过程中,您将获得子进程的PID.在子进程中,您得到0.

所以规范使用如下:

p = fork();
if (0 == p)
{
    // We're the child process
}
else if (p > 0)
{
    // We're the parent process
}
else
{
    // We're the parent process, but child couldn't be created
}
Run Code Online (Sandbox Code Playgroud)

  • 如果没有创建进程,这意味着`fork`*失败*,所以它返回`-1`,而不是'0`,并适当地设置`errno`.它在原始进程中执行此操作(不是父进程,因为没有创建进程). (3认同)
  • @compiler:不,两个进程(旧进程和新创建的进程)在**调用`fork`之后继续执行**. (2认同)

pmg*_*pmg 29

                             p = fork();
                        /* assume no errors */
                        /* you now have two */
                        /* programs running */
                         --------------------
      if (p > 0) {                |            if (p == 0) {
        printf("parent\n");       |              printf("child\n");
        ...                       |              ...


Nou*_*him 8

fork执行一次后,您有两个进程.该调用为每个进程返回不同的值.

如果你做这样的事情

int f;
f = fork();
if (f == 0) {
  printf("I am the child\n");
} else {
  printf("I am the parent and the childs pid is %d\n",f);

}
Run Code Online (Sandbox Code Playgroud)

您将看到两条消息都已打印出来.它们由两个独立的过程打印.这是他们可以区分创建的两个进程的方式.

  • 不,这意味着您正处于子女流程中."不创建流程"意味着只有一个流程.如果返回"-1",则表示没有创建子项.手册页显示了这一点. (5认同)

lev*_*vif 6

进程在有向树中构建,您只知道您的单父(getppid()).简而言之,像许多其他系统函数一样fork()返回-1错误,非零值对于fork调用的启动程序(父级)来说非常有用,可以知道它的新子pid.

没有什么比例子好:

/* fork/getpid test */
#include <sys/types.h>
#include <unistd.h>     /* fork(), getpid() */
#include <stdio.h>

int main(int argc, char* argv[])
{
    int pid;

    printf("Entry point: my pid is %d, parent pid is %d\n",
           getpid(), getppid());

    pid = fork();
    if (pid == 0) {
        printf("Child: my pid is %d, parent pid is %d\n",
               getpid(), getppid());
    }
    else if (pid > 0) {
        printf("Parent: my pid is %d, parent pid is %d, my child pid is %d\n",
               getpid(), getppid(), pid);
    }
    else {
        printf("Parent: oops! can not create a child (my pid is %d)\n",
               getpid());
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果(bash是pid 2249,在这种情况下):

Entry point: my pid is 16051, parent pid is 2249
Parent: my pid is 16051, parent pid is 2249, my child pid is 16052
Child: my pid is 16052, parent pid is 16051
Run Code Online (Sandbox Code Playgroud)

如果您需要在父级和子级之间共享一些资源(文件,父级pid等),请查看clone()(对于GNU C库,以及其他人)


Chr*_*per 5

这是很酷的部分。它等于两者。

嗯,不是真的。但是一旦fork返回,您现在就有两个正在运行的程序副本!两个过程。您可以将它们视为备用宇宙。其一,返回值是0。另一方面,它ID是新进程的!

通常你会有这样的事情:

p = fork();
if (p == 0){
    printf("I am a child process!\n");
    //Do child things
}
else {
    printf("I am the parent process! Child is number %d\n", p);
    //Do parenty things
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,两个字符串都将被打印,但通过不同的过程!


Bol*_*ock 2

fork()在父进程中调用。然后生成一个子进程。当子进程产生时,fork()它已经完成执行。

此时,fork()已准备好返回,但它返回不同的值,具体取决于它是在父级还是子级中。在子进程中,它返回0,在父进程/线程中,它返回子进程ID。