OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 import signal |
| 3 import os |
| 4 import time |
| 5 import pty |
| 6 |
| 7 def signal_handler (signum, frame): |
| 8 print 'Signal handler called with signal:', signum |
| 9 print 'signal.SIGCHLD=', signal.SIGKILL |
| 10 |
| 11 # First thing we do is set up a handler for SIGCHLD. |
| 12 signal.signal (signal.SIGCHLD, signal.SIG_IGN) |
| 13 |
| 14 print 'PART 1 -- Test signal handling with empty pipe.' |
| 15 # Create a child process for us to kill. |
| 16 try: |
| 17 pid, fd = pty.fork() |
| 18 except Exception as e: |
| 19 print str(e) |
| 20 |
| 21 if pid == 0: |
| 22 # os.write (sys.stdout.fileno(), 'This is a test.\n This is a test.') |
| 23 time.sleep(10000) |
| 24 |
| 25 print 'Sending SIGKILL to child pid:', pid |
| 26 os.kill (pid, signal.SIGKILL) |
| 27 |
| 28 # SIGCHLD should interrupt sleep. |
| 29 # Note that this is a race. |
| 30 # It is possible that the signal handler will get called |
| 31 # before we try to sleep, but this has not happened yet. |
| 32 # But in that case we can only tell by order of printed output. |
| 33 print 'Entering sleep...' |
| 34 try: |
| 35 time.sleep(10) |
| 36 except: |
| 37 print 'sleep was interrupted by signal.' |
| 38 |
| 39 # Just for fun let's see if the process is alive. |
| 40 try: |
| 41 os.kill(pid, 0) |
| 42 print 'Child is alive. This is ambiguous because it may be a Zombie.' |
| 43 except OSError as e: |
| 44 print 'Child appears to be dead.' |
| 45 |
| 46 print 'PART 2 -- Test signal handling with full pipe.' |
| 47 # Create a child process for us to kill. |
| 48 try: |
| 49 pid, fd = pty.fork() |
| 50 except Exception as e: |
| 51 print str(e) |
| 52 |
| 53 if pid == 0: |
| 54 os.write (sys.stdout.fileno(), 'This is a test.\n This is a test.') |
| 55 time.sleep(10000) |
| 56 |
| 57 print 'Sending SIGKILL to child pid:', pid |
| 58 os.kill (pid, signal.SIGKILL) |
| 59 |
| 60 # SIGCHLD should interrupt sleep. |
| 61 # Note that this is a race. |
| 62 # It is possible that the signal handler will get called |
| 63 # before we try to sleep, but this has not happened yet. |
| 64 # But in that case we can only tell by order of printed output. |
| 65 print 'Entering sleep...' |
| 66 try: |
| 67 time.sleep(10) |
| 68 except: |
| 69 print 'sleep was interrupted by signal.' |
| 70 |
| 71 # Just for fun let's see if the process is alive. |
| 72 try: |
| 73 os.kill(pid, 0) |
| 74 print 'Child is alive. This is ambiguous because it may be a Zombie.' |
| 75 except OSError as e: |
| 76 print 'Child appears to be dead.' |
| 77 |
OLD | NEW |