<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 5, comments - 14, trackbacks - 0, articles - 11

    linux進(jìn)程監(jiān)控

    Posted on 2007-02-12 16:30 東舟 閱讀(1324) 評(píng)論(4)  編輯  收藏 所屬分類: Linux

    ?


    #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 ;
    ?}

    Feedback

    # re: linux進(jìn)程監(jiān)控[未登錄](méi)  回復(fù)  更多評(píng)論   

    2007-12-09 15:15 by zhan
    我很想知道你這個(gè)進(jìn)程監(jiān)控程序大概的思路,尤其是他是怎么樣對(duì)我想監(jiān)控的進(jìn)程進(jìn)行監(jiān)控的,是怎樣判斷的是不是那個(gè)進(jìn)程。謝謝!!
    我的郵箱:zhanshicai8139@163.com

    # 可以告訴我你的思路嗎?  回復(fù)  更多評(píng)論   

    2008-03-05 13:49 by jiangminjie
    我的郵箱是:jiangminjie2008@hotmail.com

    # re: linux進(jìn)程監(jiān)控[未登錄](méi)  回復(fù)  更多評(píng)論   

    2012-08-07 21:05 by jiang
    我的郵箱:jlyh225@163.com
    我也想知道你的思路

    # re: linux進(jìn)程監(jiān)控  回復(fù)  更多評(píng)論   

    2012-10-12 17:24 by avrtercel
    你好,最近在學(xué)習(xí)進(jìn)程監(jiān)控這一塊,想請(qǐng)教博主的這段程序的進(jìn)程監(jiān)控的原理,我的郵箱avrtercel@163.com,辛苦了!謝謝!

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产小视频精品久久久三级| a毛片免费全部在线播放**| 亚洲人成在久久综合网站| 久久综合亚洲色一区二区三区 | 大地资源在线资源免费观看| 国产亚洲精品免费视频播放| 毛片基地看看成人免费| 99精品视频在线观看免费| 免费看一区二区三区四区| 免费在线中文日本| 免费A级毛片无码A∨免费| 国产a视频精品免费观看| 免费av欧美国产在钱| 免费鲁丝片一级在线观看| 日本无卡码免费一区二区三区| 四只虎免费永久观看| 亚洲国产香蕉人人爽成AV片久久 | 日本xxwwxxww在线视频免费| 四虎成人精品在永久免费| 精品亚洲视频在线观看| 国产亚洲大尺度无码无码专线 | 亚洲国产一成人久久精品| 久久久久亚洲AV成人无码| 亚洲日韩乱码中文无码蜜桃| 伊人久久亚洲综合影院首页| 美女一级毛片免费观看| 99久久成人国产精品免费| 777成影片免费观看| 成年女人毛片免费播放视频m| 国产三级免费观看| 亚洲精品白浆高清久久久久久| 亚洲综合激情另类小说区| 亚洲真人无码永久在线观看| 人禽伦免费交视频播放| 四虎影视在线影院在线观看免费视频 | 亚洲精品在线免费观看视频| 亚洲国产欧美日韩精品一区二区三区| 精品在线视频免费| 香港a毛片免费观看 | 在人线av无码免费高潮喷水| 亚洲国产黄在线观看|