here's a weird thing I found today on Mac OSX.
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);
}
And the execution track :
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.
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 ?
Curious...I can reproduce the problem on Mac OS X 10.9 Mavericks with GCC 4.8.2 and with Clang.
POSIX says that some functions that fail will set
errno
(andfork()
is one of those functions), but does not say that functions that succeed will not seterrno
. For example, on Solaris, many standard I/O functions seterrno
if the output stream is not a terminal. However, resettingerrno = 0;
after theprintf()
doesn't alter the behaviour on Mac OS X.POSIX 2008 (System Interfaces — General Information: 3. Error numbers):
If
fork()
failed, thenerrno
would be set to indicate the failure. When it succeeds, it is not technically valid to inspecterrno
. And this is a demonstration of why.