为什么id变量1的输出?

时间:2015-02-10 22:30:07

标签: fork

#include <stdio.h>
#include <unistd.h>

int main()
{
    int id;
    printf("here comes the date.\n");
    if (id = fork() == 0) {
        printf(“%d”, id);
        printf ("PID is %d and ID  is %d\n", getpid (),id);
        execl ("/bin/date", "date", 0);
    }
    printf ("that was the date.\n");
}

输出:

here comes the date.
that was the date.
PID is 1414 and ID  is 1
Tue Feb 10 14:03:02 PST 2015

1 个答案:

答案 0 :(得分:0)

因为您将其设置为fork() == 0的结果,这是一个逻辑测试。

Fork将在分叉线程内成功(返回零)。外部线程将具有PID。