本文整理汇总了C++中boost::asio::io_service::notify_fork方法的典型用法代码示例。如果您正苦于以下问题:C++ io_service::notify_fork方法的具体用法?C++ io_service::notify_fork怎么用?C++ io_service::notify_fork使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::asio::io_service
的用法示例。
在下文中一共展示了io_service::notify_fork方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: daemonize
bool daemonize( ba::io_service& service )
{
try {
// Inform the io_service that we are about to become a daemon.
service.notify_fork(ba::io_service::fork_prepare);
if( pid_t pid = fork() ) {
if (pid > 0) {
service.notify_fork(ba::io_service::fork_parent);
exit(0);
} else {
sz_log(0, "Daemonize: First fork failed: %s", strerror(errno));
return false;
}
}
setsid();
(void)chdir("/");
umask(0);
service.notify_fork(ba::io_service::fork_prepare);
if( pid_t pid = fork() ) {
if (pid > 0) {
service.notify_fork(ba::io_service::fork_parent);
exit(0);
} else {
sz_log(0, "Daemonize: Second fork failed: %s", strerror(errno));
return false;
}
}
close(0);
close(1);
close(2);
if (open("/dev/null", O_RDONLY) < 0) {
sz_log(0, "Daemonize: Unable to open /dev/null: %s", strerror(errno));
return false;
}
// Inform the io_service that we have finished becoming a daemon.
service.notify_fork(boost::asio::io_service::fork_child);
} catch (std::exception& e) {
sz_log(0, "Daemonize: Exception: %s", e.what());
return false;
}
return true;
}