Skip to content

Commit

Permalink
[demo] Verify signal state
Browse files Browse the repository at this point in the history
Fix typo in test/group-session.sh

Trying to figure out why we need stop_process_hack() in
spec/stateful/harness.py
  • Loading branch information
Andy C committed Feb 18, 2022
1 parent 0d1c5ff commit 250fce8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
71 changes: 71 additions & 0 deletions demo/cpython/fork_signal_state.py
@@ -0,0 +1,71 @@
#!/usr/bin/env python2
"""
fork_signal_state.py
"""
from __future__ import print_function

import os
import signal
import sys
import time


def log(msg, *args):
if args:
msg = msg % args
print(msg, file=sys.stderr)


def SignalState(pid):
with open('/proc/%d/status' % pid) as f:
for line in f:
if line.startswith('Sig'):
print(line, end='')


def main(argv):
parent_pid = os.getpid()
log('parent is %d', parent_pid)

# Hm this looks like it works
# Now why doesn't Ctrl-Z through the terminal work? The process group should
# be controlling the terminal

# test/group-session.sh shows PGID and TPGID (controlling tty process group ID)

log('===')
SignalState(parent_pid)
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
SignalState(parent_pid)
log('===')

pid = os.fork()
if pid == 0:
child_pid = os.getpid()

log('---')
SignalState(child_pid)
signal.signal(signal.SIGTSTP, signal.SIG_DFL)
SignalState(child_pid)
log('---')

log('sleep 1 in child %d', child_pid)
time.sleep(1)

elif pid < 0:
raise AssertionError()

else:
log('parent spawned %d', pid)

log('waiting')
result = os.waitpid(-1, 0)
log('wait => %s', result)


if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
"""
py_signals.py
keyboard_interrupt.py: How to replace KeyboardInterrupt
"""
from __future__ import print_function

Expand Down
6 changes: 3 additions & 3 deletions test/group-session.sh
Expand Up @@ -27,17 +27,17 @@ show_group_session() {

case $kind in (*fgproc*)
echo '[foreground process]'
ps -o pid,ppid,pgid,sid,tgid,comm
ps -o pid,ppid,pgid,sid,tpgid,comm
;;
esac

case $kind in (*bgproc*)
echo '[background process]'
ps -o pid,ppid,pgid,sid,tgid,comm &
ps -o pid,ppid,pgid,sid,tpgid,comm &
wait

# - Gets its own PGID
# - Hm UNLIKE bgpipe it also gets its own TGID? Seems consistent in all
# - Hm UNLIKE bgpipe it also gets its own TPGID? Seems consistent in all
# shells. Why is that?
;;
esac
Expand Down

0 comments on commit 250fce8

Please sign in to comment.