父进程退出,子进程继续运行(父进程若不退出则在其结束时会在终端提示)
子进程使用setsid()创建新会话(使用子进程创建新会话为了避免新的会话的进程组号和会话号与原进程组号、会话号重复发生冲突)
使用umask()设置用户创建文件的默认权限
使用chdir()改变工作路径(通常设置在根目录下)(由于守护进程是一个长时间的进程,若不改变工作路径,则当前工作目录不方便卸载)
关闭所有文件描述符,并重定向至"/dev/null",向该文件写入的数据都会被丢弃(使用dup2()来进行)(这步是为了使得新的会话脱离终端,不再受标准输入、标准输出、标准错误的影响)
实例:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void work(){
time_t tm = time(NULL);
struct tm * loc = localtime(&tm);
char * str = asctime(loc);
int fd = open("time.txt", O_RDWR | O_CREAT | O_APPEND, 0664);
write(fd ,str, strlen(str));
close(fd);
}
int main(){
//父进程退出,子进程继续运行
pid_t pid = fork();
if(pid >0){
exit(0);
}
//使用子进程创建一个新会话
pid_t sid = setsid();
//设置掩码
umask(022);
//更改工作目录(该处设置为用户目录下是因为业务逻辑中有创建文件的操作,若设置在根目录下没有权限))
chdir("/home/nowcoder/");
//关闭重定向文件描述符
int fd = open("/dev/null", O_RDWR);
dup2(fd,STDIN_FILENO);
dup2(fd,STDOUT_FILENO);
dup2(fd,STDERR_FILENO);
//业务逻辑代码
struct sigaction act;
act.sa_handler = work;
act.sa_flags =0;
sigemptyset(&act.sa_mask);
sigaction(SIGALRM,&act,NULL);
struct itimerval val;
val.it_value.tv_sec=2;
val.it_value.tv_usec=0;
val.it_interval.tv_sec=2;
val.it_interval.tv_usec=0;
setitimer(ITIMER_REAL,&val,NULL);
while(1){
sleep(10);
}
return 0;
}