派生后在子进程中设置errno-OSX [英] errno set in child process after fork - OSX

查看:89
本文介绍了派生后在子进程中设置errno-OSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我今天在Mac OSX上发现的一个奇怪的东西.

here's a weird thing I found today on Mac OSX.

成功完成分叉后,将errno设置为0 在父进程中(如预期),但在子进程中设置为22. 这是源代码:

After a fork, which has succeeded, errno is set at 0 in the father's process (as expected), but set at 22 in the child process. Here's the source-code :

#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);
}

执行轨迹:

Remis-Mac:TP3 venant$ ./errno_try
Errno value before the call to fork : 0.
Father : pid value : 9526 ; errno value : 0.
Child : errno value : 22.

据我所知,并根据 Opengroup规范 , 新进程(子进程)应是调用进程(父进程)的精确副本,除非以下详细说明",其中包括全局变量errno -_-

As far as I know, and according to the Opengroup specifications, "The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below [...]", including the value of the global variable errno -_-

有人有线索来解释这种不良行为吗?

Does anyone have a clue to explain that undesired behavior ?

推荐答案

很好奇...我可以在带有GCC 4.8.2和Clang的Mac OS X 10.9 Mavericks上重现该问题.

Curious...I can reproduce the problem on Mac OS X 10.9 Mavericks with GCC 4.8.2 and with Clang.

POSIX说某些失败的功能将设置errno(和 是其中的一个功能),但并未说明成功的功能不会设置errno.例如,在Solaris上,如果输出流不是终端,则许多标准I/O功能将设置errno.但是,在printf()之后重设errno = 0;并不会改变Mac OS X上的行为.

POSIX says that some functions that fail will set errno (and fork() is one of those functions), but does not say that functions that succeed will not set errno. For example, on Solaris, many standard I/O functions set errno if the output stream is not a terminal. However, resetting errno = 0; after the printf() doesn't alter the behaviour on Mac OS X.

某些函数在通过符号errno访问的变量中提供错误号,该符号通过包含<errno.h>标头定义.仅当函数的返回值指示errno的值有效时,才应检查该值. POSIX.1-2008的此卷中的任何功能均不得将errno设置为零.对于进程的每个线程,errno的值均不受其他线程对函数的调用或对errno的分配的影响.

Some functions provide the error number in a variable accessed through the symbol errno, defined by including the <errno.h> header. The value of errno should only be examined when it is indicated to be valid by a function's return value. No function in this volume of POSIX.1-2008 shall set errno to zero. For each thread of a process, the value of errno shall not be affected by function calls or assignments to errno by other threads.

如果fork()失败,则将设置errno指示失败.成功后,检查errno在技术上无效.这说明了原因.

If fork() failed, then errno would be set to indicate the failure. When it succeeds, it is not technically valid to inspect errno. And this is a demonstration of why.

这篇关于派生后在子进程中设置errno-OSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆