Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will ctrl+c send SIGINT signals to both parent and child processes in Linux?

In the terminal, I executed a main parent process which will fork a child process. In both the parent and child processes I implemented a SIGINT signal handler. So when I press "ctrl+c", will both the handlers be called at the same time? Or do I need to call the child process's signal handler explicitly in the parent process's handler?

I looked up this post: How does Ctrl-C terminate a child process? which says that "The SIGINT signal is generated by the terminal line discipline, and broadcast to all processes in the terminal's foreground process group". I just didn't quite understand what does "foreground process group" means.

Thanks,

like image 901
Hao Shen Avatar asked Aug 09 '15 17:08

Hao Shen


People also ask

Does control c Send SIGINT?

Alternatively, we can send signals in a terminal using key combinations. For instance, Ctrl+C sends SIGINT, Ctrl+S sends SIGSTOP, and Ctrl+Q sends SIGCONT. Each signal has a default action, but a process can override the default action and handle it differently, or ignore it.

What signal does Ctrl-C send Linux?

Ctrl-C. Pressing this key causes the system to send an INT signal ( SIGINT ) to the running process. By default, this signal causes the process to immediately terminate.

What happens when you press Ctrl-C Linux?

While in a command line such as MS-DOS, Linux, and Unix, Ctrl + C is used to send a SIGINT signal, which cancels or terminates the currently-running program.


1 Answers

In both the parent and child processes I implemented a SIGINT signal handler. So when I press "ctrl+c", will both the handlers be called at the same time?

Yes, they both will receive SIGINT.

Or do I need to call the child process's signal handler explicitly in the parent process's handler?

"Calling" another process' signal handler doesn't make sense. If the both the process have a handler installed then they will be called once they receive the signal SIGINT.

I just didn't quite understand what does "foreground process group" means.

Typically, a process associated with a controlling terminal is foreground process and its process group is called foreground process group. When you start a process from the command line, it's a foreground process:

E.g.

$ ./script.sh # foreground process
$ ./script & # background process

I suggest you read about tty and The TTY demystified for a detailed explanation.

like image 154
P.P Avatar answered Nov 15 '22 19:11

P.P