void daemonize(void) {
int fd;
// fork退出父进程,让子进程无法成为僵尸进程
if (fork() != 0) exit(0); /* parent exits */
// 开启新会话成为新的会话的领头进程,并与其父进程的会话组和进程组脱离。
// 由于会话对控制终端的独占性,进程同时与控制终端脱离。
setsid(); /* create a new session */
/* Every output goes to /dev/null. If Redis is daemonized but
* the 'logfile' is set to 'stdout' in the configuration file
* it will not log at all. */
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd);
}
}
redis中守护进程的实现
最新推荐文章于 2025-03-14 16:06:45 发布