等待函数返回值

时间:2016-10-26 14:52:26

标签: function unix process fork wait

我在我试图运行的书中有以下程序:

int i;
void *xpto(void *ptr) {
    printf ("xpto: beggining\n");
    for (; i<5; i++)
        sleep(1);
    printf ("xpto: end i=%d\n", i);
    exit(i);
}

int main(int argc, const char * argv[]){
    int a, b, c;
    i = 0;
    printf ("before fork\n");
    a = fork();
    if (a == 0) {
        printf ("before calling xpto\n");
        xpto(NULL);
        exit (EXIT_SUCCESS);
    }
    else if (a > 0) {
        b = wait(&c);
        printf ("after wait (%d, %d)\n",
                b, WEXITSTATUS(c));
        exit (EXIT_SUCCESS);
    }}

这是输出:

before fork
before calling xpto
xpto: beggining
xpto: end i=5
after wait (-1, 0)
Program ended with exit code: 0

在父进程中,不应该等待(&amp; c)返回子进程ID,而不是返回-1?

0 个答案:

没有答案