小编Rem*_*ant的帖子

errno在fork之后设置在子进程中 - OSX

这是我今天在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)

c unix macos fork errno

3
推荐指数
1
解决办法
2521
查看次数

标签 统计

c ×1

errno ×1

fork ×1

macos ×1

unix ×1