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

            色噜噜狠狠先锋影音久久| 亚洲中文字幕伊人久久无码| 亚洲?V乱码久久精品蜜桃| 亚洲美日韩Av中文字幕无码久久久妻妇 | 国产精品一久久香蕉国产线看| 亚洲色大成网站WWW久久九九| 久久精品人人做人人爽97 | 久久精品国产99久久丝袜| 欧美精品九九99久久在观看| 精品乱码久久久久久久| 久久精品亚洲精品国产欧美| 久久热这里只有精品在线观看| 久久久久久亚洲Av无码精品专口| 成人a毛片久久免费播放| 成人综合久久精品色婷婷| 九九99精品久久久久久| 久久精品青青草原伊人| 国产一区二区三精品久久久无广告 | 中文字幕久久亚洲一区| 99久久这里只有精品| 女人高潮久久久叫人喷水| 国产韩国精品一区二区三区久久| 日韩十八禁一区二区久久| 久久综合综合久久狠狠狠97色88 | 久久超乳爆乳中文字幕| 亚洲v国产v天堂a无码久久| 色综合久久中文色婷婷| 国内精品久久久久影院日本| 精品久久久无码21p发布| 欧美亚洲日本久久精品| 国产成人精品久久亚洲高清不卡| 久久综合狠狠综合久久综合88| 午夜精品久久久久久| 久久久久久久综合日本| 久久青草国产精品一区| 精品一区二区久久| 国内精品久久久久| 国产福利电影一区二区三区久久久久成人精品综合 | 久久精品国产亚洲AV久| 精品久久久久成人码免费动漫 | 精品熟女少妇av免费久久|