int daemonise ()
{
int nullfd;
pid_t pid;
if ((nullfd = open ("/dev/null", O_WRONLY, 0)) == -1)
{
return -1;
}
if ((pid = fork ()) < 0)
{
return -1;
}
else if (pid != 0)
{
close (nullfd);
exit (0); // parent goes bye-bye
}
// child continues
dup2 (nullfd, STDIN_FILENO); // stdin
dup2 (nullfd, STDOUT_FILENO); // stdout
dup2 (nullfd, STDERR_FILENO); // stderr
close (nullfd);
setsid (); // become session leader
chdir ("/"); // change working directory
umask (0); // clear our file mode creation mask
return 0;
}
创建守护进程的简单示例
最新推荐文章于 2020-11-11 16:52:37 发布