Skip to content

Instantly share code, notes, and snippets.

@amitsaha
Created February 9, 2014 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaha/8898074 to your computer and use it in GitHub Desktop.
Save amitsaha/8898074 to your computer and use it in GitHub Desktop.
Code for experimenting with session ID, process group ID, and signal handling
import os
import signal
def sighup_handler(signum, frame):
print 'SIGHUP handled by', os.getpid()
def sigtstp_handler(signum, frame):
print 'SIGTSTP handled by', os.getpid()
def sigint_handler(signum, frame):
print 'SIGINT handled by', os.getpid()
cpid = os.fork()
if cpid == 0:
print 'In first child'
print 'PGID: ', os.getpgrp(), 'SID: ', os.getsid(0), 'PPID: ', os.getppid(), 'PID: ', os.getpid()
signal.signal(signal.SIGHUP, sighup_handler)
signal.signal(signal.SIGTSTP, sigtstp_handler)
signal.signal(signal.SIGINT, sigint_handler)
cpid_2 = os.fork()
if cpid_2 == 0:
print 'In second child'
print 'PGID: ', os.getpgrp(), 'SID: ', os.getsid(0), 'PPID: ', os.getppid(), 'PID: ', os.getpid()
signal.signal(signal.SIGHUP, sighup_handler)
signal.signal(signal.SIGTSTP, sigtstp_handler)
signal.signal(signal.SIGINT, sigint_handler)
while True:
pass
else:
while True:
pass
else:
print 'In parent'
print 'PGID: ', os.getpgrp(), 'SID: ', os.getsid(0), 'PPID: ', os.getppid(), 'PID: ', os.getpid()
signal.signal(signal.SIGHUP, sighup_handler)
signal.signal(signal.SIGTSTP, sigtstp_handler)
signal.signal(signal.SIGINT, sigint_handler)
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment