I've created a rudimentary example of monitoring a child process and restarting it when it fails or exits. What is the preferred/more robust method of doing this? Is it good practice to continuously poll for a change in status? My understanding is that I should utilize something like SIGCHLD
but have been unable to find any good examples.
I'm an embedded C coder mainly and this is my first attempt at trying to understand fork()
.The purpose of this code will eventually be to monitor a call to another program using exec()
and restart this program if and when it fails or finishes.
Edit:
After comments from @cnicutar I have rewritten the example below in a way I think makes more sense in the hope that it is of use to somebody later. I would like the parent to monitor a child process whilst foing other things and make a new call to exec
on a new fork
when the child process fails/finishes. I want to try and use unix signals to achieve this
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[])
{
pid_t cpid;
int status;
cpid = fork();
for (;;)
{
switch (cpid)
{
case -1 : //Fork failure
exit(EXIT_FAILURE);
break;
case 0 : //Child Process
exec(some function);
return 0; //exit child process
break;
default : //Parent process
if (waitpid(-1, &status, WNOHANG) != 1) cpid = fork(); //restart
//Do parent stuff here...
break;
}
}
}
Adding a
SIGCHLD
handler won't buy you much since you already wait all the time and only do that - there's nothing else to "interrupt".One thing I would suggest is a threshold such that the process doesn't die/start too often.
You use
SIGCHLD
to know when to wait. A typicalSIGCHLD
handler just doeswaitpid
in a loop until no children are left. In your case you don't need that since your main code is a loop stopped onwaitpid
.EDIT
You can find plenty of examples for
SIGCHLD
handling. One such example is How can I handle sigchld in C. In that code, after thewhile
you can justfork
:To reiterate, if you do this
SIGCHLD
will be called every time a child dies and after you properly wait for it you can justfork
another. This only makes sense if the parent has better stuff to do in the meantime than to just block onwaitpid
.Word to the wise. There are certain functions that must not be called from a signal handler lest you add difficult bugs to your program. Look up "async signal safe" functions: these and only these can be called from a signal handler. Some of the most common functions (like
printf
andmalloc
) cannot be called from a signal handler.