I do the regular thing:
- fork()
- execvp(cmd, ) in child
If execvp fails because no cmd is found, how can I notice this error in parent process?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The well-known self-pipe trick can be adapted for this purpose.
Here’s a complete program.
How this works:
Create a pipe, and make the write endpoint
CLOEXEC: it auto-closes when anexecis successfully performed.In the child, try to
exec. If it succeeds, we no longer have control, but the pipe is closed. If it fails, write the failure code to the pipe and exit.In the parent, try to read from the other pipe endpoint. If
readreturns zero, then the pipe was closed and the child must haveexecsuccessfully. Ifreadreturns data, it’s the failure code that our child wrote.