• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            使用信號殺死其他的線程

            Posted on 2009-08-05 15:15 Prayer 閱讀(275) 評論(0)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX

            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>
            #include <pthread.h>

            #define THREAD_NUM  3
            pthread_t threadBuf[THREAD_NUM];
            int       tcBuf[THREAD_NUM];

            void* threadOpr1(void* arg);
            void* threadOpr2(void* arg);
            void  sigTermHandler(void);

            /**********************************
            試驗一個線程殺死其他的線程
            但是進程不退出
            ***********************************/
            int main(){
               int i=0;
               pthread_t killer;
              
               /*signal(SIGCLD, SIG_IGN);*/
              
                for(i = 0; i < THREAD_NUM; i++){
                    tcBuf[i] = i + 1;
                    if( (pthread_create(&threadBuf[i], NULL,threadOpr1,
                        &tcBuf[i])) != 0 ) {
                        printf("pthread_create \n");
                        return -1;           
                    }    
                } 
               
                sleep(5);
               
                if( (pthread_create(&killer, NULL,threadOpr2,
                    NULL)) != 0 ) {
                    printf("pthread_create \n");
                    return -1;           
                }
               
                sleep(30);
               
                printf("main exit\n");  
                  
                return 0;  
            }
            /**********************************
            被殺死的線程函數
            ***********************************/
            void* threadOpr1(void* arg){
                int tcId=*(int *)arg;
                int i=0;
               
                signal(SIGTERM,(void (*)( ))sigTermHandler);
               
                while(i<10){
                   printf("ID%d --%d  live\n",tcId,pthread_self());
                   i++;
                   sleep(2); 
                }
                printf("killed exit\n");
               
               pthread_exit(NULL);
            }

            /**********************************
            殺死其他線程的線程
            ***********************************/
            void* threadOpr2(void* arg){
                int i=0;
               
                for(i = 0; i < THREAD_NUM; i++){
                    printf("%d \n",i);        
                    if( (pthread_kill(threadBuf[i],SIGTERM)) != 0 ) {
                        printf("pthread_kill \n");
                        pthread_exit(NULL);           
                    }
                    sleep(2);   
                }
                sleep(30); 
               
                printf("killer exit \n"); 
                  
               pthread_exit(NULL);
            }

            /**********************************
            信號處理函數
            ***********************************/
            void sigTermHandler(void)
            {
              /*早期版本,每次信號處理完之后,隨即將信號動作復置為默認值*/
                printf("sigal exit  %d\n",pthread_self());
               signal(SIGTERM,(void (*)( ))sigTermHandler);     
               pthread_exit(NULL);

            }

            結果:
            ID3 --772  live
            ID1 --258  live
            ID2 --515  live
            ID3 --772  live
            ID1 --258  live
            ID2 --515  live
            ID3 --772  live
            ID1 --258  live
            ID2 --515  live
            0
            sigal exit  258
            ID3 --772  live
            ID2 --515  live
            1
            sigal exit  515
            ID3 --772  live
            2
            sigal exit  772
            main exit

            国产成人久久精品激情 | 青青草原精品99久久精品66| 久久国产AVJUST麻豆| 一本色道久久综合狠狠躁| 久久久女人与动物群交毛片| 97久久超碰国产精品旧版| 久久国产成人午夜AV影院| 久久综合九色综合网站| 伊人久久综合热线大杳蕉下载| 女同久久| 久久久久久综合一区中文字幕 | 久久精品亚洲精品国产色婷| 国产成人久久精品麻豆一区| 国产成人精品综合久久久| 日本道色综合久久影院| 久久久久国产精品嫩草影院| 热99re久久国超精品首页| 最新久久免费视频| 久久青青草原国产精品免费| 波多野结衣久久一区二区| 国产福利电影一区二区三区久久老子无码午夜伦不 | 性做久久久久久久久| 久久发布国产伦子伦精品| 国产亚洲精久久久久久无码AV| 国产精品日韩深夜福利久久| 久久国产精品一国产精品金尊 | 亚洲国产成人乱码精品女人久久久不卡| 7777久久久国产精品消防器材| 国产亚洲精午夜久久久久久| 午夜精品久久久久久99热| 久久国产免费直播| 亚洲&#228;v永久无码精品天堂久久 | 欧美熟妇另类久久久久久不卡| 婷婷综合久久中文字幕| 久久99久国产麻精品66| 久久夜色精品国产亚洲av| 久久国产成人午夜AV影院| 免费一级欧美大片久久网| 久久这里都是精品| 久久久久久久久66精品片| 久久久久久免费视频|