… | |
… | |
1454 | |
1454 | |
1455 | |
1455 | |
1456 | =head2 C<ev_child> - watch out for process status changes |
1456 | =head2 C<ev_child> - watch out for process status changes |
1457 | |
1457 | |
1458 | Child watchers trigger when your process receives a SIGCHLD in response to |
1458 | Child watchers trigger when your process receives a SIGCHLD in response to |
1459 | some child status changes (most typically when a child of yours dies). |
1459 | some child status changes (most typically when a child of yours dies). It |
|
|
1460 | is permissible to install a child watcher I<after> the child has been |
|
|
1461 | forked (which implies it might have already exited), as long as the event |
|
|
1462 | loop isn't entered (or is continued from a watcher). |
|
|
1463 | |
|
|
1464 | Only the default event loop is capable of handling signals, and therefore |
|
|
1465 | you can only rgeister child watchers in the default event loop. |
|
|
1466 | |
|
|
1467 | =head3 Process Interaction |
|
|
1468 | |
|
|
1469 | Libev grabs C<SIGCHLD> as soon as the default event loop is |
|
|
1470 | initialised. This is necessary to guarantee proper behaviour even if |
|
|
1471 | the first child watcher is started after the child exits. The occurance |
|
|
1472 | of C<SIGCHLD> is recorded asynchronously, but child reaping is done |
|
|
1473 | synchronously as part of the event loop processing. Libev always reaps all |
|
|
1474 | children, even ones not watched. |
|
|
1475 | |
|
|
1476 | =head3 Overriding the Built-In Processing |
|
|
1477 | |
|
|
1478 | Libev offers no special support for overriding the built-in child |
|
|
1479 | processing, but if your application collides with libev's default child |
|
|
1480 | handler, you can override it easily by installing your own handler for |
|
|
1481 | C<SIGCHLD> after initialising the default loop, and making sure the |
|
|
1482 | default loop never gets destroyed. You are encouraged, however, to use an |
|
|
1483 | event-based approach to child reaping and thus use libev's support for |
|
|
1484 | that, so other libev users can use C<ev_child> watchers freely. |
1460 | |
1485 | |
1461 | =head3 Watcher-Specific Functions and Data Members |
1486 | =head3 Watcher-Specific Functions and Data Members |
1462 | |
1487 | |
1463 | =over 4 |
1488 | =over 4 |
1464 | |
1489 | |
… | |
… | |
1487 | |
1512 | |
1488 | The process exit/trace status caused by C<rpid> (see your systems |
1513 | The process exit/trace status caused by C<rpid> (see your systems |
1489 | C<waitpid> and C<sys/wait.h> documentation for details). |
1514 | C<waitpid> and C<sys/wait.h> documentation for details). |
1490 | |
1515 | |
1491 | =back |
1516 | =back |
|
|
1517 | |
|
|
1518 | =head3 Examples |
|
|
1519 | |
|
|
1520 | Example: C<fork()> a new process and install a child handler to wait for |
|
|
1521 | its completion. |
|
|
1522 | |
|
|
1523 | ev_child cw; |
|
|
1524 | |
|
|
1525 | static void |
|
|
1526 | child_cb (EV_P_ struct ev_child *w, int revents) |
|
|
1527 | { |
|
|
1528 | ev_child_stop (EV_A_ w); |
|
|
1529 | printf ("process %d exited with status %x\n", w->rpid, w->rstatus); |
|
|
1530 | } |
|
|
1531 | |
|
|
1532 | pid_t pid = fork (); |
|
|
1533 | |
|
|
1534 | if (pid < 0) |
|
|
1535 | // error |
|
|
1536 | else if (pid == 0) |
|
|
1537 | { |
|
|
1538 | // the forked child executes here |
|
|
1539 | exit (1); |
|
|
1540 | } |
|
|
1541 | else |
|
|
1542 | { |
|
|
1543 | ev_child_init (&cw, child_cb, pid, 0); |
|
|
1544 | ev_child_start (EV_DEFAULT_ &cw); |
|
|
1545 | } |
1492 | |
1546 | |
1493 | |
1547 | |
1494 | =head2 C<ev_stat> - did the file attributes just change? |
1548 | =head2 C<ev_stat> - did the file attributes just change? |
1495 | |
1549 | |
1496 | This watches a filesystem path for attribute changes. That is, it calls |
1550 | This watches a filesystem path for attribute changes. That is, it calls |