a flag of libev about ev_default_fork

458 views Asked by At

I'm learning libev. But I don't understand about the ev_default_fork flag of ev_loop. Is this a question of close-on-exec? Like the FD_CLOEXEC fcntl() flag do? when I need to set the flag? Which case this flag is necessary? This a description of doc:

This function sets a flag that causes subsequent ev_loop iterationsto reinitialise the kernel state for backends that have one. Despite thename, you can call it anytime, but it makes most sense after forking, inthe child process (or both child and parent, but that again makes littlesense). You must call it in the child before using any of the libevfunctions, and it will only take effect at the next ev_loop iteration.

On the other hand, you only need to call this function in the childprocess if and only if you want to use the event library in the child. Ifyou just fork+exec, you don't have to call it at all.

The function itself is quite fast and it's usually not a problem to callit just in case after a fork. To make this easy, the function will fit inquite nicely into a call to pthread_atfork: pthread_atfork (0, 0, ev_default_fork);

1

There are 1 answers

5
hroptatyr On

Well just read the documentation, you call the function in the child after fork(), preferrably the first thing you do. So your code should look like:

switch (fork()) {
case -1:
    /* everything's gone pear-shaped */

default:
    /* i am the parent */
    ...
    break;

case 0:
    /* i am the child */
    ev_loop_fork(EV_DEFAULT);
    ...
    break;
}