1 //????USE_SIGWAIT?
2 --Thread b7f316c0 Received Signal  3(Quit)!   //Ctrl+
3 sigwait returns 0(Success)?? signo = 2         //Ctrl+C
4 //δ????USE_SIGWAIT?
5 --Thread b7fb66c0 Received Signal  3(Quit)!   //Ctrl+
6 sigwaitinfo returns -1(Interrupted system call)?? signo = 0

???????????sigwaitinfo()???????????????????ж????sigwait()?????ж??
????3.2 ???2
??????????????????У?sigwait()??sigwaitinfo()??????????????????

 

1 void *SigMgrThread(void *pvArg)
2 {
3     pthread_detach(pthread_self());
4
5     //????SIGQUIT???????????????????????
6     signal(SIGQUIT?? sighandler);
7
8     //????????????pvArg?????????????
9     int dwRet;
10     while(1)
11     {
12 #ifdef USE_SIGWAIT
13         int dwSigNo;
14         dwRet = sigwait((sigset_t*)pvArg?? &dwSigNo);
15         if(dwRet == 0)
16             SigHandler(dwSigNo);
17         else
18             printf("sigwait() failed?? errno: %d(%s)! "?? dwRet?? strerror(dwRet));
19 #else
20         siginfo_t tSigInfo;
21         dwRet = sigwaitinfo((sigset_t*)pvArg?? &tSigInfo);
22         if(dwRet != -1) //dwRet??tSigInfo.si_signo????
23             SigHandler(tSigInfo.si_signo);
24         else
25         {
26             if(errno == EINTR) //??????????ж?
27                 printf("sigwaitinfo() was interrupted by a signal handler! ");
28             else
29                 printf("sigwaitinfo() failed?? errno: %d(%s)! "?? errno?? strerror(errno));
30         }
31     }
32 #endif
33 }
34
35 void *WorkerThread(void *pvArg)
36 {
37     pthread_t tThrdId = pthread_self();
38     pthread_detach(tThrdId);
39
40     printf("Thread %x starts to work! "?? (unsigned int)tThrdId);
41     //working...
42     int dwVal = 1;
43     while(1)
44         dwVal += 5;
45 }
46
47 int main(void)
48 {
49     printf("Main thread %x is running! "?? (unsigned int)pthread_self());
50
51     //????SIGUSR1???????′???????????и???????
52     sigset_t tBlockSigs;
53     sigemptyset(&tBlockSigs);
54     sigaddset(&tBlockSigs?? SIGRTMIN);
55     sigaddset(&tBlockSigs?? SIGRTMIN+2);
56     sigaddset(&tBlockSigs?? SIGRTMAX);
57     sigaddset(&tBlockSigs?? SIGUSR1);
58     sigaddset(&tBlockSigs?? SIGUSR2);
59     sigaddset(&tBlockSigs?? SIGINT);
60
61     sigaddset(&tBlockSigs?? SIGSEGV); //???????SIGSEGV???
62
63     //????????????????
64     pthread_sigmask(SIG_BLOCK?? &tBlockSigs?? NULL);
65
66     signal(SIGINT?? sighandler); //??????SIGINT???
67
68     //??????????????????????????????????
69     pthread_t tMgrThrdId;
70     pthread_create(&tMgrThrdId?? NULL?? SigMgrThread?? &tBlockSigs);
71     printf("Create a signal manager thread %x! "?? (unsigned int)tMgrThrdId);
72     //???????????????????????????tMgrThrdId?????????????????
73     pthread_t tMgrThrdId2;
74     pthread_create(&tMgrThrdId2?? NULL?? SigMgrThread?? &tBlockSigs);
75     printf("Create another signal manager thread %x! "?? (unsigned int)tMgrThrdId2);
76
77     //???????????????????????????(??????)???????????
78     pthread_t WkrThrdId;
79     pthread_create(&WkrThrdId?? NULL?? WorkerThread?? NULL);
80     printf("Create a worker thread %x! "?? (unsigned int)WkrThrdId);
81
82     pid_t tPid = getpid();
83     //??????????????????Щ??????tMgrThrdId?????????
84     //?????????tMgrThrdId??δ?????????Щ??????????
85     printf("Send signals... ");
86     kill(tPid?? SIGRTMAX);
87     kill(tPid?? SIGRTMAX);
88     kill(tPid?? SIGRTMIN+2);
89     kill(tPid?? SIGRTMIN);
90     kill(tPid?? SIGRTMIN+2);
91     kill(tPid?? SIGRTMIN);
92     kill(tPid?? SIGUSR2);
93     kill(tPid?? SIGUSR2);
94     kill(tPid?? SIGUSR1);
95     kill(tPid?? SIGUSR1);
96
97     int dwRet = sleep(1000);
98     printf("%d seconds left to sleep! "?? dwRet);
99
100     ThreadKill(WkrThrdId?? 0); //?????????????????????????
101
102     sleep(1000);
103     int *p=NULL; *p=0; //?????δ???(SIGSEGV)
104
105     return 0;
106 }