Questions tagged [signals]

A signal is a notification to a process that an event occurred. Signals are sometimes described as software interrupts. Signals are analogous to hardware interrupts in that they interrupt the normal flow of execution of a program; in most cases, it is not possible to predict exactly when a signal will arrive. They are defined in the C standards and extended in POSIX, but many other programming languages/systems provide access to them as well.

Standards

These standards actually place requirements on the signal-handling facilities:

C++11 essentially says that you get the same facilities as in C, as long as you restrict signal handlers to the common subset of C and C++, and use C linkage for them. Quoting n3242 section 18.10 "Other runtime support" [support.runtime] (paragraph 8),

The common subset of the C and C++ languages consists of all declarations, definitions, and expressions that may appear in a well formed C++ program and also in a conforming C program. A POF (“plain old function”) is a function that uses only features from this common subset, and that does not directly or indirectly use any function that is not a POF, except that it may use functions defined in Clause 29 that are not member functions. All signal handlers shall have C linkage. A POF that could be used as a signal handler in a conforming C program does not produce undefined behavior when used as a signal handler in a C++ program. The behavior of any other function used as a signal handler in a C++ program is implementation-defined.

7326 questions
801
votes
13 answers

What killed my process and why?

My application runs as a background process on Linux. It is currently started at the command line in a Terminal window. Recently a user was executing the application for a while and it died mysteriously. The text: Killed was on the terminal. This…
sbq
  • 8,049
  • 3
  • 17
  • 5
689
votes
13 answers

How do I capture SIGINT in Python?

I'm working on a python script that starts several processes and database connections. Every now and then I want to kill the script with a Ctrl+C signal, and I'd like to do some cleanup. In Perl I'd do this: $SIG{'INT'} = 'exit_gracefully'; sub…
James Thompson
  • 46,512
  • 18
  • 65
  • 82
459
votes
34 answers

What's the best way to send a signal to all members of a process group?

I want to kill a whole process tree. What is the best way to do this using any common scripting languages? I am looking for a simple solution.
Adam Peck
  • 6,930
  • 3
  • 23
  • 27
296
votes
10 answers

How to prevent SIGPIPEs (or handle them properly)

I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply. The problem is that the client may have no interest in the answer and sometimes exits early. …
jkramer
  • 15,440
  • 5
  • 47
  • 48
282
votes
11 answers

Is it possible to capture a Ctrl+C signal (SIGINT) and run a cleanup function, in a "defer" fashion?

I want to capture the Ctrl+C (SIGINT) signal sent from the console and print out some partial run totals.
Sebastián Grignoli
  • 32,444
  • 17
  • 71
  • 86
205
votes
9 answers

Catch Ctrl-C in C

How does one catch Ctrl+C in C?
Feldor
  • 2,061
  • 2
  • 13
  • 4
164
votes
9 answers

What is the difference between sigaction and signal?

I was about to add an extra signal handler to an app we have here and I noticed that the author had used sigaction() to set up the other signal handlers. I was going to use signal(). To follow convention I should use sigaction() but if I was writing…
Matthew Smith
  • 6,165
  • 6
  • 34
  • 35
162
votes
4 answers

How can I catch a ctrl-c event?

How do I catch a Ctrl+C event in C++?
Scott
  • 5,135
  • 12
  • 57
  • 74
150
votes
2 answers

Signal handling with multiple threads in Linux

In Linux, what happens when a program (that possibly has multiple threads) receives a signal, like SIGTERM or SIGHUP? Which thread intercepts the signal? Can multiple threads get the same signal? Is there a special thread dedicated entirely to…
user500944
150
votes
6 answers

What does `kill -0 $pid` in a shell script do?

Basically, what signal does '0' represent, because here I see SIGNAL numbers starting from 1.
gjain
  • 4,468
  • 5
  • 39
  • 47
145
votes
5 answers

What is the difference between SIGSTOP and SIGTSTP?

Just wondering about the difference between SIGSTOP and SIGTSTP signals.
user1419715
  • 1,607
  • 2
  • 12
  • 8
115
votes
7 answers

In what order should I send signals to gracefully shutdown processes?

In a comment on this answer of another question, the commenter says: don’t use kill -9 unless absolutely necessary! SIGKILL can’t be trapped so the killed program can’t run any shutdown routines to e.g. erase temporary files. First try…
system PAUSE
  • 37,082
  • 20
  • 62
  • 59
109
votes
17 answers

Can I send a ctrl-C (SIGINT) to an application on Windows?

I have (in the past) written cross-platform (Windows/Unix) applications which, when started from the command line, handled a user-typed Ctrl-C combination in the same way (i.e. to terminate the application cleanly). Is it possible on Windows to send…
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
98
votes
8 answers

How to avoid using printf in a signal handler?

Since printf is not reentrant, it's not supposed to be safe to use it in a signal handler. But I've seen lots of example codes that uses printf this way. So my question is: when do we need to avoid using printf in a signal handler, and is there a…
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
97
votes
4 answers

How can I catch SIGSEGV (segmentation fault) and get a stack trace under JNI on Android?

I'm moving a project to the new Android Native Development Kit (i.e. JNI) and I'd like to catch SIGSEGV, should it occur (possibly also SIGILL, SIGABRT, SIGFPE) in order to present a nice crash reporting dialog, instead of (or before) what currently…
1
2 3
99 100