支持HW团队,就支付宝领取下面的红包吧!(2018年3月31前,就几毛,也会几块,可以和其他红包叠加使用),你领取消费,HW有奖励。红包使用无条件限制,有条件请注意是不是有病毒。

小伙伴们,给大家发红包喽!人人可领,领完就能用。祝大家领取的红包金额大大大!#吱口令#长按复制此消息,打开支付宝就能领取!er1OEj73Uj

登入 注册 | 验证
| 搜索
HelloWorld论坛 : > 计算机科学、技术、教学> 电脑应用> linux系统> [转]linux进程监控
 
 
 
 
 
 
类别:c++ 阅读:4471 评论:0 时间:三月 18, 2012, 8:19 a.m. 关键字:

 

 来源: http://www.blogjava.net/jnzgrass/archive/2007/12/09/99473.html

#include  < sys / wait.h > 
#include  < sys / types.h > 
#include  < unistd.h > 
#include  < errno.h > 
#include  < stdarg.h > 
#include  < stdio.h > 
#include  < string.h > 

int  startprocess( char *  path[], char *  name[], int  pid[ 3 ]);
int  watchprocess( int  ppid);
int  rebootpocess( char *  path[], char *  name[], int  ppid, int  num);
int  main( int  argc,  char   * argv[],  char   * envp[]) {


        char *  path[ 3 ];
        path[ 0 ]  =   " /home/tony/testmysql/source/mysqlDB/test " ;
        path[ 1 ]  =   " /home/tony/testmysql/source/mysqlDB/test1 " ;
        path[ 2 ]  =   " /home/tony/testmysql/source/mysqlDB/test2 " ;
        char *  name[ 3 ];
        name[ 0 ] = " test " ;
        name[ 1 ] = " test1 " ;
        name[ 2 ] = " test2 " ;

        int  pid[ 3 ]; int  ppid;


        startprocess(path,name,pid);
        // printf("%d\n",pid[0]); 

        for (;;) {
                int  j;
                for (j = 0 ;j < 3 ;j ++ ) {
                        if (watchprocess(pid[j]) < 0 )
                                rebootpocess(path,name,pid[j],j);
                } 
                sleep( 30 );
        } 

        // if(watchprocess(3025)<0)
        //  rebootpocess(path,name,5317,2); 
        return   0 ;
} 
int  startprocess( char *  path[], char *  name[], int  pid[ 3 ]) {

        int  i = 0 ;
        for (i = 0 ;i < 3 ;i ++ ) {
                int  fd;
                fd = fork();
                if (fd < 0 )
                {
                        return   - 1 ; 
                } 
                if (fd == 0 )
                {
                        execl(path[i],name[i],( char   *  ) 0 );
                        exit( 0 );
                } 
                else 
                {  
                        pid[i] = fd;
                        continue ;
                } 
        }  


        return   0 ;
} 
int  watchprocess( int  ppid) {

        printf( " ======watchprocess========\n " );
        printf( " %d\n " ,ppid);

        char *  filename = ( char   * )malloc( 100 );
        sprintf(filename, " /proc/%d/status " ,ppid);
        FILE  *  pFile;
        char   *  line  =  NULL;
        size_t len  =   0 ;
        ssize_t read;
        pFile  =  fopen (filename, " r " );
        if  (pFile == NULL)  {
                printf( " %s\n " ,  " Config file is not exist!\n " );
                return   - 1 ;
        } 
        int  linenum = 0 ;
        char   *  tmp;
        // char* tmp1 = (char*)malloc(20); 
        while  ((read  =  getline( & line,  & len, pFile))  !=   - 1   &&  linenum  <   3 )  {
                linenum ++ ;     
                strtok(line,  " \t " );
                tmp  =  strtok(NULL, "   " );
                if ( linenum  ==   2   &&  ((strcmp(tmp, " S " ) == 0 )  || (strcmp(tmp, " R " ) == 0 )) ) {
                        // printf("sdfsdlfw2rwW$#@^$%&YERGHFDGDFGERYG\n"); 
                        fclose(pFile);
                        pFile  =  NULL;
                        return   0 ;
                }      
        } 
        free(filename);
        fclose(pFile);
        pFile  =  NULL; 
        return   - 1 ;
} 
int  rebootpocess( char *  path[], char *  name[], int  ppid, int  num) {

        printf( " ======rebootpocess========\n " );
        printf( " %d\n " ,num);
        if (kill(ppid,SIGKILL) == 0 )
        {   
                int  fd;   
                fd = fork();
                if (fd == 0 )
                {     
                        execl(path[num],name[num],NULL);
                        exit( 0 );
                } 
                else 
                {
                        return   0 ;
                } 
        } 
        else 
        {
                printf( " Kill the process failed! " );
                return   - 1 ; 
        } 

        return   0 ;
} 

 

[挂载人]初学MPEG [审核人]初学MPEG 推荐

个人签名--------------------------------------------------------------------------------

Please Login (or Sign Up) to leave a comment