Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are asynchronous signals handled in Linux?

This seems like a silly question, but I can't find the answer to it anywhere I look. I know that in UNIX, signals are handled asynchronously. If I write a function that handles a signal, where is that function run? Is a new thread spawned? Is an existing thread interrupted somehow? Or is this handled in a system thread like asynchronous I/O is?

like image 975
Jason Baker Avatar asked May 06 '11 01:05

Jason Baker


2 Answers

A signal function is executed as if a thread in the process has been interrupted. That is, the signal handler is called using the signaled thread and the stack is rearranged so that when the signal handler returns the thread continues execution. No new threads are introduced.

like image 188
Richard Pennington Avatar answered Sep 30 '22 08:09

Richard Pennington


An existing process thread is interrupted until the function returns. There are serious restrictions on what it can safely do to ensure it doesn't corrupt state of function calls the thread was in the middle of - specifically, any functions it calls that the thread may have already been calling must be async reentrant. See the man pages e.g. signal, sigaction for further details or ask more specific questions as you like.

like image 28
Tony Delroy Avatar answered Sep 30 '22 08:09

Tony Delroy