sewardj | 811d141 | 2002-05-13 00:38:52 +0000 | [diff] [blame] | 1 | /******************************************************** |
| 2 | * An example source module to accompany... |
| 3 | * |
| 4 | * "Using POSIX Threads: Programming with Pthreads" |
| 5 | * by Brad nichols, Dick Buttlar, Jackie Farrell |
| 6 | * O'Reilly & Associates, Inc. |
| 7 | * |
| 8 | ******************************************************** |
| 9 | * stat_sigwait.c |
| 10 | * |
| 11 | * Simple example of pthreads and signals. |
| 12 | */ |
| 13 | |
| 14 | #include <stdlib.h> |
| 15 | #include <stdio.h> |
| 16 | #include <unistd.h> |
| 17 | #include <signal.h> |
| 18 | #include <time.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #include <pthread.h> |
| 22 | |
| 23 | #define MAX_NUM_THREADS 10 |
| 24 | |
| 25 | pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER; |
| 26 | int mean, samples, total; |
| 27 | |
| 28 | void *report_stats(void *p) |
| 29 | { |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 30 | int caught, i; |
sewardj | 811d141 | 2002-05-13 00:38:52 +0000 | [diff] [blame] | 31 | sigset_t sigs_to_catch; |
| 32 | |
| 33 | /* Identify our thread */ |
| 34 | printf("\nreport_stats() started.\n"); |
| 35 | |
| 36 | /* |
| 37 | * We inherited a thread sigmask with all the signals |
| 38 | * blocked. So, we can wait on whatever signals we're |
| 39 | * interested in and (as long as no other thread waits |
| 40 | * for them) we'll be sure return from sigwait() to |
| 41 | * handle it. |
| 42 | */ |
| 43 | |
sewardj | 811d141 | 2002-05-13 00:38:52 +0000 | [diff] [blame] | 44 | /* set this thread's signal mask to block out SIGUSR1 */ |
| 45 | sigemptyset(&sigs_to_catch); |
| 46 | sigaddset(&sigs_to_catch, SIGUSR1); |
| 47 | |
| 48 | for (;;) { |
| 49 | sigwait(&sigs_to_catch, &caught); |
| 50 | |
| 51 | pthread_mutex_lock(&stats_lock); |
| 52 | mean = total/samples; |
| 53 | printf("\nreport_stats(): mean = %d, samples = %d\n", mean, samples); |
| 54 | pthread_mutex_unlock(&stats_lock); |
sewardj | 3a951cf | 2002-05-15 22:25:47 +0000 | [diff] [blame] | 55 | |
| 56 | /* Delay for a while so it's obvious whether or not SIGUSR1 is |
| 57 | still blocked here (it should be). */ |
| 58 | // for (i = 0; i < 100000; i++) ; |
| 59 | |
sewardj | 811d141 | 2002-05-13 00:38:52 +0000 | [diff] [blame] | 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | /* |
| 64 | * worker_thread -- |
| 65 | * |
| 66 | * Don't read too much into what this thread does. It's |
| 67 | * a very simpleminded example. The only interesting thing |
| 68 | * it does is write to the global statistics data-- which |
| 69 | * means the thread processing the signal has to protect |
| 70 | * against simultaneous access. |
| 71 | */ |
| 72 | void *worker_thread(void *p) |
| 73 | { |
| 74 | time_t now; |
| 75 | for (;;) { |
| 76 | |
| 77 | sleep(1 + (*(int*)p) % 2 ); |
| 78 | |
| 79 | now = time(NULL); |
| 80 | |
| 81 | pthread_mutex_lock(&stats_lock); |
| 82 | total+=((int)now)%60; /* probably not the safest thing to do but |
| 83 | it's just an example */ |
| 84 | samples++; |
| 85 | pthread_mutex_unlock(&stats_lock); |
| 86 | } |
| 87 | /* Won't get here. */ |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | extern int |
| 92 | main(void) |
| 93 | { |
| 94 | int i; |
| 95 | pthread_t threads[MAX_NUM_THREADS]; |
| 96 | int num_threads = 0; |
| 97 | sigset_t sigs_to_block; |
| 98 | |
| 99 | |
| 100 | /* Identify our thread */ |
| 101 | printf("main() (pid %d) running in thread 0x%x\n", |
| 102 | getpid(), (int)pthread_self()); |
| 103 | |
| 104 | /* |
| 105 | * Set this thread's signal mask to block SIGUSR1 |
| 106 | * Other threads will inherit the mask |
| 107 | */ |
| 108 | sigemptyset(&sigs_to_block); |
| 109 | sigaddset(&sigs_to_block, SIGUSR1); |
| 110 | pthread_sigmask(SIG_BLOCK, &sigs_to_block, NULL); |
| 111 | |
| 112 | /* spawn statistics reporting thread */ |
| 113 | pthread_create(&threads[num_threads++], |
| 114 | NULL, |
| 115 | report_stats, |
| 116 | NULL); |
| 117 | |
| 118 | /* spawn the threads */ |
| 119 | for (i=num_threads; i<MAX_NUM_THREADS; i++) { |
| 120 | pthread_create(&threads[num_threads++], |
| 121 | NULL, |
| 122 | worker_thread, |
| 123 | &i); |
| 124 | } |
| 125 | |
| 126 | printf("main()\t\t\t\t%d threads created\n",num_threads); |
| 127 | |
| 128 | /* wait until all threads have finished */ |
| 129 | for (i = 0; i < num_threads; i++) { |
| 130 | pthread_join(threads[i], NULL); |
| 131 | printf("main()\t\tjoined to thread %d \n", i); |
| 132 | } |
| 133 | |
| 134 | printf("main()\t\tall %d threads have finished. \n", num_threads); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |