Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- #include <pthread.h>
- pthread_t tid[2];
- pthread_mutex_t startlock;
- pthread_cond_t startcond;
- #define START 1
- #define STOP 2
- static void usr2 (int signum, siginfo_t *siginfo, void *context)
- {
- sigset_t intmask;
- printf("T [%lu]: got signal%d\n", pthread_self(), signum);
- sigemptyset(&intmask);
- sigaddset(&intmask, signum);
- sigprocmask(SIG_UNBLOCK, &intmask, NULL);
- if(siginfo->si_value.sival_int == STOP) {
- pthread_mutex_init(&startlock, NULL);
- pthread_cond_init(&startcond, NULL);
- pthread_mutex_lock(&startlock);
- pthread_cond_wait(&startcond, &startlock);
- pthread_mutex_unlock(&startlock);
- } else if(siginfo->si_value.sival_int == START) {
- pthread_cond_signal(&startcond);
- } else
- printf("unknown action: %d\n", siginfo->si_value.sival_int);
- }
- static void * t1(void *arg)
- {
- for(int i = 0; ; i++) {
- printf("T1[%lu]: %d\n", pthread_self(), i);
- sleep(1);
- }
- return NULL;
- }
- static void * t2(void *arg)
- {
- for(int i = 0; ; i++) {
- printf("T2[%lu]: %d\n", pthread_self(), i);
- sleep(1);
- if(i == 1) {
- printf("T2[%lu]: sending signal STOP\n", pthread_self());
- pthread_sigqueue(tid[0], SIGUSR2, (union sigval)STOP);
- } else if(i == 5) {
- printf("T2[%lu]: sending signal START\n", pthread_self());
- pthread_sigqueue(tid[0], SIGUSR2, (union sigval)START);
- } else if(i == 8) {
- printf("T2[%lu]: sending signal RANDOM\n", pthread_self());
- pthread_sigqueue(tid[0], SIGUSR2, (union sigval)(int)(tid[0] * tid[1] >> 16 & 0xffff));
- }
- }
- return NULL;
- }
- struct sigaction act = {
- .sa_sigaction = usr2,
- .sa_flags = SA_SIGINFO,
- };
- int main(int argc, char *argv[])
- {
- sigaction(SIGUSR2, &act, NULL);
- pthread_create(tid + 0, NULL, t1, 0);
- pthread_create(tid + 1, NULL, t2, 0);
- while(1)
- sleep(10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement