[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] how to handle SIGCHLD before going into dispatch



I have a piece of code that does a fork, and the parent goes into the
event_dispatch loop while the child does something. The child will
exit at some point of time so I will need to attach a SIGCHLD handler
into the event_base (I assume that I shouldn't be calling
event_base_loopexit from a standard unix signal handler).

There's a problem when the child exits too fast, right after I do the
evsignal_new, but before going into the dispatch loop. To simulate
this, I have created a test program as follow, the child basically
just does nothing and exits (similiar with what my program does
sometimes), and the parent does a couple of things after forking,
before calling evsignal_new, and then do some other things again
(adding more handlers), to simulate this, I have added a sleep 1 into
the code.

In Linux, this piece of code seems to work properly, but in Solaris 10
Intel (libevent 2.0.6rc), it gives me a warning:

evsig_handler: received signal 18, but have no base configured

The effect is that the sigchld is not processed, and the loop is not exited.

How do I handle this case in Solaris 10?

==== start code ====
#include <signal.h>
#include "event.h"

static struct event_base *base = NULL;
static struct event *signal_event_chld;

static void child_signal_cb(evutil_socket_t fd, short event, void *arg)
{
        struct timeval tv;

        fprintf(stderr, "got sigchild\n");

        tv.tv_usec = 500000;
        tv.tv_sec = 0;
        event_base_loopexit(base, &tv);
}

void do_parent()
{
        base = event_base_new();
        if (base==NULL) {
                perror("event_base");
        }

        signal_event_chld = evsignal_new(base, SIGCHLD, child_signal_cb, NULL);
        evsignal_add(signal_event_chld, NULL);

        sleep(1);

        event_base_dispatch(base);
        printf("event_base_dispatch done\n");
}

int main()
{
        pid_t pid;

        pid = fork();
        switch (pid) {
        case -1:
                perror("fork");
                return -1;
                break;
        case 0:
                break;
        default:
                fprintf(stderr, "forked child:%ld\n", (long)pid);
                do_parent();
                break;
        }
        return 0;
}

==== end code ====
-- 
Cheers,
Phuah Yee Keat
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.