0
votes

b.py

import subprocess
f = subprocess.Popen(['python', 'a.py'])
time.sleep(3000)

a.py

import time
time.sleep(1000)

Run python b.py, Press CTRL+C, both processes will terminate.

However send the signal SIGINT to the parent process b.py, kill -2 xxxx, but the child process a.py remains.

1
What makes you think a CTRL-C is handled as a SIGINT? - Martijn Pieters
typo. SIGINT. I'm talking about signal 2 anyway - Bob
Same question, what makes you think CTRL-C is a simple SIGINT? What OS are you running this on? - Martijn Pieters
Linux. Every piece of information I find on Internet tells me CTRL+C equals to SIGINT. - Bob
Ctrl+C is similar to os.killpg(xxxx, signal.SIGTERM) here. - jfs

1 Answers

1
votes

Ctrl-C at your terminal typically sends SIGINT to all processes in the foreground process group. Both your parent and your child process are in this process group.

For a more detailed explanation, see for example The TTY demystified or the more technical version by Kirk McKusick at Process Groups and Sessions

If you just kill the parent process, the child is parentless and thus gets reparented to PID 1 (init). You can see this too, in the output of ps. Since your subprocess never receives a signal, it simply continues running.