notes blog about

2015-10-20

Signal – a message from the kernel (or a process) to a process. Used for:

To ask the kernel to a send a signal:

kill [-SIGNAL] PID  # default signal is TERM

Selected signal types:

Each process has a default disposition (what to do) for each possible signal. You may install your own handler or otherwise change the disposition of most signals. Only SIGKILL and SIGSTOP cannot be changed. The rest you can:

This is how you can catch signals in Perl:

#!/usr/bin/env perl
#
# signal-catcher -- send me a signal, e.g.:
#
# $ kill -2 <my-pid>
#
use 5.014;    # includes strict
use warnings;
use autodie;

our $shucks = 0;            # shuck - škrupina, šok?

sub catch_zap {             # zap - šleha?
    my $signame = shift();
    $shucks++;
    die "Somebody sent me a SIG$signame!";
}

$SIG{INT}   = \&catch_zap;
$SIG{QUIT}  = \&catch_zap;  # catch another signal, too

$|++;
print "Going to sleep ";
while (1) {
    print ".";
    sleep 1;
}

Catching a signal in Python:

#!/usr/bin/env python

import sys, signal, time

def now(): return time.ctime(time.time())       # current time string

def onSignal(signum, stackframe):               # python signal handler
    print('Got signal', signum, 'at', now())

signum = int(sys.argv[1])                       # from the cmd line

signal.signal(signum, onSignal)                 # install signal handler
while True: signal.pause()                      # wait for signals

Handling signals in Go.

Source: