当前位置: 代码迷 >> AIX >> []守护进程有关问题
  详细解决方案

[]守护进程有关问题

热度:2892   发布时间:2013-02-26 00:00:00.0
[求助]守护进程问题
我想写个守护进程,可是运行后用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;
  相关解决方案