[OPEN-ILS-DEV] C nits: osrf_system.c
Scott McKellar
mck9 at swbell.net
Sun May 13 13:09:37 EDT 2007
In the parent post I proposed a wait loop to replace the current
use of a signal handler combined with a sleep loop:
--- Scott McKellar <mck9 at swbell.net> wrote:
>
> daemonize();
> while(1) {
> int status;
> pid_t pid;
>
> pid = wait( &status );
> osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
> }
After playing around a bit and doing some research I have a new
improved proposal:
daemonize();
while(1) {
errno = 0;
pid_t pid = wait( NULL );
if( -1 == pid ) {
if( errno != ECHILD )
osrfLogWarning( OSRF_LOG_MARK, "Oops!" );
break;
} else
osrfLogWarning( OSRF_LOG_MARK, "We lost child %d", pid);
}
Remarks:
1. Since we don't examine the status code, there's no point in
capturing it. So I pass NULL to wait() instead of an pointer to an
int.
2. In case of an error, wait() returns -1, so I test for that. Then
I check errno to see what kind of error it is.
3. If errno == ECHILD, that means that there aren't any child
processes left, so I exit the loop. Otherwise I would go into an
endless loop, calling wait() repeatedly and getting the same error
every time, which would just be silly.
I don't know what you want to do once all the children have died.
In the present code the sleep loop just keeps going forever. I
suspect that the intent is to restart any child that has died, but
that part hasn't been written yet.
4. If I encounter an error other than ECHILD, I issue a separate
error message before exiting the loop. Yeah, the message in the
sample code above needs some work. Probably the best thing to do
is to call strerror() to get a textual description of the error,
and incorporate that into the message.
5. I haven't found this documented anywhere, but from my experiments
it appears that wait() is not interrupted by signals, except of
course for signals that terminate the process. So there's no need
to code anything special to distinguish signals from other kinds
of interruptions.
Scott McKellar
http://home.swbell.net/mck9/aargh/
More information about the Open-ils-dev
mailing list