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

            網(wǎng)絡(luò)服務(wù)器軟件開發(fā)/中間件開發(fā),關(guān)注ACE/ICE/boost

            C++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              152 Posts :: 3 Stories :: 172 Comments :: 0 Trackbacks

            代碼操作步驟如下:

            [tqg@localhost test]$ vi test.cpp


            #include <pthread.h>
            #include <signal.h>
            #include <stdio.h>
            #include <unistd.h>

            void signal_handler(int sig)
            {
                    printf("catch signal: %d,thread id = %u\n",sig,pthread_self());
                    pthread_exit(0);
            }

            void* thread_handler(void*arg)
            {
                    signal(SIGQUIT,signal_handler);

                    printf("thread arg = %s\n",(char*)arg);

                    sleep(10);
                    printf("in thread\n");

                    return (void*)0;
            }

            int main()
            {
                    char* pArg = "hello";
                    pthread_t tid;
                    pthread_create(&tid,NULL,thread_handler,pArg);

                    printf("main thread id = %u\n",pthread_self());
                    sleep(2);

                    printf("killing now\n");
                    pthread_kill(tid,SIGQUIT);

                    sleep(20);

                    printf("exit main now\n");

                    return 0;
            }
            ~
            ~
            ~
            ~
            "test.cpp" 42L, 648C written
            [tqg@localhost test]$ g++ -o test test.cpp -lpthread
            [tqg@localhost test]$ ./test
            main thread id = 3086875296
            thread arg = hello
            killing now
            catch signal: 3,thread id = 3086871472
            exit main now
            [tqg@localhost test]$

            可以看出,信號(hào)處理函數(shù)的執(zhí)行是在要捕獲信號(hào)的子線程thread_handler的上下文中執(zhí)行的。

            posted on 2009-01-15 11:39 true 閱讀(2403) 評(píng)論(6)  編輯 收藏 引用 所屬分類: C++基礎(chǔ)linux

            Feedback

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解[未登錄] 2009-01-15 12:59 steven
            昏倒,
            pthread_kill(tid,SIGQUIT);這是什么呀,這是向線程發(fā)信號(hào),而不是向進(jìn)程發(fā)。
            你要測(cè)試你需要的效果,請(qǐng)用kill(getpid(),SIGQUIT);  回復(fù)  更多評(píng)論
              

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解 2009-01-15 14:51 true
            沒太明白樓上的意識(shí)哦,我就是要向線程發(fā)送信號(hào)哦,“該線程先獲得cpu時(shí)間”的描述有誤解?確切些是進(jìn)程中的線程獲得了cpu時(shí)間  回復(fù)  更多評(píng)論
              

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解 2009-01-24 06:53 ooo
            愣是沒看懂。。。lz你太有才了。。。

            你自己create了一個(gè)thread,然后發(fā)signal給剛建好的thread--到此為止一切正常

            但是你怎么得出結(jié)論,這是內(nèi)核為你創(chuàng)建的一個(gè)線程?這分明是你自己剛創(chuàng)建的啊!

            你還是別學(xué)編程了。。。  回復(fù)  更多評(píng)論
              

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解 2009-02-02 16:05 true
            @ooo
            我表達(dá)的不夠清楚?“可以推斷為,當(dāng)內(nèi)核(?)捕獲到信號(hào)時(shí),啟動(dòng)了一個(gè)更高優(yōu)先級(jí)的線程,該線程先獲得cpu時(shí)間,有他來執(zhí)行信號(hào)處理函數(shù)。”,我是說的執(zhí)行signal_handler的線程,不是thread_handler!  回復(fù)  更多評(píng)論
              

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解 2009-03-10 11:02 bert
            樓主啊,這是你自創(chuàng)的結(jié)論吧。。。
            在main函數(shù)的printf("main thread id = %u\n",pthread_self());
            下面添加一行:
            printf("child thread id = %u\n", tid);
            看看結(jié)果。
            再思考一下吧。。  回復(fù)  更多評(píng)論
              

            # re: linux下信號(hào)處理函數(shù)實(shí)現(xiàn)的一點(diǎn)理解 2009-03-10 11:24 true
            @bert
            @ooo
            今天又看了下文章,發(fā)現(xiàn)自己的錯(cuò)誤如此低級(jí),竟然誤拿main的id和子線程id進(jìn)行了比較,謝謝2位,并向大家道歉!文章雖然修改  回復(fù)  更多評(píng)論
              

            久久夜色精品国产亚洲av| 久久精品国产清自在天天线| 亚洲中文字幕无码久久综合网| 久久影视综合亚洲| 国产精品一区二区久久精品涩爱| 最新久久免费视频| 久久综合久久自在自线精品自 | 色偷偷888欧美精品久久久| 久久精品亚洲精品国产欧美| 精品久久久久久国产| 久久99中文字幕久久| 久久乐国产精品亚洲综合| 久久久久亚洲AV片无码下载蜜桃| 国产精品午夜久久| 久久久无码精品亚洲日韩按摩| 国产午夜精品理论片久久| 伊人久久综合成人网| 久久人妻少妇嫩草AV蜜桃| 国产精品免费看久久久| 精品久久久久成人码免费动漫| 久久99久久99小草精品免视看| 亚洲中文久久精品无码ww16| 久久人人爽人人爽人人片AV东京热| 99国产精品久久久久久久成人热| 亚洲精品tv久久久久| 国产精品久久久久久福利69堂| 久久精品极品盛宴观看| 欧美久久天天综合香蕉伊| 久久久91精品国产一区二区三区| 久久久精品久久久久影院| 女同久久| 欧美久久一级内射wwwwww.| 国产成人久久久精品二区三区 | 韩国三级中文字幕hd久久精品| 国内精品久久久久久久97牛牛| 久久久久久精品无码人妻| 久久精品国产亚洲7777| 久久香蕉国产线看观看99| 国产精品久久久久久搜索| 久久久国产精品亚洲一区| 精品久久久久久无码专区不卡|