OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 import signal |
| 3 import os |
| 4 import time |
| 5 |
| 6 def signal_handler (signum, frame): |
| 7 print 'Signal handler called with signal:', signum |
| 8 print 'signal.SIGCHLD=', signal.SIGKILL |
| 9 |
| 10 # Create a child process for us to kill. |
| 11 pid = os.fork() |
| 12 if pid == 0: |
| 13 time.sleep(10000) |
| 14 |
| 15 #signal.signal (signal.SIGCHLD, signal.SIG_IGN) |
| 16 signal.signal (signal.SIGCHLD, signal_handler) |
| 17 |
| 18 print 'Sending SIGKILL to child pid:', pid |
| 19 os.kill (pid, signal.SIGKILL) |
| 20 |
| 21 # SIGCHLD should interrupt sleep. |
| 22 # Note that this is a race. |
| 23 # It is possible that the signal handler will get called |
| 24 # before we try to sleep, but this has not happened yet. |
| 25 # But in that case we can only tell by order of printed output. |
| 26 interrupted = 0 |
| 27 try: |
| 28 time.sleep(10) |
| 29 except: |
| 30 print 'sleep was interrupted by signal.' |
| 31 interrupted = 1 |
| 32 |
| 33 if not interrupted: |
| 34 print 'ERROR. Signal did not interrupt sleep.' |
| 35 else: |
| 36 print 'Signal interrupted sleep. This is good.' |
| 37 |
| 38 # Let's see if the process is alive. |
| 39 try: |
| 40 os.kill(pid, 0) |
| 41 print 'Child is alive. This is ambiguous because it may be a Zombie.' |
| 42 except OSError as e: |
| 43 print 'Child appears to be dead.' |
| 44 |
OLD | NEW |