Created
February 9, 2014 11:57
-
-
Save amitsaha/8898074 to your computer and use it in GitHub Desktop.
Code for experimenting with session ID, process group ID, and signal handling
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
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