linux实现daemon程序

本文详细介绍了Linux系统下编写Daemon程序的步骤,并通过一个实际的例子展示了如何创建一个后台运行的服务端程序。文章强调了Daemon程序的特点、编写规则以及如何避免常见问题,如信息输出、信号处理等。通过执行示例程序并使用ps和log命令进行验证,读者能够直观地理解Daemon程序的运行过程和调试方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://linux.chinaitlab.com/c/798692.html

注:SIGTERM 程序结束(terminate)信号,与SIGKILL不同的是该信号可以被阻塞和处理通常用来要求程序自己正常退出. shell命令kill缺省产生这个信号. 

 

编写Linux系统下Daemon程序的方法步骤

  一、引言 Daemon程序是一直运行的服务端程序,又称为守护进程。

  本文介绍了在Linux下编写Daemon程序的步骤,并给出了例子程序。

  二、Daemon程序简介 Daemon是长时间运行的进程,通常在系统启动后就运行,在系统关闭时才结束。一般说Daemon程序在后台运行,是因为它没有控制终端,无法和前台的用户交互。Daemon程序一般都作为服务程序使用,等待客户端程序与它通信。我们也把运行的Daemon程序称作守护进程。

  三、Daemon程序编写规则

  编写Daemon程序有一些基本的规则,以避免不必要的麻烦。

  1、首先是程序运行后调用fork,并让父进程退出。子进程获得一个新的进程ID,但继承了父进程的进程组ID。

  2、调用setsid创建一个新的session,使自己成为新session和新进程组的leader,并使进程没有控制终端(tty)。

  3、改变当前工作目录至根目录,以免影响可加载文件系统。或者也可以改变到某些特定的目录。

  4、设置文件创建mask为0,避免创建文件时权限的影响。

  5、关闭不需要的打开文件描述符。因为Daemon程序在后台执行,不需要于终端交互,通常就关闭STDIN、STDOUT和STDERR。其它根据实际情况处理。

  另一个问题是Daemon程序不能和终端交互,也就无法使用printf方法输出信息了。我们可以使用syslog机制来实现信息的输出,方便程序的调试。在使用syslog前需要首先启动syslogd程序,关于syslogd程序的使用请参考它的man page,或相关文档,我们就不在这里讨论了。

  四、一个Daemon程序的例子 编译运行环境为Redhat Linux 8.0。

  我们新建一个daemontest.c程序,文件内容如下:

  #include <unistd.h>

  #include <sys/types.h>

  #include <sys/stat.h>

  #include <stdlib.h>

  #include <stdio.h>

  #include <syslog.h>

  #include <signal.h>

  int daemon_init(void)

  { pid_t pid;

  if((pid = fork()) < 0)return(-1);

  else if(pid != 0) exit(0); /* parentexit */

  /* child continues */

  setsid(); /* become session leader*/

  chdir("/"); /* changeworking directory */

  umask(0); /* clear file modecreation mask */

  close(0); /* close stdin */

  close(1); /* close stdout */

  close(2); /* close stderr */

  return(0); }

  void sig_term(int signo)

  { if(signo == SIGTERM)

  /* catched signal sent by kill(1)command */

  { syslog(LOG_INFO, "programterminated.");

  closelog(); exit(0); }

  }

  int main(void)

  { if(daemon_init() == -1)

  { printf("can't forkself\n"); exit(0); }

  openlog("daemontest",LOG_PID, LOG_USER);

  syslog(LOG_INFO, "programstarted.");

  signal(SIGTERM, sig_term); /*arrange to catch the signal */

  while(1) { sleep(1); /* put yourmain program here */ }

  return(0); }

  使用如下命令编译该程序: gcc -Wall -o daemontestdaemontest.c编译完成后生成名为daemontest的程序,执行./daemontest来测试程序的运行。

  使用ps axj命令可以显示系统中已运行的daemon程序的信息,包括进程ID、session ID、控制终端等内容。

  部分显示内容:

  PPID PID PGID SID TTY TPGID STATUID TIME COMMAND

  1098 1101 1101 1074 pts/1 1101 S 00:00 -bash 1 1581 777 777 ? -1 S 500 0:13 gedit 1 1650 1650 1650 ? -1 S 5000:00 ./daemontest 794 1654 1654 794 pts/0 1654 R 500 0:00

  ps axj 从中可以看到daemontest程序运行的进程号为1650。

  我们再来看看/var/log/messages文件中的信息: Apr 7 22:00:32 localhost

  daemontest[1650]: program started.

  显示了我们在程序中希望输出的信息。

  我们再使用kill 1650命令来杀死这个进程,/var/log/messages文件中就会有如下的信息:

  Apr 7 22:11:10 localhostdaemontest[1650]: program terminated.

  使用ps axj命令检查,发现系统中daemontest进程已经没有了。

  五、参考资料

  Advanced Programming in the UNIXEnvironment W.Richard Stevens

  http://zhiwei.li/text/tag/daemon/

  <ahref="http://www.chinese%3Ca%20class%3D/"channel_keylink"="" target="_blank" style="margin-top:0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px;padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap:break-word; text-decoration: none; color: rgb(51, 51, 51);">linuxuniversity.net/articles/2153.shtml">http://www.chineselinuxuniversity.net/articles/2153.shtml

  使用 Zope3 技术建立 Python 服务(Unix 版本)

  http://eishn.blog.163.com/blog/static/65231820069310828642/

  daemon.py: daemonize function forPython

  http://www.clapper.org/software/python/daemon/#Installation

  机房监控之Python变daemon守护进程

  http://www.linuxboy.net/wordpress2/2009/05/26/288.html

  Python Daemon(守护进程)

  http://hi.baidu.com/sunorr/blog/item/ef42c1f86dcc1c09d9f9fde9.html

  http://snippets.dzone.com/posts/show/1532

  (翻译)Python标准库的threading.Thread类

  http://www.cppblog.com/len/archive/2008/06/24/54472.html

  python多线程程序的中断(信号)处理

  http://www.162cm.com/archives/904.html

  以daemon方式运行一个程序

  http://bbs3.chinaunix.net/thread-1073255-1-1.html

  ==============

  用 Python写 daemon

  http://blog.youkuaiyun.com/snleo/archive/2008/12/24/3592275.aspx

  http://www.linuxforum.net/forum/showflat.php?Board=python&Number=380279

  http://blog.chinaunix.net/u/5547/showart_1213999.html

  http://bbs2.chinaunix.net/thread-1275803-1-1.html

  [转][Python一招鲜系列] 守护进程一招鲜

  http://blog.163.com/sea_haitao/blog/static/7756216200921023932171/

  http://www.kekeyu.com/database/open_2050607696.html

  http://qingfengxiyu.spaces.live.com/blog/cns!E5F7A3AEF9B09ED!222.entry

  python 非阻塞读数据

  http://www.mangbar.com/task/docview/5d023b211427315f01142a701a150335

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值