Receiving SIGINT and exception Handles in Linux

java 8 signal handling
linux signals
java handle ctrl-c

Let's say we have a program in C that uses the sleep() function

The program executes and goes to sleep. Then we type Ctrl+C to send a SIGINT signal to the process.

We know that the default action upon receipt of a SIGINT is to terminate the process, we also know that the sleep() function resume the process whenever the sleeping process receives a signal.

And my textbook says in order to allow sleep() function to return, we must install a SIGINT handler like this:

void handler(int sig){
    return; /* Catch the signal and return */
int main(int argc, char **argv) {
   if (signal(SIGINT, handler) == SIG_ERR) /* Install SIGINT handler */
      unix_error("signal error\n");

Althouth the code seems to be straightforward, I still have questions if I want to dig deeper:

Background: When the process is sleeping and we type Ctrl+C to send SIGINT

Q1-My understanding is, Kernel sends SIGINT to the process by updating the SIGINT's corresponging pending bit in the pend bit vector, is my understanding correct?

Q2-The processor detects the existance of SIGINT, but since we overwrite the handler to make it return in stead of terminating the process, so our handler get executed, and then Kernel clears SIGINT's corresponging pending bit, is my understanding correct?

Q3- Since SIGINT's corresponging pending bit is cleared, then how can sleep() function gets return? I think it should be in sleep still because in theory, sleep() function has no way of knowing the existance of SIGINT(has been cleared)

The Linux Signals Handling Model, but you can change that to any other character with the stty command. SIGTERM is not linked to an interrupt character but is just the signal sent by default by the kill command. Overwriting signal handler for one of the exception signals, will cause your program to ignore this signal and an exception that has caused the signal. This is something that you don’t want to do. In case you still want to handle exception signals, read my How to handle SIGSEGV, but also generate a core dump article.

in bash does control-c = SIGINT or SIGTERM?, The default action of SIGINT is to terminate the process according to the documentation (taken here from signal(7) on OpenBSD). These signals  Note however that SIGINT handling is in practice very complicated: shells and other processes will routinely have SIGINT handlers (otherwise the shell would exit, which could be bad), pressing control+c may not send a signal to the foreground process group (a program could put the terminal into raw mode with e.g. the ncurses cbreak() call and

Your understanding is correct.

Think about it. The process is blocked in the kernel. We need to return to user space to run the handler. How can we do that without interrupting whatever blocking kernel call was running? We only have one process/thread context to work with here. The process can't be both sleeping and running a signal handler.

The sequence is:

  1. Process blocks in some blocking kernel call.
  2. Signal is sent to it.
  3. Bit is set, process is made ready-to-run.
  4. Process resumes running in kernel mode, checks for pending non-blocked signals.
  5. Signal dispatcher is invoked.
  6. Process context is modified to execute signal handler upon resumption.
  7. Process is resumed in user space
  8. Signal handler runs.
  9. Signal handler returns.
  10. Kernel is invoked by end of signal handler.
  11. Kernel makes decision whether to resume system call or return interruption error.

Signal (IPC), how signals and exceptions are handled by the Java HotSpot Virtual Machine. the signal chaining facility, available on the Oracle Solaris, Linux, and macOS execute if the process receives a SIGQUIT , SIGTERM , SIGINT , or SIGHUP . Example C Program to Catch a Signal. Most of the Linux users use the key combination Ctr+C to terminate processes in Linux. Have you ever thought of what goes behind this. Well, whenever ctrl+c is pressed, a signal SIGINT is sent to the process. The default action of this signal is to terminate the process. But this signal can also be handled.

Miscellaneous Signals (The GNU C Library), What are the ways to receive notifications from operating system about events that occur. For instance, SIGINT default handler will terminate your application. Overwriting signal handler for one of the exception signals, will  There is a default behavior for some (i.e. a process is terminated when it receives an inturrupt SIGINT signal by pressing keystrokes ctrl-C) but this tutorial shows how to handle the signal by defining callback functions to manage the signal. Where possible, this allows one to close files and perform operations and react in a manner defined by

After Receiving an signal, which exception handler invoked first , Signals are a limited form of inter-process communication (IPC), typically used in Unix, If the process has previously registered a signal handler, that routine is executed. SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process An exception from this rule is while init is ptraced on Linux. 7 Handle Signals and Exceptions This chapter provides information about how signals and exceptions are handled by the Java HotSpot Virtual Machine. It also describes the signal chaining facility, available on the Oracle Solaris, Linux, and macOS operating systems, which facilitates writing applications that must install their own signal handlers.

Handle Signals and Exceptions, We will present the practical aspect of signal handling using C SIGINT) printf("​received SIGINT\n"); } int main(void) { if (signal(SIGINT,  The first three signals listed in Table 1—SIGHUP, SIGINT and SIGQUIT—are generated by a keyboard entry from the controlling terminal (SIGINT and SIGHUP) or if the control terminal becomes disconnected (SIGHUP—use of the nohup command makes processes “immune” from hangups by setting the disposition of SIGHUP to Ignore).