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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2003 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <signal.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "basis_types.h"
#undef FALSE
#undef TRUE
enum { FALSE = 0, TRUE = 1, NUM_THRDS = 3 };
typedef struct {
pthread_mutex_t mtx;
pthread_cond_t cndvar;
int quit; /* should we quit */
int cntr; /* number of threads (except main thread) */
} control_block_t;
static control_block_t cb = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 };
static void incr_thrd_cnt(void);
static void* signal_emitter(void*);
static void* signal_waiter(void*);
static void ignore_signals(void);
static void reap_thrds(void);
static int should_quit(void);
/****** /utilbin/tst_pthread_signals/main() ************************************
* NAME
* main() -- test pthread signal handling
*
* SYNOPSIS
* int main(int argc, char* argv[])
*
* FUNCTION
* Test pthread signal handling. Create a signal handling thread and multiple
* threads which do emit signals. Shutdown if a 'SIGINT' is received.
*
* Please note that all signals are send to the process, not a particular
* thread.
*
* INPUTS
* int argc - not used
* char* argv[] - not used
*
* RESULT
* int - 0
*
*******************************************************************************/
int main(int argc, char* argv[])
{
sigset_t sig_set;
pthread_t id[NUM_THRDS];
int i;
sigfillset(&sig_set);
pthread_sigmask(SIG_SETMASK, &sig_set, NULL);
printf("main: creating threads\n");
pthread_create(&(id[0]), NULL, signal_waiter, NULL);
incr_thrd_cnt();
for (i = 1; i < NUM_THRDS; i++) {
pthread_create(&(id[i]), NULL, signal_emitter, NULL);
incr_thrd_cnt();
}
printf("main: startig to join threads\n");
for (i = 0; i < NUM_THRDS; i++) {
pthread_join(id[i], NULL);
}
printf("main: all threads joined - quit\n");
return 0;
} /* main() */
/****** utilbin/tst_pthread_signals/incr_thrd_cnt() ***************************
* NAME
* incr_thrd_cnt() -- increment thread count
*
* SYNOPSIS
* static void incr_thrd_cnt(void)
*
* FUNCTION
* Increment number of active threads. The so called 'main thread' is NOT
* counted. The unmber of active threads is used to coordinate the shutdown
* process.
*
* INPUTS
* void - none
*
* RESULT
* void - none
*
* NOTES
* MT-NOTE: incr_thrd_cnt() is MT safe.
*
* SEE ALSO
* utilbin/tst_pthread_signals/should_quit()
* utilbin/tst_pthread_signals/reap_thrds()
*
*******************************************************************************/
static void incr_thrd_cnt(void)
{
pthread_mutex_lock(&cb.mtx);
cb.cntr++;
pthread_mutex_unlock(&cb.mtx);
return;
} /* incr_thrd_cnt() */
/****** utilbin/tst_pthread_signals/signal_emitter() ***************************
* NAME
* signal_emitter() -- emit signals
*
* SYNOPSIS
* static void* signal_emitter(void* anArg)
*
* FUNCTION
* Emit signals, randomly selected from a fixed set of signals. Enter
* infinite loop. Select signal. Send signal to the process. After
* each iteration check for termination.
*
* INPUTS
* void* anArg - not used
*
* RESULT
* void* - NULL
*
* NOTES
* MT-NOTE: signal_emitter() is a thread function.
*
*******************************************************************************/
static void* signal_emitter(void* anArg)
{
int sig[3] = {SIGPIPE, SIGUSR1, SIGUSR2};
unsigned int i = (unsigned int)pthread_self(); /* seed */
bool done = false;
while (!done) {
int j = (rand_r(&i) % 3);
if (should_quit() == TRUE) {
printf("signal_emitter: will terminate\n");
done = true;
break;
}
printf("signal_emitter %d will raise: %d\n", (int)pthread_self(), sig[j]);
kill(getpid(), sig[j]);
sleep(4);
}
return NULL;
} /* signal_emitter() */
/****** utilbin/tst_pthread_signals/should_quit() ******************************
* NAME
* should_quit() -- should thread quit?
*
* SYNOPSIS
* static int should_quit(void)
*
* FUNCTION
* Determine if thread should quit. Lock control block mutex. Inspect quit
* flag. Decrement thread counter. Signal control block condition variable
* waiters. Unlock control block mutex.
*
* INPUTS
* void - none
*
* RESULT
* FALSE - continue
* TRUE - quit
*
* NOTES
* MT-NOTE: should_quit() is MT safe.
*
*******************************************************************************/
static int should_quit(void)
{
int res = FALSE;
printf("should_quit: check termination\n");
pthread_mutex_lock(&cb.mtx);
if (cb.quit == TRUE) {
printf("should_quit: do quit\n");
cb.cntr--;
pthread_cond_signal(&cb.cndvar);
res = TRUE;
}
pthread_mutex_unlock(&cb.mtx);
return res;
} /* should_quit() */
/****** utilbin/tst_pthread_signals/signal_waiter() ****************************
* NAME
* signal_waiter() -- wait for signals
*
* SYNOPSIS
* static void* signal_waiter(void* anArg)
*
* FUNCTION
* Wait for signals. Establish recognized signal set. Enter infinite loop.
* Wait for signal. Announce signal received. If signal is 'SIGINT', wait
* for all other threads (except main thread) to terminate. Terminate.
*
* INPUTS
* void* anArg - not used
*
* RESULT
* void* - NULL
*
* NOTES
* MT-NOTE: signal_waiter() is a thread function.
*
*******************************************************************************/
static void* signal_waiter(void* anArg)
{
sigset_t set;
int num;
bool exit = false;
printf("signal_waiter started\n");
ignore_signals();
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGALRM);
sigaddset(&set, SIGPIPE);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
while (!exit)
{
printf("signal_waiter is waiting for signal\n");
sigwait(&set, &num);
switch (num) {
case SIGINT:
printf("signal_waiter: got signal SIGINT\n");
reap_thrds();
exit = true;
break;
case SIGALRM:
printf("signal_waiter: got signal SIGALRM\n");
break;
case SIGPIPE:
printf("signal_waiter: got signal SIGPIPE\n");
break;
case SIGUSR1:
printf("signal_waiter: got signal SIGUSR1\n");
break;
case SIGUSR2:
printf("signal_waiter: got signal SIGUSR2\n");
break;
default:
printf("signal_waiter: got signal %d\n", num);
break;
}
}
return NULL;
} /* signal_waiter() */
/****** utilbin/tst_pthread_signals/ignore_signals() ***************************
* NAME
* ignore_signals() -- ignore signals
*
* SYNOPSIS
* static void ignore_signals(void)
*
* FUNCTION
* Ignore all signals, which 'signal_waiter()' should not recognize.
*
* INPUTS
* void - none
*
* RESULT
* void - none
*
* NOTES
* MT-NOTE: ignore_signals() is NOT MT safe.
*
*******************************************************************************/
static void ignore_signals(void)
{
struct sigaction act;
act.sa_handler = SIG_IGN;
sigaction(SIGABRT, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
sigaction(SIGCONT, &act, NULL);
sigaction(SIGHUP, &act, NULL);
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGTSTP, &act, NULL);
sigaction(SIGTTIN, &act, NULL);
sigaction(SIGTTOU, &act, NULL);
sigaction(SIGURG, &act, NULL);
#if !(defined(NECSX4) || defined(NECSX5))
sigaction(SIGVTALRM, &act, NULL);
#endif
#if !defined(DARWIN) && !defined(FREEBSD) && !defined(NETBSD)
sigaction(SIGPOLL, &act, NULL);
#endif
return;
} /* ignore_signals() */
/****** utilbin/tst_pthread_signals/reap_thrds() *******************************
* NAME
* reap_thrds() -- reap threads
*
* SYNOPSIS
* static void reap_thrds(void)
*
* FUNCTION
* Reap threads. Lock control block mutex. Set quit flag to true. If there
* is more than one remaining, wait on control block condition variable.
* Unlock control block mutex.
*
* INPUTS
* void - none
*
* RESULT
* void - none
*
* NOTES
* MT-NOTE: reap_thrds() must be called from within a single thread only.
*
*******************************************************************************/
static void reap_thrds(void)
{
printf("reap_thrds: start to reap threads\n");
pthread_mutex_lock(&cb.mtx);
cb.quit = TRUE;
while(cb.cntr > 1) {
pthread_cond_wait(&cb.cndvar, &cb.mtx);
}
printf("reap_thrds: all threads harvested\n");
pthread_mutex_unlock(&cb.mtx);
return;
} /* reap_thrds() */
|