原创 linux 守护进程、看守另外3个进程

2011-6-20 09:16 2645 11 11 分类: MCU/ 嵌入式

程序注释(功能):
1、这是一个守护进程,守护进程看护着另外3个进程,当另外3个进程意外退出是启动。
2、可以防止守护进程被启动2次。确保本守护进程只有一个存在。
3、可以防止被看管的进程被启动2次。确保被看管的进程只有一个存在。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>
#include <string.h>


main(int argc,char **argv)
{
 time_t now;
 int childpid,fd,fdtablesize,temp;
 int error,in,out,pid;
 FILE *file,*daemon_file;
 char buffer1[20] = {0};
 char buffer2[20] = {0};

 syslog( LOG_USER|LOG_INFO,"the snmpd.pid error!%d %s\n",__LINE__,buffer1 );
 /* 忽略终端 I/O信号,STOP信号 */
 signal(SIGTTOU,SIG_IGN);
 signal(SIGTTIN,SIG_IGN);
 signal(SIGTSTP,SIG_IGN);
 signal(SIGHUP ,SIG_IGN);


 system("ps -A|grep daemon_test|awk '{print $1}' > /tmp/daemon_test.pid");
 daemon_file = fopen("/tmp/daemon_test.pid", "r");
 if(daemon_file)
 {
  syslog( LOG_USER|LOG_INFO,"daemon_file fopen success! :%d \n",__LINE__);
  //if ( fgets( buffer1, sizeof(buffer1), daemon_file ))
    
  {
   fgets( buffer1, sizeof(buffer1), daemon_file );
   syslog( LOG_USER|LOG_INFO,"daemon_file fgets success buffer1 :%s  line:%d \n",buffer1,__LINE__ );
   if(NULL != fgets( buffer2, sizeof(buffer2), daemon_file ))
   {
    syslog( LOG_USER|LOG_INFO,"daemon_test process >1 line:%d \n",__LINE__ );
    pid= atol(buffer2);
    sprintf(buffer2, "%s %i", "kill", pid);
    system(buffer2);
    memset(buffer2,0,sizeof(buffer2));
   }
  }
 }

 /* 父进程退出,程序进入后台运行 */
 if(fork()!=0)
 { 
  syslog( LOG_USER|LOG_INFO," father fork  line= %d \n",__LINE__);

  exit(1);
 }

 /* 创建一个新的会议组 */
 if(setsid()<0)
 {
  exit(1);
 }

 /* 子进程退出,孙进程没有控制终端了 */ 
 if(fork()!=0)
 {
     exit(1);
 }

 //if(chdir("/tmp")==-1)
 //{
 // exit(1);
 //}

 //if(chroot("/")==-1)
 //{
 // exit(1);
 //}

 /*重设文件创建掩模 */
 umask(0);

 syslog( LOG_USER|LOG_INFO,"the snmpd.pid error!%d\n",__LINE__ );
 /* 忽略SIGCHLD信号 */
 signal(SIGCHLD,SIG_IGN);

 /*打开log系统*/
 syslog(LOG_USER|LOG_INFO,"daemon process start!\n"); 
 
 childpid = fork();
 if ( childpid != 0 )
 {

  syslog(LOG_USER|LOG_INFO,"pid = %d childpid = %d  line = %d \n",getpid(),childpid,__LINE__);
  while(1) 
  { 
   FILE *file,*file1,*file2;
   char buffer3[20];
   char buffer4[20];
   syslog( LOG_USER|LOG_INFO,"the daemon start :%d \n",__LINE__);
   system("ps -A|grep test1|awk '{print $1}' > /tmp/test1.pid");
   system("ps -A|grep test2|awk '{print $1}' > /tmp/test2.pid");
   system("ps -A|grep test3|awk '{print $1}' > /tmp/test3.pid");
   syslog(LOG_USER|LOG_INFO,"ps -A|grep test n|awk '{print $1}' > /tmp/test n.pid\n",__LINE__);
   file = fopen("/tmp/test1.pid", "r");
   file1 = fopen("/tmp/test2.pid", "r");
   file2 = fopen("/tmp/test3.pid", "r");
   syslog( LOG_USER|LOG_INFO,"the test1  :%d \n",__LINE__);

   if ( !file )
   {
    syslog( LOG_USER|LOG_INFO,"the test1 fopen error! :%d \n",__LINE__);
    //处理snmpd进程关闭问题
   }
   else
   {
    syslog( LOG_USER|LOG_INFO,"test1 fopen success! :%d \n",__LINE__);
    if ( fgets( buffer4, sizeof(buffer4), file ))
    {
     syslog( LOG_USER|LOG_INFO,"test1 fgets success :%d \n",__LINE__ );
     if(NULL != fgets( buffer3, sizeof(buffer3), file ))
     {
      syslog( LOG_USER|LOG_INFO,"test1 process >1 line:%d \n",__LINE__ );
      pid= atol(buffer3);
      sprintf(buffer3, "%s %i", "kill", pid);
      system(buffer3);
      syslog( LOG_USER|LOG_INFO,"kill test1 process line:%d \n",__LINE__ );
      memset(buffer3,0,sizeof(buffer3));
     }

    }
    else
    {
     syslog( LOG_USER|LOG_INFO,"test1 fgets error :%d \n",__LINE__ );
     int temp_pid;
     temp_pid = fork();
     if(temp_pid == 0)
     {
      syslog(LOG_USER|LOG_INFO,"children2 process start!  :%d \n",__LINE__);
      execl("/home/wangdan/jinche_test/test1",NULL,NULL,NULL);
      syslog(LOG_USER|LOG_INFO,"children2 process end!  :%d \n",__LINE__);
      sleep(13);
      exit(2);
     }

     //处理snmpd进程关闭问题
    }

    fclose(file);
   }

   if ( !file1 )
   {
    syslog( LOG_USER|LOG_INFO,"the test2 fopen error! :%d \n",__LINE__);
    //处理snmpd进程关闭问题
   }
   else
   {
    syslog( LOG_USER|LOG_INFO,"test2 fopen success! :%d \n",__LINE__);
    if ( fgets( buffer4, sizeof(buffer4), file1 ))
    {
     syslog( LOG_USER|LOG_INFO,"test2 fgets success :%d \n",__LINE__ );
     if(NULL != fgets( buffer3, sizeof(buffer3), file1 ))
     {
      syslog( LOG_USER|LOG_INFO,"test1 process >1 line:%d \n",__LINE__ );
      pid= atol(buffer3);
      sprintf(buffer3, "%s %i", "kill", pid);
      system(buffer3);
      syslog( LOG_USER|LOG_INFO,"kill test1 process line:%d \n",__LINE__ );
      memset(buffer3,0,sizeof(buffer3));
     }
    }
    else
    {
     syslog( LOG_USER|LOG_INFO,"test2 fgets error :%d \n",__LINE__ );
     int temp_pid;
     temp_pid = fork();
     if(temp_pid == 0)
     {
      syslog(LOG_USER|LOG_INFO,"children2 process start!  :%d \n",__LINE__);
      execl("/home/wangdan/jinche_test/test2",NULL,NULL,NULL);
      syslog(LOG_USER|LOG_INFO,"children2 process end!  :%d \n",__LINE__);
      sleep(13);
      exit(2);
     }

     //处理snmpd进程关闭问题
    }

    fclose(file1);
   }

   if ( !file2 )
   {
    syslog( LOG_USER|LOG_INFO,"the test3 fopen error! :%d \n",__LINE__);
    //处理snmpd进程关闭问题
   }
   else
   {

    syslog( LOG_USER|LOG_INFO,"test3 fopen success! :%d \n",__LINE__);
    if ( fgets( buffer4, sizeof(buffer4), file2))
    {
     syslog( LOG_USER|LOG_INFO,"test3 fgets success :%d \n",__LINE__ );
     if(NULL != fgets( buffer3, sizeof(buffer3), file2 ))
     {
      syslog( LOG_USER|LOG_INFO,"test1 process >1 line:%d \n",__LINE__ );
      pid= atol(buffer3);
      sprintf(buffer3, "%s %i", "kill", pid);
      system(buffer3);
      syslog( LOG_USER|LOG_INFO,"kill test1 process line:%d \n",__LINE__ );
      memset(buffer3,0,sizeof(buffer3));
     }
    }
    else
    {
     syslog( LOG_USER|LOG_INFO,"test3 fgets error :%d \n",__LINE__ );
     int temp_pid;
     temp_pid = fork();
     if(temp_pid == 0)
     {
      syslog(LOG_USER|LOG_INFO,"children2 process start!  :%d \n",__LINE__);
      execl("/home/wangdan/jinche_test/test3",NULL,NULL,NULL);
      syslog(LOG_USER|LOG_INFO,"children2 process end!  :%d \n",__LINE__);
      sleep(13);
      exit(2);
     }

    }

    fclose(file2);
   }

   sleep(6);

  } 
 }
 else
 {
  syslog(LOG_USER|LOG_INFO,"children process start!  :%d \n",__LINE__);
  syslog(LOG_USER|LOG_INFO,"2222222222222222222222222 line :%d \n",__LINE__);
  //execl("/home/kala/Timer/timer",NULL); 

 }
}

 

 

文章评论0条评论)

登录后参与讨论
我要评论
0
11
关闭 站长推荐上一条 /2 下一条