Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* sig4.c - an example from the Solaris 2 manual.
- The following sample C code creates a thread (sigint) to handle the
- receipt of the SIGINT signal.
- In Solaris2 compile with -D_POSIX_PTHREAD_SEMANTICS switch.
- */
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <signal.h>
- static void *threadTwo(void *);
- static void *threadThree(void *);
- static void *sigint(void *);
- sigset_t signalSet;
- static void *threadTwo(void *arg)
- {
- printf("hello world, from threadTwo [tid: %lu]\n",
- (unsigned long)pthread_self());
- while(1)
- {
- printf(" . ");
- usleep(1000);
- }
- pthread_exit((void *)0);
- }
- void *sigint(void *arg)
- {
- int sig;
- int err;
- printf("thread sigint [tid: %lu] awaiting SIGINT\n",
- (unsigned long)pthread_self());
- /*
- * use POSIX sigwait() -- 2 args: signal set, signum
- */
- while(1){
- err = sigwait(&signalSet, &sig);
- //sigemptyset(&signalSet);
- // sets = pthread_sigmask(SIG_SETMASK, &signalSet, NULL);
- printf("received signal is %d \n",sig );
- /* test for SIGINT; could catch other signals */
- if (sig == SIGINT)
- {
- printf("\nSIGINT signal %d caught by sigint thread [tid: %lu]\n",
- sig, (unsigned long)pthread_self());
- exit(0);
- pthread_exit((void *)0);
- }
- }
- }
- void error(char *message)
- {
- perror(message);
- kill(0, SIGTERM);
- exit(EXIT_FAILURE);
- }
- int main(void)
- {
- pthread_t t1;
- pthread_t t2;
- pthread_t t3;
- sigfillset(&signalSet);
- /*
- * Block signals in initial thread. New threads will
- * inherit this signal mask.
- */
- sigprocmask(SIG_SETMASK, &signalSet, NULL);
- //pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
- if (pthread_create(&t1, NULL, sigint, NULL))
- error("t1");
- printf("Creating threads\n");
- printf("##################\n");
- printf("press CTRL-C to deliver SIGINT to sigint thread\n");
- printf("##################\n");
- while(1)
- {
- printf(" . ");
- usleep(10000);
- }
- if (pthread_join(t1, NULL))
- perror("join");
- printf("Bye!\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement