OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 import signal |
| 3 import os |
| 4 import time |
| 5 import pty |
| 6 import sys |
| 7 import fcntl |
| 8 import tty |
| 9 GLOBAL_SIGCHLD_RECEIVED = 0 |
| 10
|
| 11 def nonblock (fd): |
| 12 # if O_NDELAY is set read() returns 0 (ambiguous with EOF). |
| 13 # if O_NONBLOCK is set read() returns -1 and sets errno to EAGAIN |
| 14 original_flags = fcntl.fcntl (fd, fcntl.F_GETFL, 0) |
| 15 flags = original_flags | os.O_NONBLOCK |
| 16 fcntl.fcntl(fd, fcntl.F_SETFL, flags) |
| 17 return original_flags |
| 18 |
| 19 def signal_handler (signum, frame): |
| 20 print '<HANDLER>' |
| 21 global GLOBAL_SIGCHLD_RECEIVED |
| 22 status = os.waitpid (-1, os.WNOHANG) |
| 23 if status[0] == 0: |
| 24 print 'No process for waitpid:', status |
| 25 else: |
| 26 print 'Status:', status |
| 27 print 'WIFEXITED(status):', os.WIFEXITED(status[1]) |
| 28 print 'WEXITSTATUS(status):', os.WEXITSTATUS(status[1]) |
| 29 GLOBAL_SIGCHLD_RECEIVED = 1 |
| 30 |
| 31 def main (): |
| 32 signal.signal (signal.SIGCHLD, signal_handler) |
| 33 pid, fd = pty.fork() |
| 34 if pid == 0: |
| 35 os.write (sys.stdout.fileno(), 'This is a test.\nThis is a test.
') |
| 36 time.sleep(10000) |
| 37 nonblock (fd) |
| 38 tty.setraw(fd) #STDIN_FILENO) |
| 39 print 'Sending SIGKILL to child pid:', pid |
| 40 time.sleep(2) |
| 41 os.kill (pid, signal.SIGKILL) |
| 42 |
| 43 print 'Entering to sleep...' |
| 44 try: |
| 45 time.sleep(2) |
| 46 except: |
| 47 print 'Sleep interrupted' |
| 48 try: |
| 49 os.kill(pid, 0) |
| 50 print '\tChild is alive. This is ambiguous because it may be a Z
ombie.' |
| 51 except OSError as e: |
| 52 print '\tChild appears to be dead.' |
| 53 # print str(e) |
| 54 print |
| 55 print 'Reading from master fd:', os.read (fd, 1000) |
| 56 |
| 57 |
| 58 |
| 59 if __name__ == '__main__': |
| 60 main () |
OLD | NEW |