fork()返回0,但是子进程getpid()!= 0.为什么? [英] fork() returns 0, but the child process getpid()!=0. Why?

查看:376
本文介绍了fork()返回0,但是子进程getpid()!= 0.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是测试fork()系统调用的C代码:

This is the C code to test fork() system call:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>

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

    printf("I am: %d\n", (int)getpid());

    pid_t pid = fork();

    printf("fork returned: %d\n", (int)pid);

    if (pid < 0)
    {
        perror("fork failed");  
    }

    if (pid==0)
    {
        printf("I am the child with pid %d\n", (int)getpid());
        sleep(5);
        printf("Child exiting...\n");
        exit(0);
    }

    printf("I am the parent with pid %d, waiting for the child\n", (int)getpid());
    wait(NULL);
    printf("Parent ending. \n");

    return 0;
}

终端的输出是:

I am: 25110
fork returned: 25111
I am the parent with pid 25110, waiting for the child
fork returned: 0
I am the child with pid 25111
Child exiting...
Parent ending. 

问题:叉返回:0 时,它在子进程中.但是子进程显示我是pid为25111的孩子.我以为子进程的pid应该为0,为什么更改为25111?

Question: When fork returned: 0, it is in the child process. But the child process shows I am the child with pid 25111. I thought the child process pid should be 0, why it changes to 25111?

与父进程相同, fork返回25111 ,但是getpid()返回25110(我是pid为25110的父,正在等待子进程)

Same thing with the parent process, fork returned 25111, but getpid() returns 25110 (I am the parent with pid 25110, waiting for the child)

推荐答案

子进程显示我是pid为25111的孩子.我认为子进程pid应该为0

the child process shows I am the child with pid 25111. I thought the child process pid should be 0

不.在父级fork()中返回子级的pid.在子级中,fork返回0-这不是任何东西的pid,它只是一个标记. getpid告诉您,孩子的pid是25111.

No. In the parent fork() returns the pid of the child. In the child, fork returns 0 -- which is not the pid of anything, it's just a marker. The child pid is 25111, as getpid told you.

在父进程中,fork返回25111,但是getpid()返回25110

with the parent process, fork returned 25111, but getpid() returns 25110

对.父pid始终为25110,如getpid告诉您的那样.然后fork()返回新孩子的pid.

Right. The parent pid was always 25110, as getpid told you. And fork() returned the pid of the new child.

fork在父级中返回的值与在子级中返回getpid的值相同的事实证明这一切都正常进行.

The fact that fork returned the same value in the parent as getpid returned in the child proves that this is all working properly.

听起来您好像认为fork()总是返回您所在进程的pid.但这没有任何意义-我们已经有了getpid调用.

It sounds like you think fork() always returns the pid of the process that you're in. But that wouldn't make sense -- we already have the getpid call for that.

如果您是父级,则fork()返回 other 进程的pid,即子进程.如果您是孩子,fork()根本不会返回pid. (如果您是孩子,并且想知道父母的pid,那是一个很好的,频繁的和单独的问题.答案:请致电getppid().)

If you're the parent, fork() returns the pid of the other process, the child. And if you're the child, fork() doesn't return a pid at all. (If you're the child and you want to know the pid of your parent, that's a good, frequent, and separate question. Answer: call getppid().)

我们可以将所有这些总结如下:

We can summarize all this as follows:

                 parent   child
                 ------   -----
pid:              25110   25111
fork returns:     25111       0
getpid returns:   25110   25111
getppid returns:  ?????   25110

记住这一点的方法是考虑将要调用fork()的代码:它将要做什么,它需要知道什么.父母需要知道它是父母.孩子需要知道它是孩子.父母经常需要知道孩子的pid(并且没有其他方法可以获取它).

The way to remember this is to think about the code that's going to be calling fork(): what it's going to do, what it needs to know. The parent needs to know that it is the parent. The child needs to know that it is the child. The parent very often needs to know the pid of the child (and would have no other way of obtaining it).

如果fork始终返回pid,则在fork调用之后,查看其返回值,您将无法知道自己是父母还是孩子-但这通常是第一个和第二个您需要知道的最重要的事情.

If fork always returned a pid, then after the fork call, looking at its return value, you would have no way of knowing whether you were the parent or the child -- but this is typically the first and most important thing you need to know.

(在所有这些中,我忽略了第三种可能性,即fork失败,并在父级中返回-1,而在子级中不返回任何东西,因为没有一个.)

(In all of this I've ignored the third possibility, namely that fork fails, and returns -1 in the parent, and doesn't return anything in the child, because there isn't one.)

另请参见为什么fork()返回0子进程?.

这篇关于fork()返回0,但是子进程getpid()!= 0.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆