<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進程監控

    Posted on 2007-02-12 16:30 東舟 閱讀(1323) 評論(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進程監控[未登錄]  回復  更多評論   

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

    # 可以告訴我你的思路嗎?  回復  更多評論   

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

    # re: linux進程監控[未登錄]  回復  更多評論   

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

    # re: linux進程監控  回復  更多評論   

    2012-10-12 17:24 by avrtercel
    你好,最近在學習進程監控這一塊,想請教博主的這段程序的進程監控的原理,我的郵箱avrtercel@163.com,辛苦了!謝謝!

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产精品视频永久免费播放| 亚洲精品老司机在线观看| 亚洲色大成网站www永久一区| 2017亚洲男人天堂一| 国产婷婷成人久久Av免费高清| 成年丰满熟妇午夜免费视频| 久久久久亚洲精品成人网小说| 深夜A级毛片视频免费| 18禁免费无码无遮挡不卡网站 | 国产大片线上免费看| 亚洲福利视频网址| a毛片在线还看免费网站| 免费大黄网站在线观看| 中文文字幕文字幕亚洲色| 久久综合国产乱子伦精品免费| 国产精品亚洲精品日韩已方 | 亚洲视频在线观看免费| 亚洲一区二区三区乱码A| 亚洲av成人中文无码专区| 一个人看的www在线观看免费| 亚洲精品免费在线观看| a级午夜毛片免费一区二区| 亚洲人成人无码网www国产| 成人婷婷网色偷偷亚洲男人的天堂 | 十九岁在线观看免费完整版电影| 亚洲午夜无码AV毛片久久| 国产精品亚洲综合一区在线观看 | 免费午夜爽爽爽WWW视频十八禁| 亚洲日本久久久午夜精品| 18禁黄网站禁片免费观看不卡| 亚洲丁香色婷婷综合欲色啪| a级大片免费观看| 亚洲色成人WWW永久网站| aa午夜免费剧场| 亚洲一区二区视频在线观看| 黄色毛片免费在线观看| 免费人成视频在线观看视频| 羞羞的视频在线免费观看| 国产免费黄色大片| 老司机精品视频免费| 婷婷亚洲天堂影院|