﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-linux</title><link>http://www.cppblog.com/zkmlinux/</link><description>linux &amp; unix</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 19:59:43 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 19:59:43 GMT</pubDate><ttl>60</ttl><item><title>线程与信号</title><link>http://www.cppblog.com/zkmlinux/archive/2010/03/16/109858.html</link><dc:creator>coming</dc:creator><author>coming</author><pubDate>Tue, 16 Mar 2010 14:53:00 GMT</pubDate><guid>http://www.cppblog.com/zkmlinux/archive/2010/03/16/109858.html</guid><wfw:comment>http://www.cppblog.com/zkmlinux/comments/109858.html</wfw:comment><comments>http://www.cppblog.com/zkmlinux/archive/2010/03/16/109858.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zkmlinux/comments/commentRss/109858.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zkmlinux/services/trackbacks/109858.html</trackback:ping><description><![CDATA[<meta http-equiv="content-type" content="text/html; charset=utf-8"><table id="Table1" class="tb" cellspacing="0" cellpadding="3" border="0" style="font-size: 13px; "><tbody><tr><td><pre><div><span style="color: rgb(0, 0, 0); "><br class="Apple-interchange-newline">#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">stdio.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">stdlib.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 255); ">string</span><span style="color: rgb(0, 0, 0); ">.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">signal.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">errno.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">pthread.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">unistd.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">
#include </span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">sys</span><span style="color: rgb(0, 0, 0); ">/</span><span style="color: rgb(0, 0, 0); ">types.h</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">

</span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> sig_handler(</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> signum)
{
    </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(signum </span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); "> SIGUSR1)
        printf(</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">sigusr1\\n</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">);
    </span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(signum </span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); "> SIGUSR2)
        printf(</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">sigusr2\\n</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">);
    </span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); ">
        printf(</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">Receive signale: %d\\n</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">, signum);
}

</span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">sigmgr_thread(</span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">arg)
{
    sigset_t waitset;
    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> sig;
    </span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> rc;
    pthread_t ppid </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> pthread_self();

    pthread_detach(ppid);
    
    sigemptyset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset,SIGRTMIN);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset,SIGRTMIN</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset,SIGRTMAX);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset,SIGUSR1);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset,SIGUSR2);

    </span><span style="color: rgb(0, 0, 255); ">while</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">1</span><span style="color: rgb(0, 0, 0); ">) {
        rc </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> sigwait(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">waitset, </span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">sig);
        </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(rc </span><span style="color: rgb(0, 0, 0); ">==</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">) {
            sig_handler(sig);
        } </span><span style="color: rgb(0, 0, 255); ">else</span><span style="color: rgb(0, 0, 0); "> {
            printf(</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">sigwaitinfo() return err: %d; %s\\n</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">, errno, strerror(errno));
        }
    }
}

</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> main(</span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); ">)
{
    sigset_t    bset, oset;
    pid_t        pid </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> getpid();
    pthread_t    ppid;

    sigemptyset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, SIGRTMIN);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, SIGRTMIN</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, SIGRTMAX);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, SIGUSR1);
    sigaddset(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, SIGUSR2);
    
    </span><span style="color: rgb(0, 0, 255); ">if</span><span style="color: rgb(0, 0, 0); ">(pthread_sigmask(SIG_BLOCK, </span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">bset, </span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">oset) </span><span style="color: rgb(0, 0, 0); ">!=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">) 
        printf(</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">set pthread_mask failed\\n</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">);

    kill(pid, SIGRTMAX);
    kill(pid, SIGRTMAX);
    kill(pid, SIGRTMIN</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">);
    kill(pid, SIGRTMIN);
    kill(pid, SIGRTMIN</span><span style="color: rgb(0, 0, 0); ">+</span><span style="color: rgb(0, 0, 0); ">2</span><span style="color: rgb(0, 0, 0); ">);
    kill(pid, SIGRTMIN);
    kill(pid, SIGUSR2);
    kill(pid, SIGUSR2);
    kill(pid, SIGUSR1);
    kill(pid, SIGUSR1);

    pthread_create(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">ppid, NULL, sigmgr_thread, NULL);

    sleep(</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">);

    exit(</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">);
}</span></div></pre></td></tr></tbody></table><img src ="http://www.cppblog.com/zkmlinux/aggbug/109858.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zkmlinux/" target="_blank">coming</a> 2010-03-16 22:53 <a href="http://www.cppblog.com/zkmlinux/archive/2010/03/16/109858.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>