views:

79

answers:

2

If you have a multithreaded program (Linux 2.26 kernel), and one thread does something that causes a segfault, will the other threads still be scheduled to run? How are the other threads terminated? Can someone explain the process shutdown procedure with regard to multithreaded programs?

+4  A: 

Will the other thread still be scheduled to run?

No. The SEGV is a process-level issue. Unless you've handled the SEGV (which is almost always a bad idea) your whole process will exit, and all threads with it.

I suspect that the other threads aren't handled very nicely. If the handler calls exit() or _exit() thread cleanup handlers won't get called. This may be a good thing if your program is severely corrupted, it's going to be hard to trust much of anything after a seg fault.

One note from the signal man page:

According to POSIX, the behaviour of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by the kill(2) or the raise(3) functions.

After a segfault you really don't want to be doing anything other than getting the heck out of that program.

Paul Rubel
+2  A: 

When a fatal signal is delivered to a thread, either the do_coredump() or the do_group_exit() function is called. do_group_exit() sets the thread group exit code and then signals all the other threads in the thread group to exit with zap_other_threads(), before exiting the current thread. (do_coredump() calls coredump_wait() which similarly calls zap_threads()).

zap_other_threads() posts a SIGKILL for every other thread in the thread group and wakes it up with signal_wake_up(). signal_wake_up() calls kick_process(), which will boot the thread into kernel mode so that it can recieve the signal, using an IPI1 if necessary (eg. if it's executing on another CPU).


1. Inter-Processor Interrupt

caf
@caf - Is this in get_signal_to_deliver? It looks to me like do_group_exit is called after do_coredump (in the coredump case). Can other threads continue to run while the coredump is being taken? It's not clear to me what zap_threads does.
@abellia: `do_coredump()` never returns, so either it *or* `do_group_exit()` is called. `zap_threads()` is very similar to `zap_other_threads()` - it also posts a `SIGKILL` and wakes up the target. `do_coredump()` kills all the threads and waits for them to exit, then actually writes the core file.
caf