Created
May 22, 2023 15:00
-
-
Save z-image/762691ee7a67ffdeb88318c47d9ebf0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
import signal | |
import sys | |
import time | |
def signal_handler(signum, _frame) -> None: | |
if signum == 10: | |
return | |
sys.exit() | |
num_children = 2 | |
pids = [] | |
for _ in range(num_children): | |
read_pipe, write_pipe = os.pipe() | |
pid = os.fork() | |
if pid == 0: | |
# Child process | |
os.close(write_pipe) | |
signal.signal(signal.SIGUSR1, signal_handler) | |
# Block on reading from the pipe | |
while True: | |
data = os.read(read_pipe, 1) | |
print(f"read() returned {data}") | |
if data == b'': | |
sys.exit() | |
time.sleep(1) | |
else: | |
# Parent process | |
pids.append(pid) | |
# Parent process | |
os.close(read_pipe) | |
time.sleep(1) # give some time to children to setup signal handler | |
start = time.time() | |
while time.time() < start + 120: | |
for pid in pids: | |
os.kill(pid, signal.SIGUSR1) | |
time.sleep(0.1) | |
for pid in pids: | |
os.kill(pid, signal.SIGTERM) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment