Standard approach for determining success / failure of fork / exec (while the parent is running at the same time)?

I made a program using fork() and exec*() . The problem is that I cannot determine the success or failure of exec() from the parent process, because it is on a separate child process. I think that you can use an alarm to check this state, but I have no idea about it.

  • What is the recommended / standard / widely used way to test this?
  • And what are the pitfalls that I have to take care of when doing this?

Update request details (sorry for the lack of important information)

I want both processes to work, so I can't just wait for the child process to exit. In other words, I want to receive a notification about the successful completion of the exec child process.

+7
source share
1 answer

Your parent process can use the pid of the child process to detect that it is alive or exited (and can eliminate the error code, as well as errors with the error with the dead, see waitpid). You can use certain error codes or signals to notify the parent of specific cases of errors (for example, in a forked child before exec), but for a completely general child you cannot reserve any exit codes or signals (because the parent cannot determine if it has completed whether exec, and then the child exited with these values).

Another approach that is often used is to create a fd channel pair (see the "syscall" pipe) and pass one end to the child (usually the end of the record) and the other to the parent. A child can use this to send specific error codes to parents. And the parent can detect premature termination if the pipe is closed without receiving any data. There are some pitfalls: SIGPIPE will be sent to parents if it reads on a pipe without active authors, and using fd (except for stdin / stdout / stderr) in a child process can confuse some poorly written child processes (although close-on-exec can help fix this is).

In general, all the code I've seen to make fork + exec reliable is pretty hacky.

+7
source

All Articles