这是我今天在Mac OSX上发现的一个奇怪的事情.
在成功的fork之后,在父进程中将errno设置为0(如预期的那样),但在子进程中设置为22.这是源代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int nbArgs, char** args){
int pid;
errno = 0;
printf("Errno value before the call to fork : %d.\n", errno);
if ((pid = fork()) == -1){
perror("Fork failed.");
exit(1);
}
if (pid == 0){
printf("Child : errno value : %d.\n", errno);
}else{
printf("Father : pid value : %d ; errno value : %d.\n", pid, errno);
wait(NULL);
}
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
和执行轨道:
Remis-Mac:TP3 venant$ ./errno_try
Errno value before the call to …Run Code Online (Sandbox Code Playgroud)