I often start to read a huge file and then want to quit after a while, but there is a lag from pressing
Ctrl+C to the program stops. Is there a chance of shortening the lag by pressing the Ctrl+C key several times? Or am I wasting my keypresses?
After the first Ctrl-C, the program will receive
SIGINT and usually starts cleaning up (deleting tmp files, closing sockets, etc.). If you hit Ctrl-C again while that is going on, it may happen that you interrupt the clean up routine (i.e. the additional signal might be acted upon instead of being left alone), leaving a mess behind. While this usually is not the case, more commonly the additional signals are in fact sent after the process finished (because of the inherent delays in the interaction of the operator with the system). That means that signals are received by another process (often shell, but not always). If that recipient doesn't handle this signal properly (like shell usually does - see Jenny D's answer) you may be unpleasantly surprised by the outcome of such an action.
You're wasting them. All that happens is that once the server finishes with the screen output, it will receive multiple Ctrl-C. The first one will be used to kill the process, and the following ones will end up in your shell, which will then look something like
[user@server]$ ^C [user@server]$ ^C [user@server]$ ^C [user@server]$ ^C [user@server]$ ^C [user@server]$
Short answer: If the process reacts to it.
Long answer: When you hit ctrl+c the kernel sends a signal to the process. Which signal can be determined by the following command:
user@host:~# stty -a | grep -i "\^C" intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
See the man page of
intr CHAR CHAR will send an interrupt signal
It's the signal
INT, also known as number 2. When the process has a signal handler it can react to this. Most processes do some cleanup jobs to end successfully.
You're right. The keypresses are being wasted. When you press Crtl+C and it gets detected, there are resources that need to be cleared, which is why it takes time. The one possible case I know where pressing Ctrl+C is required, is when you want to cancel a Yum update process, where Yum requires you to press Ctrl+C twice to confirm that you really want to cancel.
Although only one Ctrl-C is necessary in the general case, there are some situations where it may be necessary. Python, for example, traps Ctrl-C when a new thread is being spawned, and so if something goes wrong in the midst of starting up many threads, it is necessary to resend it multiple times in order for it to get through to the
python parent process without being caught.
I always have to press Ctrlc multiple times. I have never had it respond on the first press, and have to use it multiple times until the system actually realizes there's a Ctrlc being sent. Seems as though it actually misses/loses most of them.