【发布时间】:2016-07-19 22:08:11
【问题描述】:
我有一个简单的程序,如下所示:-
int main(int argc, const char * argv[])
{
printf ("Before Fork [%d][%d:%s]\n",getpid(),errno,strerror(errno));
pid_t pid = fork();
if (!pid) //CHILD PROCESS
{
printf ("In Child Process [%d [%d:%s]\n",getpid(),errno,strerror(errno));
}
else
{
while(1);
}
}
这会产生输出:-
Before Fork [50083][0:Undefined error: 0]
In Child Process [50084][22:Invalid argument]
有谁知道为什么操作系统在 FORK 之后立即抛出 INVALID ARGUMENT 错误?
【问题讨论】:
-
You're not alone in your observation。选择的答案也适用于您。如果
fork()成功,则errno中没有具体的值需要设置;只有当它失败时,你才能相信它有一个理由。 -
明天可能是 33 岁
-
如有疑问,请务必阅读相关手册页。在这种情况下,errno man page:“它的值只有在调用的返回值指示错误时才有意义”
-
你运行的是什么操作系统?
-
@everyone :- 正如 WhozCraig 和 michi 提到的,由于 fork 有效(不返回 -1),我们不能(而不应该)处理 errno 值。