-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpthread-sigwait.c
43 lines (39 loc) · 1.06 KB
/
pthread-sigwait.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
void * signal_waiter(void *times) {
printf("in signal_waiter\n");
int caught;
sigset_t signals_to_catch;
sigemptyset(&signals_to_catch);
sigaddset(&signals_to_catch, SIGUSR1);
for (;;) {
// will block until the signals arrives
// $ kill -USR1 18407
sigwait(&signals_to_catch, &caught);
printf("got signal\n");
break;
}
return NULL;
}
int main(int argc, char** argv) {
printf("pthread sigwait pid=%d.\n", getpid());
sigset_t signals_to_block;
// empty the signal set
sigemptyset(&signals_to_block);
// add the signal to block
sigaddset(&signals_to_block, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &signals_to_block, NULL);
pthread_t t1;
if(pthread_create(&t1, NULL, signal_waiter, NULL)) {
printf("Could not create pthread\n");
exit(1);
}
if(pthread_join(t1, NULL)) {
printf("Could not join pthread\n");
exit(1);
}
return 0;
}