???????????????????????????????????????????pvArg?????????????????????????????????ж???????????????????????????????????????????????????????????????????????????????????棬???????????????(??????)????????????????????檔

????????????У?????????SigMgrThread????????????????????????????????????????????SigMgrThread??????????????????????????pvArg????????????????????????????????????????????????????????????????????
???????????????н??????(?????????USE_SIGWAIT)??

 

1 Main thread b7fcd6c0 is running!
2 Create a signal manager thread b7fccb90!
3 Create another signal manager thread b75cbb90!
4 Create a worker thread b6bcab90!
5 Send signals...
6 ++Thread b7fccb90 Received Signal 10(User defined signal 1)!
7 ++Thread b7fccb90 Received Signal 12(User defined signal 2)!
8 ++Thread b7fccb90 Received Signal 34(Real-time signal 0)!
9 ++Thread b7fccb90 Received Signal 34(Real-time signal 0)!
10 ++Thread b7fccb90 Received Signal 36(Real-time signal 2)!
11 ++Thread b7fccb90 Received Signal 36(Real-time signal 2)!
12 ++Thread b7fccb90 Received Signal 64(Real-time signal 30)!
13 ++Thread b7fccb90 Received Signal 64(Real-time signal 30)!
14 Thread b6bcab90 starts to work!
15 --Thread b7fcd6c0 Received Signal  3(Quit)!      //Ctrl+
16 997 seconds left to sleep!
17 Thread b6bcab90 is alive!
18 ++Thread b7fccb90 Received Signal  2(Interrupt)! //Ctrl+C
19 ++Thread b7fccb90 Received Signal  2(Interrupt)! //Ctrl+C
20 --Thread b7fcd6c0 Received Signal  3(Quit)!      //Ctrl+
21 Segmentation fault

???????°??н?????????????н????
??????6~13?С????????????(???С??SIGRTMIN)???????????????????????????Σ???????????(????Χ?SIGRTMIN~SIGRTMAX)?????????????????????????????????????????????ж???????????????????????????С????????SIGUSR1(10)????SIGUSR2(12)??SIGRTMIN(34)????SIGRTMAX(64)?????????????漲???δ??????????У??????????С?????????????????????????????????????????δ???塣
???????SIGRTMIN/SIGRTMAX????????Unix???п??????????NPTL??????????????????????????LinuxThreads?????????????????????????????????????SIGRTMIN????????????SIGRTMIN+N/SIGRTMAX-N(N?????????)????????????????????SIGRTMIN/SIGRTMAX???????????????switch…case?????±??????
???????kill –l????????????????????
??????6~13?С?sigwait()???????????(thread-safe)???????tMgrThrdId??tMgrThrdId2?????????????????????tMgrThrdId(SigMgrThread)????????????????????????????????
??????14?С?????pthread_create()??????′????????????δ???????????ú?????????′?????????????????
??????15?С?SIGQUIT???????????????????ж?SigMgrThread?е?sigwaitinfo()???á????SIGQUIT???(signal???)??SigMgrThread????????????????????????SIGQUIT????????????????
??????16?С?sleep()????????y?????????????y???????????????sleep()??????????????δ??????(?????????????????????)?????sigwait()???????????????sleep()????????????????з???SIGQUIT?????sleep()??????????SIGINT?????С?
??????????о??????????sleep()??usleep()????????nanosleep()???????????SIGALARM??????(???????)???????????????????usleep()??POSIX 2008?б???????
??????17?С?WorkerThread???????????pthread_detach()?????????????????????????????????????WorkerThread????????У???????ThreadKill()???????????????????????????????????
??????18?С???????????????SIGINT???????????sigwait()?????????????????????????sigwait()??????????
??????19?С?sigwait()?????δ??????????????????????????????????????sigwait()???????????????????????????????????????η???SIGINT?????????sigwait()?????????
??????21?С????pthread_sigmask()????SIGSEGV????sigwait()??δ??????????????"Segmentation fault"???????????????????????????????????SIGSEGV???????????????????????????? signal()/sigaction()???????????????д????????????????????????(longjmp)????????????(exit)?????SIGSEGV??????????????????????С????磬????????????????????????????????????sighandler()????SigMgrThread?????????δ????????н???????????????SIGSEGV????
??????????????????????????????“?????-?????????-???????”????????
????3.3 ???3
?????????????????????????????????????????????????????????????δ?????????

 

1 int gWorkFlag = 0;  //?????????????
2 sigset_t gBlkSigs;  //?????????(???????)
3
4 pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER;
5 pthread_cond_t gCond = PTHREAD_COND_INITIALIZER;
6
7 void *SigThread(void *pvArg)
8 {
9     pthread_detach(pthread_self());
10
11     int dwSigNo;
12     sigwait(&gBlkSigs?? &dwSigNo);
13     if(dwSigNo != SIGUSR1)
14     {
15         printf("Unexpected signal %d! "?? dwSigNo);
16         exit(1);
17     }
18     pthread_mutex_lock(&gLock);
19     gWorkFlag = 1;   //?????????????
20     pthread_mutex_unlock(&gLock);
21     pthread_cond_signal(&gCond);
22
23     return 0;
24 }
25
26 void *WkrThread(void *pvArg)
27 {
28     pthread_detach(pthread_self());
29     printf("Worker thread starts! ");
30
31     pthread_mutex_lock(&gLock);
32     while(gWorkFlag == 0)
33         pthread_cond_wait(&gCond?? &gLock);
34     pthread_mutex_unlock(&gLock);
35     //???′????????????????????????
36     printf("Worker thread starts working... ");
37     int dwVal = 1;
38     while(1)
39         dwVal += 5;
40 }
41
42 int main(void)
43 {
44     sigemptyset(&gBlkSigs);
45     sigaddset(&gBlkSigs?? SIGUSR1);
46     pthread_sigmask(SIG_BLOCK?? &gBlkSigs?? NULL);
47
48     pthread_t tSigThrdId?? tWkrThrdId;
49     pthread_create(&tSigThrdId?? NULL?? SigThread?? NULL);
50     pthread_create(&tWkrThrdId?? NULL?? WkrThread?? NULL);
51
52     while(1);
53     exit(0);
54 }