I have the following code:
pid_t pid = fork();
if (pid == -1)
{
// ...
}
else if (pid == 0)
{
stdin = someopenfile;
stdout = someotherfile;
stderr = somethirdopenfile;
execvp(args[0], args);
// handle error ...
}
else
{
// ...
}
The problem is, the input/output of the execvp()
call is still the console, rather than the files. Clearly I am doing something wrong, what is the right way to do this?
dup2(2)
is no exception. In particular it can fail witherrno == EMFILE
when reaching the file descriptor limit (which you could lower withulimit
bash builtin orsetrlimit(2)
syscall). Read kernel.org/doc/man-pages/online/pages/man2/dup2.2.html – Varden