文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>编写Daemon程序的基本步骤

编写Daemon程序的基本步骤

时间:2007-06-01  来源:ly44770

 一、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程序的例子    编译运行环境为fedora core 6。 我们新建一个daemon.c程序,文件内容如下:   #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#define MAXFILE 65535
int main()
{
 pid_t pc;
 int i,fd,len;
 char *buf="This is a Dameon\n";
 len =strlen(buf);
 pc=fork();
 if(pc<0){
  printf("error fork\n");
  exit(1);
 }else if(pc>0)
  exit(0);
 setsid();
 chdir("/");
 umask(0);
 for(i=0;i<MAXFILE;i++)
  close(i);
 while(1){
  if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
   perror("open");
   exit(1);
  }
  write(fd, buf, len+1);
  close(fd);
  sleep(10);
 }
}
    使用如下命令编译该程序: gcc -Wall -o daemon daemon.c编译完成后生成名为daemon的程序,执行./daemon来测试程序的运行。   使用ps axj命令可以显示系统中已运行的daemon程序的信息,包括进程ID、session ID、控制终端等内容。   部分显示内容: PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND    1  2024  2024  2024 ?           -1 Ss       0   0:00 ./dameon
 1971  2044  2044  1971 pts/1     2044 R+       0   0:00  从中可以看到daemon程序运行的进程号为2024。
    我们再来看看/tmp/dameon.log 文件中的信息: [root@localhost 7]# cat /tmp/dameon.log
This is a Dameon
This is a Dameon
This is a Dameon
......
This is a Dameon
This is a Dameon
显示了我们在程序中希望输出的信息。
我们再使用 [root@localhost 7]# kill 2024
命令来杀死这个进程,使用ps axj命令检查,发现系统中daemon进程已经没有了。
   四、syslog机制    Daemon程序不能和终端交互,也就无法使用printf方法输出信息了。我们可以使用syslog机制来实现信息的输出,方便程序的调试。在使用syslog前需要首先启动syslogd程序。 使用syslog机制的函数:   #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<syslog.h>
#define MAXFILE 65535
int main()
{
 pid_t pc,sid;
 int i,fd,len;
 char *buf="This is a Dameon\n";
 len =strlen(buf);
 pc=fork();
 if(pc<0){
  printf("error fork\n");
  exit(1);
 }else if(pc>0)
  exit(0);
 openlog("demo_update",LOG_PID, LOG_DAEMON);
 if((sid=setsid())<0){
  syslog(LOG_ERR, "%s\n", "setsid");
  exit(1);
 }
 if((sid=chdir("/"))<0){
  syslog(LOG_ERR, "%s\n", "chdir");
  exit(1);
 }
 umask(0);
 for(i=0;i<MAXFILE;i++)
  close(i);
 while(1){
  if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND, 0600))<0){
   syslog(LOG_ERR, "open");
   exit(1);
  }
  write(fd, buf, len+1);
  close(fd);
  sleep(10);
 }
 closelog();
 exit(0);
}
   
相关阅读 更多 +
排行榜 更多 +
火柴人霓虹战争无限金币

火柴人霓虹战争无限金币

冒险解谜 下载
火柴人英雄格斗无限金币版

火柴人英雄格斗无限金币版

冒险解谜 下载
摔跤革命3d汉化版

摔跤革命3d汉化版

冒险解谜 下载