I have an assignment in which I have to create a mini shell which is capable of doing a lot of things including job control. I managed to create new jobs using fork and execvp. But I also want to get the exit codes of the programs run by execvp. From what I have looked up from other posts I can do this by using if(WIFEXITED(status))
.
But we are also required the run the process in the background if user enters '&' with the process name in the shell. So it doesn't make sense to wait for the parent process to wait while the child program finishes in background. Is there any interrupt that I can set up which notifies the parent process that the child process has ended? Then I can use the if(WIFEXITED(status))
statement to get the exit code.
Example
sleep 100 &
will run in the background while I can execute other shell commands in the foreground simultaneously.
If you don't do anything special the parent receives a
SIGCHLD
when the child dies. Careful though, multiple children can terminate at the same time and you'll get a single signal. The correct way to handle this is to loop (in the signal handler):