/* Frage: Welcher Thread bekommt SIGCHLD, wenn die Behandlungsfunktion * global installiert wird? */ #include #include #include #include #include #include #include void handler() { printf("handler: %lu\n", pthread_self()); exit(EXIT_SUCCESS); } void *thread(void *forkp) { if (forkp) { switch (fork()) { case -1: err(EXIT_FAILURE, "fork"); case 0: execlp("sleep", "sleep", "1", NULL); err(EXIT_FAILURE, "exec"); } printf("forking thread: %lu\n", pthread_self()); } else { printf("regular thread: %lu\n", pthread_self()); } for (;;) pause(); } int main() { sigset_t mask; sigemptyset(&mask); sigaction(SIGCHLD, &((struct sigaction) { .sa_handler = handler, .sa_mask = mask }), NULL); pthread_t tid; for (int i = 0; i < 9; i++) pthread_create(&tid, NULL, &thread, NULL); pthread_create(&tid, NULL, &thread, (void*) !NULL); for (;;) pause(); return 0; } /* ERGEBNIS * * $ ./sigchld-test * regular thread: 139999324866240 * regular thread: 139999314839232 * regular thread: 139999160567488 * regular thread: 139999304812224 * regular thread: 139999223740096 * regular thread: 139999213713088 * regular thread: 139999193659072 * regular thread: 139999203686080 * regular thread: 139999183632064 * forking thread: 139999173605056 * handler: 139999173605056 * * => Zumindest die Glibc stellt das Signal dem Thread zu, welches den * Prozess erstellt hat. */