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,
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With