/* $Id: tidminusone.c,v 1.6 2012/09/21 12:38:40 kostik Exp $ */ #include #include #include #include #include #include #include #include #include static long tid; static volatile int terminate; static void siginfo_handler(int signo __unused) { } static void * dummy(void *arg __unused) { long id; if (thr_self(&id) == -1) err(1, "thr_self"); if ((int)id == -1) errx(1, "tid == -1"); tid = id; return (NULL); } static void * worker(void *arg __unused) { pthread_t thr; void *res; int error; for (; !terminate;) { error = pthread_create(&thr, NULL, dummy, NULL); if (error != 0) errc(1, error, "pthread_create"); error = pthread_join(thr, &res); if (error != 0) errc(1, error, "pthread_join"); } return (NULL); } static void usage(void) { fprintf(stderr, "usage: tidminusone [-n ]\n"); exit(2); } int main(int argc, char *argv[]) { pthread_t *pthrs; sigset_t sigs; struct sigaction sa; int ch, error, i, nthreads, sig; nthreads = 1; while ((ch = getopt(argc, argv, "n:")) != -1) { switch (ch) { case 'n': nthreads = atoi(optarg); printf("Using %d threads\n", nthreads); break; case '?': default: usage(); } } bzero(&sa, sizeof(sa)); sa.sa_handler = siginfo_handler; if (sigaction(SIGINFO, &sa, NULL) == -1) err(1, "sigaction"); sigemptyset(&sigs); sigaddset(&sigs, SIGINFO); sigaddset(&sigs, SIGTERM); sigaddset(&sigs, SIGINT); error = pthread_sigmask(SIG_BLOCK, &sigs, NULL); if (error != 0) errc(1, error, "pthread_sigmask"); pthrs = calloc(nthreads, sizeof(pthread_t)); for (i = 0; i < nthreads; i++) { error = pthread_create(&pthrs[i], NULL, worker, NULL); if (error != 0) errc(1, error, "pthread_create"); } for (; !terminate;) { error = sigwait(&sigs, &sig); if (error != 0) errc(1, error, "sigwait"); switch (sig) { case SIGINFO: printf("Tid 0x%lx\n", tid); break; default: terminate = 1; break; } } for (i = 0; i < nthreads; i++) { error = pthread_join(pthrs[i], NULL); if (error != 0) errc(1, error, "pthread_join"); } return (0); }