What to do if exec() fails?
Posted
by
Grigory
on Stack Overflow
See other posts from Stack Overflow
or by Grigory
Published on 2012-06-26T21:00:06Z
Indexed on
2012/06/26
21:15 UTC
Read the original article
Hit count: 194
Let's suppose we have a code doing something like this:
int pipes[2];
pipe(pipes);
pid_t p = fork();
if(0 == p)
{
dup2(pipes[1], STDOUT_FILENO);
execv("/path/to/my/program", NULL);
...
}
else
{
//... parent process stuff
}
As you can see, it's creating a pipe, forking and using the pipe to read the child's output (I can't use popen
here, because I also need the PID of the child process for other purposes).
Question is, what should happen if in the above code, execv
fails? Should I call exit() or abort()? As far as I know, those functions close the open file descriptors. Since fork
-ed process inherits the parent's file descriptors, does it mean that the file descriptors used by the parent process will become unusable?
© Stack Overflow or respective owner