-1

What is signal handling? What is a signal in the context of programming mean? Is it like an interrupt in hardware, like a timer for example?

Can anyone give me an example in Python?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ToniAz
  • 430
  • 1
  • 6
  • 24
  • 2
    As this is a conceptual question, I've requested that it be migrated to Programmers instead. Stack Overflow is meant for practical problems (when coding and you get stuck), Programmers for conceptual questions (while at the whiteboard, say). – Martijn Pieters May 03 '13 at 15:57
  • Well, [WP's definition](http://en.wikipedia.org/wiki/Unix_signal) isn't too bad, but there are probably better descriptions elsewhere. As for a Python example, I recently posted one towards the end of an [answer to another question](http://stackoverflow.com/questions/16341047/how-to-clean-up-subprocess-popen-instances-upon-process-termination/16341870#16341870). – Aya May 03 '13 at 15:58

1 Answers1

0

A signal is typically exactly what it sounds like - it's a message delivered to a process. Most often when people say "signal," they're referring to a software interrupt that's sent to a process to trigger an event.

Think of it as message passing between processes - whether this means that one will abort a thread/run a shutdown method, etc.

see: http://docs.python.org/2/library/signal.html for an example:

import signal, os

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    raise IOError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm
  • It's giving me an error: 'module' object has no attribute 'SIGALRM'. I think that's because I'm working on Windows... Can't I do interrupts in Python on Windows? – ToniAz May 03 '13 at 18:26
  • Windows ostensibly doesn't support SIGnals of that sort. What's in signal.__dict__ on your version of python? –  Jul 09 '13 at 23:34