为什么 id 变量的输出是 1?
Why is the output for the id variable 1?
#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
因为您将其设置为等于 fork() == 0
的结果,这是一个逻辑测试。
Fork 将在分叉线程内部成功(return 零)。外线程将具有 PID。
#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
因为您将其设置为等于 fork() == 0
的结果,这是一个逻辑测试。
Fork 将在分叉线程内部成功(return 零)。外线程将具有 PID。