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

            亚洲午夜久久久影院| 亚洲精品蜜桃久久久久久| 久久精品一区二区国产| 青青草原综合久久| 亚洲国产天堂久久综合| 亚洲中文字幕久久精品无码喷水| 久久久国产乱子伦精品作者| 久久久久99精品成人片| 色狠狠久久AV五月综合| 国产成人久久精品麻豆一区| 亚洲中文久久精品无码| 久久免费视频一区| 99精品久久久久久久婷婷| 久久久久久久亚洲Av无码| 久久精品无码av| 97久久精品国产精品青草| 伊人久久大香线蕉av不变影院| 情人伊人久久综合亚洲| 久久精品国产亚洲av麻豆色欲 | 91麻豆精品国产91久久久久久| 久久久久亚洲精品日久生情| 国产精品伊人久久伊人电影| 97久久香蕉国产线看观看| 欧美激情一区二区久久久| 久久久久亚洲精品男人的天堂| 亚洲国产天堂久久综合网站| 久久精品中文騷妇女内射| 一本久久a久久精品vr综合| 偷偷做久久久久网站| 亚洲精品NV久久久久久久久久| 久久久久99精品成人片牛牛影视| 国产精品一区二区久久精品无码| 国产福利电影一区二区三区久久久久成人精品综合 | 久久无码AV中文出轨人妻| 久久一区二区三区免费| 国产激情久久久久影院| 99久久人人爽亚洲精品美女| 国产精品免费久久久久影院| 久久99精品国产麻豆不卡| 日韩久久无码免费毛片软件| 欧美亚洲日本久久精品|