Zurück zur SP2 Übersicht
threads.c
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void *threadfunc1(void *args) {
while (1) {
sleep(1);
}
}
void *threadfunc2(void *args) {
sigset_t s;
sigemptyset(&s);
sigaddset(&s, SIGUSR2);
pthread_sigmask(SIG_BLOCK, &s, NULL);
while (1) {
sleep(1);
}
}
void signalhandler(int arg) {
pthread_t t = pthread_self();
fprintf(stderr, "Signal in Thread %d\n", t);
sleep(1);
}
int main(void) {
struct sigaction sa = {
.sa_handler = signalhandler,
.sa_flags = SA_RESTART,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigset_t s;
sigfillset(&s);
pthread_sigmask(SIG_UNBLOCK, &s, NULL);
pthread_t p;
pthread_create(&p, NULL, threadfunc1, NULL);
pthread_create(&p, NULL, threadfunc2, NULL);
pthread_sigmask(SIG_BLOCK, &s, NULL);
while(1) {
sleep(1);
}
}