LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Ctrl + C in bash (https://www.linuxquestions.org/questions/linux-general-1/ctrl-c-in-bash-773966/)

mircan 12-07-2009 06:55 AM

Ctrl + C in bash
 
I've got the following question:
What happens when I type Ctrl+C while a process is running in bash shell.
I mean - is it the kernel that sends the signal to the process or is it the shell that interprets the key combination and sends the signal to its child process?

pixellany 12-07-2009 07:48 AM

This is what you might call a "partially educated guess"....

Anything you do in a terminal is interacting with the shell (eg BASH). In a terminal session, you are in a subshell and this subshell keeps track of what its current process is. ctrl-C says "kill this current process". That results in the subshell telling the kernel to kill the process by number.

I've never tried to read the API for the kernel---I'm sure its published somewhere.

mircan 12-07-2009 04:53 PM

I understand that you mean that it is the shell that sends the signal to the script, am I right?

catkin 12-08-2009 12:33 AM

Quote:

Originally Posted by mircan (Post 3783233)
I understand that you mean that it is the shell that sends the signal to the script, am I right?

AFAIK the kernel handles the terminal (screen and keyboard) according to how the terminal is configured. By default this configuration includes setting Ctrl+C to generate the intr signal (interrupt, also known as SIGINT) as shown by
Code:

stty -a
Thus, when Ctrl+C is pressed on the keyboard, the kernel sends a SIGINT to all processes that have this particular terminal as their "controlling terminal" (the "process group"?), including the shell and its child processes and their child processes ... .

When a shell is a running a child process it disables SIGINT as explained here until the child process terminates. Thus, when you are running a process from a shell, the process is sent SIGINT and acts on it according to how it is configured; if and when it exits the shell then processes SIGINT according to how it is configured.

pixellany 12-08-2009 07:31 AM

Quote:

Originally Posted by mircan (Post 3783233)
I understand that you mean that it is the shell that sends the signal to the script, am I right?

That is not what I said----but catkin's explanation looks to be much better

mircan 12-08-2009 02:48 PM

Now, after catkin's explanation, I understand it. Thanks for answering.


All times are GMT -5. The time now is 01:54 PM.