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
|
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<signal.h>
int global;
void *handleInput(void*);
int main() {
pthread_t inputHandler; //basically our thread ID
//create thread
if(pthread_create(&inputHandler, NULL, handleInput, NULL))
perror("Failed to spawn inputHandler thread");
global = 1;
sleep(1);
printf("Global = %i\n", global);
//send SIGUSR1 signal to our input handler
pthread_kill(inputHandler, SIGUSR1);
//wait for inputHandler to return
pthread_join(inputHandler, NULL);
printf("Global = %i\n", global);
pthread_detach(inputHandler);
return 0;
}
//The thread will jump into this function
void *handleInput(void *lol) {
int signo;
//Let's add the signal SIGUSR1 to the set of signals we're waiting for
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
//block until SIGUSR1 is received
while(!sigwait(&set, &signo)) {
global=5;
return;
}
}
/*Output
john@lenin:~/school/cs4061/sandbox$ gcc threadsig.c -lpthread
john@lenin:~/school/cs4061/sandbox$ ./a.out
Global = 1
Global = 5
john@lenin:~/school/cs4061/sandbox$
*/
|