0

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);

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
simon
  • 1

1 Answers1

2

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;
}
hroptatyr
  • 4,702
  • 1
  • 35
  • 38
  • why do this?If i didn't write ev_default_fork(),what happen? – simon Aug 01 '12 at 13:02
  • this ev_default_fork() work like the FD_CLOEXEC fcntl() flag do? – simon Aug 01 '12 at 13:04
  • No, it's got nothing to do with FD_CLOEXEC. `ev_default_fork()` just does some internal tricks, so the default **loop** can be used in the child. By that I mean that all events registered with the parent will continue to work with the child. If you don't do it, then the default loop has unusable events in its queue. – hroptatyr Aug 01 '12 at 13:08
  • The major version 4 introduced some incompatible changes to the API.ev_default_destroy and ev_default_fork have been removed!!!! – simon Aug 01 '12 at 15:17
  • no you read it wrong: it's been replaced by `ev_loop_fork(EV_DEFAULT);`, i'll update my example – hroptatyr Aug 01 '12 at 15:29