• <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

            久久国产影院| 久久综合给久久狠狠97色| www亚洲欲色成人久久精品| 日本精品久久久久中文字幕| 久久香蕉一级毛片| 中文成人无码精品久久久不卡| 97久久国产综合精品女不卡| 亚洲国产精品无码久久久蜜芽| 久久久久久久综合日本亚洲| 久久婷婷色综合一区二区| 国产毛片欧美毛片久久久| 88久久精品无码一区二区毛片 | 久久精品国产第一区二区| 欧美亚洲另类久久综合婷婷| 久久久久久九九99精品| 久久伊人五月天论坛| 久久精品国产亚洲av日韩| 久久精品国产精品亚洲人人| 久久99国产乱子伦精品免费| 亚洲精品乱码久久久久久蜜桃| 国产精品久久久久久久久| 久久精品国产亚洲AV蜜臀色欲| 久久国产精品99精品国产987| 久久99热这里只有精品66| 久久久久99精品成人片| 国内精品久久久久久99蜜桃 | 久久久久久综合网天天| 国产巨作麻豆欧美亚洲综合久久| 久久婷婷国产综合精品| 波多野结衣久久一区二区| 久久综合成人网| 欧美一级久久久久久久大| 一本大道久久a久久精品综合| 久久精品一本到99热免费| 久久国产AVJUST麻豆| 伊人精品久久久久7777| 久久婷婷五月综合97色直播| 久久国产影院| 亚洲伊人久久综合影院| 亚洲乱码日产精品a级毛片久久| 一本一道久久a久久精品综合|