I was reading The Design Of UNIX OS and had a doubt.

A signal handling function can be specified in the signal() call. The handler is supposed to execute when the process receives that particular signal. An algorithm known as psig runs to handle signals if they are received by a process.

In case of a user defined handler being specified, the psig algorithm modifies the user level context (i.e. pushes a frame at the top of the stack with the stack pointer pointing to the top of the stack) This frame indicates the execution of the handling function.

If the above mentioned is true, the signal handling function should always be executed after a return from the signal() call (as the stack frame at the top of the stack will be seen by the kernel first). However, this is not true as i can specify a signal handler for a particular signal earlier in my code (lets say in main()) and send a signal at a later point of time in code. The signal handling function is only executed when the signal is sent (say by the kill() call) and not immediately after the signal() call.

Can anyone please clarify?


I assume by "The Design Of UNIX OS" you actually mean "The Design of the UNIX Operating System" by Maurice J. Bach.

My best guess at what you're running into — and guess I must, since you have given no page or section references — is section 7.2.1, spanning pages 203 to 204. He starts the section with two paragraphs describing how to set up a signal handler, then the third starts describing what the kernel does when it receives the signal your program asked it to trap.

The mere fact that one paragraph follows another doesn't tell you that the things described therein happen immediately after each other in time. A book's prose is not a computer algorithm. Bach is simply describing two separate things, without explicitly telling you that other things may happen in between.

So the answer is no, psig() does not run immediately after a signal(2) call completes. Bach didn't say it did. You just assumed that.

  • I didn't say that Bach said it. By combining everything i read, i inferred it. I guess you are right about psig. My question was that if the top of the stack is related to the signal handling function and suppose it is run at a later stage in time, how exactly does the kernel manage to execute other lines of code or for that matter functions or system calls when the current user level and register context refer to the signal handling function? Thanks for your reply. – Karan Oct 2 '12 at 9:32
  • The kernel doesn't modify the stack or program counter until the signal handler needs to execute. BTW, can you please verify that I have correctly guessed which pages you are talking about in the book? – Warren Young Oct 2 '12 at 9:57
  • Yes, you guessed it right. Page 204: In the pseudocode for psig, it clearly states that address of signal catcher is written into the PC of user saved register context. Moreover, just above it, it also talks about creating a user stack frame to mimic call to signal catcher function. – Karan Oct 2 '12 at 13:02
  • Once again, psig() is called when the signal is thrown, not when you call signal(). signal() just tells the kernel what you want psig() to do when the named signal is thrown. – Warren Young Oct 2 '12 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.