| Home | Libraries | People | FAQ | More

#### Fork

Boost.Asio supports programs that utilise the `fork()` system call. Provided the program calls `io_context.notify_fork()` at the appropriate times, Boost.Asio will recreate any internal file descriptors (such as the "self-pipe trick" descriptor used for waking up a reactor). The notification is usually performed as follows:

    io_context_.notify_fork(boost::asio::io_context::fork_prepare);
    if (fork() == 0)
    {
      io_context_.notify_fork(boost::asio::io_context::fork_child);
      ...
    }
    else
    {
      io_context_.notify_fork(boost::asio::io_context::fork_parent);
      ...
    }

User-defined services can also be made fork-aware by overriding the `io_context::service::notify_fork()` virtual function.

Note that any file descriptors accessible via Boost.Asio's public API (e.g. the descriptors underlying `basic_socket<>`, `posix::stream_descriptor`, etc.) are not altered during a fork. It is the program's responsibility to manage these as required.

###### See Also

io_context::notify_fork(), io_context::fork_event, io_context::service::notify_fork(), Fork examples.