• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            POSIX 線程 – pthread_sigmask

            Posted on 2009-11-17 19:56 Prayer 閱讀(6389) 評論(1)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX
            POSIX 線程 – pthread_sigmask

            POSIX 線程 – pthread_sigmask

             


              概念

            • 按照 POSIX, 異步 (外部) 信號發送到整個進程.
            • 所有線程共享同一個設置, 即通過 sigaction 設置的線程處置方法.
            • 每個線程有自己的信號掩碼, 線程庫根據該掩碼決定將信號發送到哪個線程.
            • 由于 Linux 線程實現上的獨特性, 外部信號始終發送到特定的線程.

              pthread_sigmask

            • pthread_sigmask 用來定義線程的信號掩碼
            • 其接口與 sigprocmask 一樣
              ===============================================================================
              #include <pthread.h>
              #include <signal.h>

              int pthread_sigmask (int how, const sigset_t *newmask, sigset_t *oldmask);
              ===============================================================================

              pthread_kill 和 sigwait

            ===============================================================================
            #include <pthread.h>
            #include <signal.h>

            int pthread_kill (pthread_t thread, int signo);
            int sigwait (const sigset_t *set, int *sig);
            ===============================================================================
            • pthread_kill 向特定的線程發送信號.
            • sigwait 暫定調用線程, 直到 set 中定義的某個信號遞達調用線程.
            • sigwait 返回時, sig 中保存的是接收到的信號編號.
            • sigwait 所等待的信號必須在所有線程中阻塞, 而不僅僅是調用線程. 在多線程的程序里,希望只在主線程中處理信號,可以使用

             

            函數:

            int pthread_sigmask (int how,

            const sigset_t *set,

            sigset_t *oset)


                 
            用作在主調線程里控制信號掩碼。

             

            How:

            SIG_BLOCK:     結果集是當前集合參數集的并集
            SIG_UNBLOCK: 
            結果集是當前集合參數集的差集
            SIG_SETMASK: 
            結果集是由參數集指向的集

             

            頭文件: <signal.h>

            錯誤:   [EINVAL] how不是已定義值
            提示:   除非信號在所有的線程里都阻塞,否則總能將異步信號傳輸給這個進程。

            例子:

            #include <pthread.h>

            #include <stdio.h>

            #include <sys/signal.h>

             

            #define NUMTHREADS 3

            void sighand(int signo);

             

            void *threadfunc(void *parm)

            {

                pthread_t             tid = pthread_self();

                int                   rc;

             

                printf("Thread %u entered\n", tid);

                rc = sleep(30);

                printf("Thread %u did not get expected results! rc=%d\n", tid, rc);

                return NULL;

            }

             

            void *threadmasked(void *parm)

            {

                pthread_t             tid = pthread_self();

                sigset_t              mask;

                int                   rc;

             

                printf("Masked thread %lu entered\n", tid);

             

                sigfillset(&mask); /* Mask all allowed signals */

                rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);

                if (rc != 0)

                {

                    printf("%d, %s\n", rc, strerror(rc));

                    return NULL;

                }

             

                rc = sleep(15);

                if (rc != 0)

                {

                    printf("Masked thread %lu did not get expected results! "

                                   "rc=%d \n", tid, rc);

                    return NULL;

                }

                printf("Masked thread %lu completed masked work\n",

                            tid);

                return NULL;

            }

             

            int main(int argc, char **argv)

            {

                int                     rc;

                int                     i;

                struct sigaction        actions;

                pthread_t               threads[NUMTHREADS];

                pthread_t               maskedthreads[NUMTHREADS];

             

                printf("Enter Testcase - %s\n", argv[0]);

             

                printf("Set up the alarm handler for the process\n");

                memset(&actions, 0, sizeof(actions));

                sigemptyset(&actions.sa_mask);

                actions.sa_flags = 0;

                actions.sa_handler = sighand;

             

                rc = sigaction(SIGALRM,&actions,NULL);

             

                printf("Create masked and unmasked threads\n");

                for(i=0; i<NUMTHREADS; ++i)

                {

                    rc = pthread_create(&threads[i], NULL, threadfunc, NULL);

                    if (rc != 0)

                    {

                        printf("%d, %s\n", rc, strerror(rc));

                        return -1;

                    }

             

                    rc = pthread_create(&maskedthreads[i], NULL, threadmasked, NULL);

                    if (rc != 0)

                    {

                        printf("%d, %s\n", rc, strerror(rc));

                        return -1;

                    }

                }

             

                sleep(3);

                printf("Send a signal to masked and unmasked threads\n");

                for(i=0; i<NUMTHREADS; ++i)

                {

                    rc = pthread_kill(threads[i], SIGALRM);

             

                    rc = pthread_kill(maskedthreads[i], SIGALRM);

                }

             

                printf("Wait for masked and unmasked threads to complete\n");

                for(i=0; i<NUMTHREADS; ++i) {

                    rc = pthread_join(threads[i], NULL);

             

                    rc = pthread_join(maskedthreads[i], NULL);

                }

                printf("Main completed\n");

                return 0;

            }

             

            void sighand(int signo)

            {

                pthread_t             tid = pthread_self();

             

                printf("Thread %lu in signal handler\n",

                                         tid);

                return;

            }

            程序返回:

            Enter Testcase - ./pthread_sigmask_test

            Set up the alarm handler for the process

            Create masked and unmasked threads

            Thread 3086597040 entered

            Masked thread 3076107184 entered

            Thread 3065617328 entered

            Masked thread 3055127472 entered

            Thread 3044637616 entered

            Masked thread 3034147760 entered

            Send a signal to masked and unmasked threads

            Wait for masked and unmasked threads to complete

            Thread 3086597040 in signal handler

            Thread 3086597040 did not get expected results! rc=27

            Thread 3065617328 in signal handler

            Thread 3065617328 did not get expected results! rc=27

            Thread 3044637616 in signal handler

            Thread 3044637616 did not get expected results! rc=27

            Masked thread 3076107184 completed masked work

            Masked thread 3055127472 completed masked work

            Masked thread 3034147760 completed masked work

            Main completed

            Feedback

            # re: POSIX 線程 – pthread_sigmask   回復  更多評論   

            2010-03-27 12:43 by yswzing
            可能有 race,pthread_kill 的時候 masked 線程可能還沒有執行 pthread_sigmask 調用
            久久人人爽人人爽人人片AV麻豆 | 亚洲va久久久久| 无码人妻久久一区二区三区蜜桃| 伊人久久大香线蕉综合5g| 少妇久久久久久久久久| 久久免费国产精品一区二区| 日批日出水久久亚洲精品tv| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 理论片午午伦夜理片久久 | 99久久综合国产精品二区| 思思久久精品在热线热| 一级做a爰片久久毛片人呢| 久久精品人人做人人爽97| 人妻无码久久精品| 国产成人久久精品激情| 国产美女亚洲精品久久久综合 | 日批日出水久久亚洲精品tv| 漂亮人妻被黑人久久精品| 香蕉久久影院| 一本久久a久久精品综合夜夜| 亚洲国产精品无码久久久久久曰| 久久99国产精一区二区三区| 久久精品人人做人人爽97| 亚洲国产精品嫩草影院久久| 成人精品一区二区久久久| 亚洲成色www久久网站夜月| 久久青青草原精品国产软件 | 99久久免费国产精精品| 97久久国产亚洲精品超碰热 | 香蕉久久AⅤ一区二区三区| 99久久精品免费观看国产| 国产精品久久波多野结衣| 久久亚洲精品中文字幕| 国内精品久久久久影院薰衣草| 尹人香蕉久久99天天拍| 伊人久久国产免费观看视频 | 亚洲精品乱码久久久久久| 久久无码高潮喷水| 77777亚洲午夜久久多人| 国产色综合久久无码有码| 97久久国产露脸精品国产|