Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Ctrl-C and SIGINT?

I have been debugging a Python program which segfaults after receiving a KeyboardInterrupt exception. This is normally done by pressing Ctrl+C from the shell. To test if a particular code change fixed the bug, I had a small shell-script that sent SIGINT to the program at random time after start-up. The problem I have is that sending Ctrl+C seems to have a different effect on the program than sending the signal SIGINT and is thus not causing the bug to appear, so I quite wonder what the difference is then between the two actions.

The program does not catch any keyboard actions at all, and is just a python program with some threads/processes in them. It installs no signal handlers (though Python does), and stty -a gives intr = ^C. I suspect it might be that Ctrl+C sends SIGINT to all the sub-processes/threads while kill -INT only sends to the primary process, but that is as far my suspicions go.

Here is the shell script which sends the kill -INT.

wait while :; do     seconds="$(python -c 'import random; print random.random()*4')"     ./mandos --debug --configdir=confdir \              --statedir=statedir --no-restore --no-dbus &     pid=$!     { sleep $seconds; kill -INT $pid; } &     fg %./mandos     status=$?     if [ $status -gt 1 ]; then         echo "Failed exit $status after $seconds seconds"         break     fi     wait done 
like image 526
Belorn Avatar asked Dec 06 '11 11:12

Belorn


People also ask

Is Ctrl C a SIGINT?

SIGINT is the signal sent when we press Ctrl+C. The default action is to terminate the process. However, some programs override this action and handle it differently. One common example is the bash interpreter.

Is Ctrl C the same as kill?

6 Answers. Show activity on this post. Basically Ctrl C sends the SIGINT (interrupt) signal while kill sends the SIGTERM (termination) signal by default unless you specify the signal to send.

What is SIGINT C?

SIGINT is one of the predefined signals that's associated with the terminal interrupt character (commonly Ctrl + C ). It causes the shell to stop the current process and return to its main loop, displaying a new command prompt to the user.

What is the difference between Ctrl C and Ctrl Z?

ctrl c is used to kill a process. It terminates your program. ctrl z is used to pause the process. It will not terminate your program, it will keep your program in background.


2 Answers

^C sends a SIGINT to all the processes in the foreground process group. To do the equivalent with kill, you should send the signal to the process group (OS-level concept):

kill -SIGINT -<pid> 

or to the job (shell-level concept, the pipeline ended with &):

kill -SIGINT % 
like image 146
ninjalj Avatar answered Sep 22 '22 20:09

ninjalj


As described here :

Python installs a small number of signal handlers by default: SIGPIPE is ignored (so write errors on pipes and sockets can be reported as ordinary Python exceptions) and SIGINT is translated into a KeyboardInterrupt exception. All of these can be overridden.

so, the behaviour should be the same between sending a SIGINT and a Ctrl + c.

But, you have to be carefull with the KeyboardInterrupt, if somewhere in your code you've got a

try:    ... except:   # notice the lack of exception class    pass 

this will "eat" the KeyboardInterrupt exception.

like image 26
Cédric Julien Avatar answered Sep 21 '22 20:09

Cédric Julien