09 - Signals

Class: CSCE-313


Notes:

Outline

  1. What is a “signal”?
  2. Signal “handling”.
  3. UNIX signals API.

What is a signal?

Examples:

Notes:

Example

cat -
> stty -a
speed 9600 baud; 52 rows; 49 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;

If we look at the types of signals:

> kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ VTALRM PROF WINCH INFO USR1 USR2

Q&A

What happens when you press Ctrl-C on the terminal with a running foreground process?

It is your terminal driver that is looking at the characters that is passing to the reader. There is a program at the other end that reads characters from that file, but in between there is this terminal driver that looks at all of these characters. It is the terminal drive that will send the SIGINT signal, not to a process but to the process group.

Notes:


How to deal with runaway processes

https://www.youtube.com/watch?v=z6sDujfLNfI

Signals are asynchronous

func (int a1, int a2) {
    int i, j = 2;
    for (i = a1; i < a2; i++) {
        j = j * 2;            // <----
        j = j / 127;          // <----
        // ...
    }
}

What is the purpose of signals?

  1. Allow humans to interact with programs using the terminal. For e.g.,
    • Ctrl+C sends an interrupt signal SIGINT
    • Ctrl+Z suspends the process by sending SIGTSTP
  2. Allow the kernel to enforce semantics
    • SIGSEGV is sent on memory exceptions
    • SIGILL sent on encountering illegal instructions
    • SIGPIPE when one writes to a pipe with no reader
      • This happens even mid-flight if readers exit

Notes:

Example of handler for an illegal isntruction:

void sigill_handler(int sig) {
	print("Caught SIGILL (Illegal instruction)")
}
...

UNIX System signals

image-6.png

What can a process do about a signal-I?

A program can tell the kernel to do one of three things when a signal occurs. We call this the disposition of the signal, or the action associated with a signal.

  1. Accept the default action. All signals have a default action.
    • signal(SIGINT, SIG_DFL)
    • Ignore
    • Terminate
    • Terminate and dump core
  2. Ignore the signal. Works for most signals.
    • signal(SIGINT, SIG_IGN)
    • Note: cannot ignore SIGKILL and SIGSTOP. It is not advised to ignore hardware exception signals.
  3. Catch the signal. (Invoke a function). Tell the kernel to invoke a given function (signal handler) whenever the signal occurs.
    • signal(SIGINT, do_something)

Notes:

What can a process do about signal-II?

Notes:

Where do signals come from?

From the user: Terminal-generated signals: for e.g., C

From the kernel: CPU Exceptions delivered as signals

From processes: Software generated signals

Notes: