首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 其他服务器 >

[]守护进程有关问题

2012-03-17 
[求助]守护进程问题我想写个守护进程,可是运行后用ps -ea没有看到守护进程,是什么原因啊?#include errno.

[求助]守护进程问题
我想写个守护进程,可是运行后用ps -ea没有看到守护进程,是什么原因啊?

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "pub_incl.h"
#include <limits.h>

void monvsssvr(void)
{
...
}
void daemonize2(void)
{

  int i,fd0,fd1,fd2;
pid_t pid;
struct sigaction sa;
struct rlimit rl;
  /*
  * Become a session leader to lose controlling TTY.
  */
umask(0);

if(getrlimit(RLIMIT_NOFILE,&rl) < 0)
{
printf("can't get file limit\n");
exit(1);
}
if ((pid = fork()) < 0) 
{
FixTraceLog( "ERROR", __FILE__, __LINE__, "","mondaemon.c:fork error");
exit(1);

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

sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if(sigaction(SIGHUP,&sa,NULL) < 0)
{
printf("%s:can't ignore SIGHUP");
exit(1);
}
if((pid = fork()) < 0)
{
printf("%s:can't fork\n");
exit(1);
}
else if(pid != 0)
{
exit(0);
}

  /*
  * Change the current working directory to the root.
  */
if (chdir("/") < 0) {
FixTraceLog( "ERROR", __FILE__, __LINE__, "","chdir");
exit(1);


if(rl.rlim_max == RLIM_INFINITY)
{
rl.rlim_max = 1024;
}
for(i = 0;i < rl.rlim_max;++ i)
{
close(i);
}
  /*
  * Attach file descriptors 0, 1, and 2 to /dev/null.
  */
fd0 = open("/dev/null",O_RDWR);
fd1 = dup(0);
fd2 = dup(0);

//openlog(cmd,LOG_CONS,LOG_DAEMON);
if(fd0 != 0 || fd1 != 1 || fd2 != 2)
{
//syslog(LOG_ERR,"unexpected file descriptors %d %d %d",fd0,fd1,fd2);
printf("fd error\n");
exit(1);
}

}

void daemonize(void)
{
pid_t pid;
  /*
  * Become a session leader to lose controlling TTY.
  */
if ((pid = fork()) < 0) 
{
FixTraceLog( "ERROR", __FILE__, __LINE__, "","mondaemon.c:fork error");
exit(1);

else if (pid != 0) /* parent */
exit(0);
printf("1.1\n");
setsid();
  /*
  * Change the current working directory to the root.
  */
  printf("1.2\n");
if (chdir("/") < 0) {
perror("chdir");
exit(1);

  /*
  * Attach file descriptors 0, 1, and 2 to /dev/null.
  */
close(0);
printf("1.3\n");
open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);
}
int main(int argc, char* argv[])
{
if(argc != 2)

printf("\nUsage:参数为tuxedo服务名称\n");
printf("%s [tuxedoSvrName]\n", argv[0]);
exit(0);
}
strcpy(cSvrName, argv[1]);
printf("1\n");
daemonize2();
printf("2\n");
dmninit();

while(1)
{
sleep(1);
printf("3\n");
if(monvsssvr() < 0)
{
return -1;


}
}
}
 
在main里用daemonize()或者daemonize2都不行,是哪里出问题了呢,系统是aix5.3

[解决办法]
太长了,不看了,给个例子吧
实现守护进程的完整实例(每隔10s在/tmp/dameon.log中写入一句话):
   =====================================================================
  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  #include<fcntl.h>
  #include<sys/types.h>
  #include<unistd.h>
  #include<sys/wait.h>
  #include <signal.h>
  #define MAXFILE 65535
  volatile sig_atomic_t _running = 1;
  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);
  signal(SIGTERM, sigterm_handler);
  while( _running ){
   if((fd=open("/tmp/dameon.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
  perror("open");
  exit(1);
  }
  write(fd,buf,len);
  close(fd);
  usleep(10*1000); //10毫秒
  }
  }
  void sigterm_handler(int arg)
  {
  _running = 0;
  }
[解决办法]
没有看到,那应该是程序异常终止了

热点排行