signal handling toy

Jim Dennis jimd at vega.starshine.org
Sun Mar 17 19:28:58 EST 2002


 The following dumb script is for playing with Python signal handling
 under Linux.  It does show some minor descrepancies between the names
 returned by kill -l and the signal.SIG* names.  

 To actually kill this you have to send it an uncatchable signal
 such as kill -9 (kill -SIGKILL).  So make sure to background it
 or to have another terminal (window, virtual console, whatever)
 into which you can enter your kill command.

#!/usr/bin/env python

import signal

linuxsigs = { 1: "SIGHUP",      2: "SIGINT",   3: "SIGQUIT",   4: "SIGILL", 
			  5: "SIGTRAP",     6: "SIGABRT",  7: "SIGBUS",    8: "SIGFPE",   
			  9: "SIGKILL",    10: "SIGUSR1", 11: "SIGSEGV",  12: "SIGUSR2",  
			  13: "SIGPIPE",   14: "SIGALRM", 15: "SIGTERM",  17: "SIGCHLD", 
			  18: "SIGCONT",   19: "SIGSTOP", 20: "SIGTSTP",  21: "SIGTTIN", 
			  22: "SIGTTOU",   23: "SIGURG",  24: "SIGXCPU",  25: "SIGXFSZ", 
			  26: "SIGVTALRM", 27: "SIGPROF", 28: "SIGWINCH", 29: "SIGIO",   
			  30: "SIGPWR",  31: "SIGSYS", 
32: "SIGRTMIN",    33: "SIGRTMIN+1",  34: "SIGRTMIN+2",  35: "SIGRTMIN+3", 
36: "SIGRTMIN+4",  37: "SIGRTMIN+5",  38: "SIGRTMIN+6",  39: "SIGRTMIN+7", 
40: "SIGRTMIN+8",  41: "SIGRTMIN+9",  42: "SIGRTMIN+10", 43: "SIGRTMIN+11", 
44: "SIGRTMIN+12", 45: "SIGRTMIN+13", 46: "SIGRTMIN+14", 47: "SIGRTMIN+15",
48: "SIGRTMAX-15", 49: "SIGRTMAX-14", 50: "SIGRTMAX-13", 51: "SIGRTMAX-12", 
52: "SIGRTMAX-11", 53: "SIGRTMAX-10", 54: "SIGRTMAX-9",  55: "SIGRTMAX-8", 
56: "SIGRTMAX-7",  57: "SIGRTMAX-6",  58: "SIGRTMAX-5",  59: "SIGRTMAX-4",
60: "SIGRTMAX-3",  61: "SIGRTMAX-2",  62: "SIGRTMAX-1",  63: "SIGRTMAX" }

	# Ugly bit of introspection follows:
pysignames = filter(lambda x: x[0:3]=="SIG" and x[3]!='_', dir(signal))
pysigs = {}
for i in pysignames:
	pysigs[signal.__dict__[i]] =  i
del(pysignames)

def prsig(sig,sframe):
	print "%d (%s?: %s) received" % \
		(sig,linuxsigs[sig],pysigs.get(sig, "Uknown"))


if __name__ == '__main__':
	for i in linuxsigs.keys():
		signal.signal(i, prsig)
		print "Signal set for: %s (%d %s?)" % (linuxsigs[i], i, pysigs.get(i))

	while i:
		## Must send "uncatchable" signal to end program?
		## Could add sigalarm + time support?
		signal.pause()




More information about the Python-list mailing list