Daemon(守护进程)是运行在后台的一种特殊进程。它于控制终端并且周期性地执行某种任务或等待处理某些发生的事件。它不需要用户输入就能运行而且提供某种服务,不是对整个系统就是对某个用户程序提供服务。Linux系统的大多数服务器就是通过守护进程实现的。常见的守护进程包括系统日志进程syslogd、 web服务器httpd、邮件服务器sendmail和数据库服务器mysqld等。
守护进程一般在系统启动时开始运行,除非强行终止,否则直到系统关机都保持运行。守护进程经常以超级用户(root)权限运行,因为它们要使用特殊的端口(1-1024)或访问某些特殊的资源。
守护进程的父进程是init进程,因为它真正的父进程在fork出子进程后就先于子进程exit退出了,所以它是一个由init继承的孤儿进程。守护进程是非交互式程序,没有控制终端,所以任何输出,无论是向标准输出设备stdout还是标准出错设备stderr的输出都需要特殊处理。
守护进程的名称通常以d结尾,比如sshd、xinetd、crond等。
首先我们要了解一些基本概念:
进程组 :
会话:
会话(session)是一个或多个进程组的集合。setsid()函数可以建立一个新会话:
如果,调用setsid的进程不是一个进程组的组长,此函数创建一个新的会话。
编写守护进程的一般步骤步骤:
4、5、6步骤是修改继承来自父类的资源,顺序无所谓。
一张简单的图可以完美诠释之前几个步骤:
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
static bool flag = true;
void create_daemon();
void handler(int);
int main()
{
time_t t;
int fd;
create_daemon();
struct sigaction act;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if(sigaction(SIGQUIT, &act, NULL))
{
printf("sigaction error.\n");
exit(0);
}
while(flag)
{
fd = open("/home/mick/daemon.log", O_WRONLY | O_CREAT | O_APPEND, 04);
if(fd == -1)
{
printf("open error\n");
}
t = time(0);
char *buf = asctime(localtime(&t));
write(fd, buf, strlen(buf));
close(fd);
sleep(60);
}
return 0;
}
void handler(int sig)
{
printf("I got a signal %d\nI'm quitting.\n", sig);
flag = false;
}
void create_daemon()
{
pid_t pid;
pid = fork();
if(pid == -1)
{
printf("fork error\n");
exit(1);
}
else if(pid)
{
exit(0);
}
if(-1 == setsid())
{
printf("setsid error\n");
exit(1);
}
pid = fork();
if(pid == -1)
{
printf("fork error\n");
exit(1);
}
else if(pid)
{
exit(0);
}
chdir("/");
int i;
for(i = 0; i < 3; ++i)
{
close(i);
}
umask(0);
return;
}
注意守护进程一般需要在 root 权限下运行。
通过
ps -ef | grep 'daemon'
可以看到:
root 2 2025 0 14:20 ? 00:00:00 ./daemon
并且产生了 daemon.log,里面是这样的时间标签
Thu Dec 8 14:35:11 2016
Thu Dec 8 14:36:11 2016
Thu Dec 8 14:37:11 2016
最后我们想退出守护进程,只需给守护进程发送 SIGQUIT 信号即可
sudo kill -3 2
再次使用 ps 会发现进程已经退出。
其实我们完全可以利用 daemon() 函数创建守护进程,其函数原型:
#include <unistd.h>
int daemon(int nochdir, int noclose);
DESCRIPTION
The daemon() function is for programs wishing to detach themselves
from the controlling terminal and run in the background as system
daemons.
If nochdir is zero, daemon() changes the process's current working
directory to the root directory ("/"); otherwise, the current working
directory is left unchanged.
If noclose is zero, daemon() redirects standard input, standard
output and standard error to /dev/null; otherwise, no changes are
made to these file descriptors.
RETURN VALUE
(This function forks, and if the fork(2) succeeds, the parent calls
_exit(2), so that further errors are seen by the child only.) On
success daemon() returns zero. If an error occurs, daemon() returns
-1 and sets errno to any of the errors specified for the fork(2) and
setsid(2).
daemon() 函数将调用进程变成守护进程。
参数:
noclose:=0将标准输入、标准输出、标准错误重定向至“/dev/null”
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}\
while (0);\
void creat_daemon(void);
int main(void)
{
time_t t;
int fd;
if(daemon(0,0) == -1)
ERR_EXIT("daemon error");
while(1){
fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,04);
if(fd == -1)
ERR_EXIT("open error");
t = time(0);
char *buf = asctime(localtime(&t));
write(fd,buf,strlen(buf));
close(fd);
sleep(60);
}
return 0;
}
运行结果和上面一样
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务