-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sig.c
150 lines (125 loc) · 4.27 KB
/
test_sig.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <[email protected]>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <[email protected]>.
**
** test_sig.c: Pth test program (signal handling)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include "pth.h"
static pth_t child1;
static pth_t child2;
static void *inthandler(void *_arg)
{
sigset_t sigs;
int sig;
int n;
fprintf(stderr, "inthandler: enter\n");
/* unblock interrupt signal only here */
sigemptyset(&sigs);
sigaddset(&sigs, SIGINT);
pth_sigmask(SIG_UNBLOCK, &sigs, NULL);
/* but the user has to hit CTRL-C three times */
for (n = 0; n < 3; n++) {
pth_sigwait(&sigs, &sig);
fprintf(stderr, "inthandler: SIGINT received (#%d)\n", n);
}
fprintf(stderr, "inthandler: cancelling child1 and child2\n");
pth_cancel(child1);
pth_cancel(child2);
fprintf(stderr, "inthandler: leave\n");
return NULL;
}
static void child_cleanup(void *arg)
{
fprintf(stderr, "%s: running cleanup\n", (char *)arg);
return;
}
static void *child(void *_arg)
{
sigset_t sigs;
char *name = (char *)_arg;
int i;
fprintf(stderr, "%s: enter\n", name);
/* establish cleanup handler */
pth_cleanup_push(child_cleanup, name);
/* block different types of signals */
pth_sigmask(SIG_SETMASK, NULL, &sigs);
sigaddset(&sigs, SIGINT);
if (strcmp(name, "child1") == 0) {
sigaddset(&sigs, SIGUSR1);
sigdelset(&sigs, SIGUSR2);
}
else {
sigdelset(&sigs, SIGUSR1);
sigaddset(&sigs, SIGUSR2);
}
pth_sigmask(SIG_SETMASK, &sigs, NULL);
/* do a little bit of processing and show signal states */
for (i = 0; i < 10; i++) {
pth_sigmask(SIG_SETMASK, NULL, &sigs);
fprintf(stderr, "%s: SIGUSR1: %s\n", name,
sigismember(&sigs, SIGUSR1) ? "blocked" : "unblocked");
fprintf(stderr, "%s: SIGUSR2: %s\n", name,
sigismember(&sigs, SIGUSR2) ? "blocked" : "unblocked");
fprintf(stderr, "%s: leave to scheduler\n", name);
pth_sleep(1);
fprintf(stderr, "%s: reentered from scheduler\n", name);
}
fprintf(stderr, "%s: leave\n", name);
return NULL;
}
int main(int argc, char *argv[])
{
pth_attr_t attr;
sigset_t sigs;
pth_init();
fprintf(stderr, "This is TEST_SIG, a Pth test using signals.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Hit CTRL-C three times to stop this test.\n");
fprintf(stderr, "But only after all threads were terminated.\n");
fprintf(stderr, "\n");
fprintf(stderr, "main: init\n");
/* block signals */
pth_sigmask(SIG_SETMASK, NULL, &sigs);
sigaddset(&sigs, SIGUSR1);
sigaddset(&sigs, SIGUSR2);
sigaddset(&sigs, SIGINT);
pth_sigmask(SIG_SETMASK, &sigs, NULL);
/* spawn childs */
attr = pth_attr_new();
pth_attr_set(attr, PTH_ATTR_NAME, "child1");
child1 = pth_spawn(attr, child, (void *)"child1");
pth_attr_set(attr, PTH_ATTR_NAME, "child2");
child2 = pth_spawn(attr, child, (void *)"child2");
pth_attr_set(attr, PTH_ATTR_NAME, "inthandler");
pth_spawn(attr, inthandler, (void *)"inthandler");
pth_attr_destroy(attr);
/* wait until childs are finished */
while (pth_join(NULL, NULL));
fprintf(stderr, "main: exit\n");
pth_kill();
return 0;
}