2

When exactly signal will start execution in unix ?Does the signal will be processed when system turns into kernel mode? or immediately when it is receives signal? I assume it will be processed immediate when it receives.

jeevan kishore
  • 320
  • 1
  • 4
  • 16
  • 1
    Possible duplicate of [How are signals handled in Unix?](http://stackoverflow.com/questions/34454737/how-are-signals-handled-in-unix) – Mohammad Yusuf Dec 18 '16 at 02:09

2 Answers2

3

A signal is the Unix mechanism for allowing a user space process to receive asynchronous notifications. As such, signals are always "delivered by" the kernel. And hence, it is impossible for a signal to be delivered without a transition into kernel mode. Therefore it doesn't make sense to talk of a process "receiving" a signal (or sending one) without the involvement of the kernel.

Signals can be generated in different ways.

  • They can be generated by a device driver within the kernel (for example, tty driver in response to the interrupt, kill, or stop keys or in response to input or output by a backgrounded process).
  • They can be generated by the kernel in response to an emergent out-of-memory condition.
  • They can be generated by a processor exception in response to something the process itself does during its execution (illegal instruction, divide by zero, reference an illegal address).
  • They can be generated directly by another process (or by the receiving process itself) via kill(2).
  • SIGPIPE can be generated as a result of writing to a pipe that has no reader.

But in every case, the signal is delivered to the receiving process by the kernel and hence through a kernel-mode transition.

The kernel might need to force that transition -- pre-empt the receiving process -- in order to deliver the signal (for example, in the case of a CPU-bound process running on processor A being sent a signal by a different process running on processor B).

In some cases, the signal may be handled for the process by the kernel itself (for example, with SIGKILL -- or several others when no signal handler is configured).

Actually invoking a process' signal handler is done by manipulating the process' user space stack so that the signal handler is invoked on return from kernel-mode and then, if/when the signal handler procedure returns, the originally executing code can be resumed.

As to when it is processed, that is subject to a number of different factors.

  • There are operating system (i.e. kernel) operations that are never interrupted by signals (these are generally relatively short duration operations), in which case the signal will be processed after their completion.
  • The process may have temporarily blocked signal delivery, in which case the signal will be "pending" until it is unblocked.
  • The process could be swapped out or non-runnable for any of a number of reasons -- in which case, its signal handler cannot be invoked until the process is runnable again.
  • Resuming the process in order to deliver the signal might be delayed by interrupts and higher priority tasks.
Gil Hamilton
  • 11,973
  • 28
  • 51
0

A signal will be immediately detected by the process which receives it. Depending on the signal type, the process might treat it with the default handler, might ignore it or might execute a custom handler. It depends a lot on what the process is and what signal it receives. The exception is the kill signal (9) which is treated by the kernel and terminates the execution of the process which was supposed to receive it.

czvtools
  • 591
  • 2
  • 7
  • Note that SIGSTOP also can't be handled by the process, indeed instructing the kernel to stop the process until a SIGCONT is received. – Daniel Porteous Dec 18 '16 at 09:10