Click to See Complete Forum and Search --> : Unix programming: errno and fork


mingshun
10-23-2002, 04:44 PM
Hi,

I need some help on this errno in fork thing. Currently, I have something like this:


extern int errno;
...
int ec, pid[2];

if (pipe (pid) < 0) {
perror ("pipe fails");
return -1;
}

if (fork ()) {
/* parent: all fds are closed before wait */
...

wait (&ec);

switch (errno) {
case: EACCES: case ENOENT: .....
printf ("Exit status of child is %d\n", errno);
break;

default:
printf ("Exit status of child is %d\n", (int) (ec>>8 &0xff));
}
}

else {
/* child: */
println ("line 65: %d\n", errno);
}


When running a.out, the errno in line 65 is set to 2. The question here is how can the child tells the parent about this? Because both has a different address space, both values of errno will be different. I have thought of sending this errno through the pipe but the pid [1] was already occupied by another thing.

Can any kind soul gives some advise? I think I'm very close to this already and I just need a bit more of hints only.

Thanks