3

I have read that when you press Ctrl+C, then a SIGINT signal will be sent to the foreground process group.

Can you give me an example of how I can have two or more processes in the foreground process group, because I want to see if all processes will terminate if I press Ctrl+C.

2

Since new processes all belong to the same process group, that of the parent process, have a process start a bunch of processes (fork), and then with appropriate logging and a delay, type control+c. They all eat a SIGINT.

$ perl -E 'fork for 1..2;say "ima $$"; $SIG{INT}=sub{die "woe $$\n"}; sleep 999'
ima 80920
ima 80922
ima 80921
ima 80923
^Cwoe 80920
woe 80922
woe 80921
woe 80923
$ 

(Add strace or sysdig or such to see the system calls or signals involved.)

  • How can a process start more than one process, I mean wouldn't the parent process block until the first child process is terminated? – Steve May 16 '17 at 19:59
  • fork makes two processes from one process. The parent can but need not block by calling some wait function. The above perl processes do not block on anything, they just all go to sleep. – thrig May 16 '17 at 20:07
  • I can also just make the parent process start only one child process, and then press Ctrl+C one time to terminate both the parent and child processes (I tried it and it worked). – Steve May 17 '17 at 12:42
2

A pipeline is one job, with multiple processes:

(cat ; echo foo >&2) | (cat ; echo bar >&2 )

If you end the input with ^D, you get the foo and bar output, if you kill the pipeline with ^C, neither is printed.

0

One example:

bash-4.3$ ( ( sleep 2 & (ps -Hfj | sh -c cat; perl -MPOSIX -E 'say tcgetpgrp 0'; sleep 5;:);:);:)
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
chazelas 18631  3848 18631 18631  0 12:51 pts/7    00:00:00 /bin/zsh
chazelas  2184 18631  2184 18631  0 21:00 pts/7    00:00:00   bash --norc
chazelas  2430  2184  2430 18631  0 21:07 pts/7    00:00:00     bash --norc
chazelas  2431  2430  2430 18631  0 21:07 pts/7    00:00:00       bash --norc
chazelas  2432  2431  2430 18631  0 21:07 pts/7    00:00:00         sleep 2
chazelas  2433  2431  2430 18631  0 21:07 pts/7    00:00:00         bash --norc
chazelas  2434  2433  2430 18631  0 21:07 pts/7    00:00:00           ps -Hfj
chazelas  2435  2433  2430 18631  0 21:07 pts/7    00:00:00           sh -c cat
chazelas  2436  2435  2430 18631  0 21:07 pts/7    00:00:00             cat
2430

At the time ps was running, there were 7 processes in the 2430 process group: 3 subshell processes (bash), one running ps, one running sh, one running cat, one running sleep 1. Later on, the processes spawned to execute perl and sleep 5 would also be in that same group.

tcgetpgrp() confirms that 2430 was indeed the foreground process group of the terminal device, so upon pressing Ctrl+C, all the processes in that group would receive a SIGINT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.