• <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 閱讀(276) 評論(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

            丁香狠狠色婷婷久久综合| 久久AV无码精品人妻糸列| www久久久天天com| 97超级碰碰碰碰久久久久| 91精品国产91久久久久久蜜臀| 精品久久一区二区| 日日狠狠久久偷偷色综合免费| 日本久久中文字幕| 久久AV高清无码| 久久嫩草影院免费看夜色| 久久妇女高潮几次MBA| 久久九九亚洲精品| 一本一本久久aa综合精品| 久久精品国产精品国产精品污| 久久久久亚洲?V成人无码| 色偷偷偷久久伊人大杳蕉| 久久久久国产视频电影| 午夜天堂av天堂久久久| 久久婷婷色综合一区二区| 久久精品国产亚洲AV电影| 少妇被又大又粗又爽毛片久久黑人| 久久亚洲中文字幕精品有坂深雪| 久久国产香蕉视频| 日本道色综合久久影院| 久久综合国产乱子伦精品免费| 久久久久亚洲爆乳少妇无| 久久精品www| 麻豆精品久久精品色综合| 久久人人爽爽爽人久久久| 欧美一区二区久久精品| 久久久久无码国产精品不卡| 久久香蕉综合色一综合色88| 久久久久免费看成人影片| 亚洲精品tv久久久久久久久| 亚洲色欲久久久久综合网| 青青草国产97免久久费观看| 精品国产综合区久久久久久| 91精品国产91久久久久久蜜臀| 国产精品美女久久久久av爽| 日本一区精品久久久久影院| 久久国产精品久久国产精品|