Live data from Hacker News

How Not to Write a Signal Handler

741mhz.com

51–52 of 52 posts

Re: How Not to Write a Signal Handler

#51
post #38

Why does the signal handler have to interrupt a thread? Why doesn't the OS just create a new thread to run the signal handler in? This would avoid this whole reentrancy problem and make the concurrency explicit.

Threads are fairly expensive - you have to allocate and map a new stack, as well as a bunch of bookkeeping data structures in the kernel. While the kernel could of course start an auxillary thread at program initialisation, and re-use it for all signals, this does complicate the runtime environment a bit. And what happens if the signal-handling-thread triggers a signal?

I suppose the Unix tradition encourages giving the user the ability to centralise signal handling in a thread if he wishes (using kpoll or signalfd or whatever you want), but not forcing any overhead upon the program.

Post reply on HN