When does a process handle a signal

2019-04-16 08:55发布

I want to know when does a linux process handles the signal. Assuming that the process has installed the signal handler for a signal, I wanted to know when would the process's normal execution flow be interrupted and signal handler called.

According to http://www.tldp.org/LDP/tlk/ipc/ipc.html, the process would handle the signal when it exits from a system call. This would mean that a normal instruction like a = b+c (or its equivalent machine code) would not be interrupted because of signal.

Also, there are system calls which would get interrupted (and fail with EINTR or get restarted) upon receiving a signal. This means that signal is processed even before the system call completes. This behaviour seems to b conflicting with what I have mentioned in the previous paragraph.

So, I am not clear as to when is the signal processed and in which process states would it be handled by the process. Can it be interrupted

  1. Anytime it enters from kernel space to user space, or
  2. Anytime it is in user space, or
  3. Anytime the process is scheduled for execution by the scheduler

Thanks!

标签: linux signals
2条回答
疯言疯语
2楼-- · 2019-04-16 09:02

A lot of the semantics of signal handling are documented (for Linux, anyway - other OSes probably have similar, but not necessarily in the same spot) in the section 7 signal manual page, which, if installed on your system, can be accessed like this:

man 7 signal

If manual pages are not installed, online copies are pretty easy to find.

查看更多
神经病院院长
3楼-- · 2019-04-16 09:20

According to http://www.tldp.org/LDP/tlk/ipc/ipc.html, the process would handle the signal when it exits from a system call. This would mean that a normal instruction like a = b+c (or its equivalent machine code) would not be interrupted because of signal.

Well, if that were the case, a CPU-intensive process would not obey the process scheduler. The scheduler, in fact, can interrupt a process at any point of time when its time quantum has elapsed. Unless it is a FIFO real-time process.

A more correct definition: One point when a signal is delivered to the process is when the control flow leaves the kernel mode to resume executing user-mode code. That doesn't necessarily involve a system call.

查看更多
登录 后发表回答