Recitation 5: UNIX Signals University at Buffalo, the State University of New York October 2, 2013
About Me 4th year PhD student But TA first time From South Korea
Today, We will study... What UNIX signals are How to generate signals How to catch and handle signals
Review UNIX/C programming Network Socket Programming POSIX Threads Makefile and Modular Programming in C
What are (UNIX) Signals? Software interupts!
What are (UNIX) Signals? Software interupts! Provide a way of handling asynchronous events
What are (UNIX) Signals? Software interupts! Provide a way of handling asynchronous events e.g., a terminal user types Ctrl-C to stop a running program
What are (UNIX) Signals? Software interupts! Provide a way of handling asynchronous events e.g., a terminal user types Ctrl-C to stop a running program UNIX signal is a Inter-Process Communication (IPC) interface
What are (UNIX) Signals? Software interupts! Provide a way of handling asynchronous events e.g., a terminal user types Ctrl-C to stop a running program UNIX signal is a Inter-Process Communication (IPC) interface Sources of Signals: Hardware exceptions: divide by 0 Software conditions: SIGSEGV (segmentation fault), SIGPIPE (write to a closed socket) Terminal (user inputs)
Common Signals Every signal has a name and is defined by an integer SIGHUP(1) hangup - sent to a process when its controlling terminal has disconnected SIGINT(2) interrupt - Ctrl-C pressed by user SIGQUIT(3) quit - Ctrl- pressed by user SIGILL(4) Illegal instruction (default core) SIGABRT(6) Abort process SIGKILL(9) kill (cannot be caught or ignored) SIGSEGV(11) Segmentation fault SIGALRM(14) Alarm clock timeout SIGUSR1,2(10,12) User-defined signals
Process Group: Diagram
Process Group A process group is a collection of one or more processes A single foreground process group One or more backgroup process groups Usually associated with the same job Receives signals from the same terminal Every process belongs to exactly one process group.
Signals from Keyboard The most common way of sending signals to processes is using the keyboard: Ctrl-C Causes the system to send an SIGINT to the running process. Ctrl-Z Causes the system to send an SIGTSTP to the running process. Like PAUSE. Ctrl-\ SIGABRT
Signals from Command Line (kill & fg) kill(1) command $ kill [options] pid... -l lists all the signals you can send -signal is a signal name or number Please read man kill for details! The fg command will resume execution of the process by sending it a CONT signal
Signals from Command Line (kill & fg) Demo!
Signal Disposition We can let the OS kernel do one of the four things on receipt of a signal
Signal Disposition We can let the OS kernel do one of the four things on receipt of a signal 1 Ignore the signal: SIGKILL and SIGSTOP cannot be ignored
Signal Disposition We can let the OS kernel do one of the four things on receipt of a signal 1 Ignore the signal: SIGKILL and SIGSTOP cannot be ignored 2 Catch the signal: We will discuss later
Signal Disposition We can let the OS kernel do one of the four things on receipt of a signal 1 Ignore the signal: SIGKILL and SIGSTOP cannot be ignored 2 Catch the signal: We will discuss later 3 Block the signal: OS queues signals for possible later delivery
Signal Disposition We can let the OS kernel do one of the four things on receipt of a signal 1 Ignore the signal: SIGKILL and SIGSTOP cannot be ignored 2 Catch the signal: We will discuss later 3 Block the signal: OS queues signals for possible later delivery 4 Let the default apply
Default Actions Every signal has a default action The default action for most signals is to terminate the process. See the Professor s Recitation 5 slides for details.
Catching the Signal
Changing Default Action The simplest interface to the signal features of the UNIX is the signal function. Synopsis #include <signal.h> typedef void Sigfunc(int); Sigfunc signal(int signo, Sigfunc *func); Actions (The value of func): SIG DFL (Re)Set to default action SIG IGN Set to ignore the specified signal type *func Catch the signal and run the provided function
Non-Catchable Signals Most signals may be caught by the process, but there are a few signals that the process cannot catch, and cause the process to terminate. e.g., KILL and STOP If you install no signal handlers of your own, the runtime environment sets up a set of default signal handlers.
Catching the Signal: Code sigusr.c https://gist.github.com/kyunghoj/6778988
Alarm Signal SIGALRM can be used as a kind of alarm clock for a process By setting a disposition for SIGALRM, a process can set an alarm to go off at a specified time with the call: Synopsis #include <unistd.h> unsigned int alarm(unsigned int seconds); int pause(void);
Alarm Signal: Code sleep1.c https://gist.github.com/kyunghoj/6779065
Interval Timers Unix system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts. Three Timers: ITIMER REAL decrements in real time, and deliver SIGALRM upon expiration. ITIMER VIRTUAL decrements only when the process is executing, and delivers SIGVTALRM upon expirtion. ITIMER PROF decrements both when the process executes and when the system is executing on behalf of the process. Used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.
Interval Timers Synopsis #include <sys/time.h> int getitimer (int which, struct itimerval *value); int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);
Summary UNIX Signals Signal Types and Actions Generating & Catching Signals ALARM Signal Interval timers
Project 1 Before starting your project...
Project 1 Before starting your project... Set up your development environments Dept servers? Your own machine? Know how to write C programs Know how to compile and run your program
Project 1 Before starting your project... Set up your development environments Dept servers? Your own machine? Know how to write C programs Know how to compile and run your program Know what you are doing Read <Beej s Guide to Network Programming >for Network Socket Programming
References W. Richard Stevens and Stephen Rago. Advanced Programming in the Unix Environment, 2nd ed. There s a newer (2013) version available...