Signals and Session Management 2002 2 Hyun-Ju Park
(Signal)? Introduction (1) mechanism events : asynchronous events - interrupt signal from users : synchronous events - exceptions (accessing an illegal address) primitive mechanism an integer value which specifies the type of events Signals and Session Management 1
History Introduction (2) Original System V signal unreliable and defective 4.2BSD reliable, robust signal SVR3 System V reliable signal (4.2BSD incompatible) POSIX 1003.1 BSD SVR3 reliable signal Solaris, AIX, HP-UX, 4.4BSD, Digital UNIX UNIX POSIX compliant signal Signals and Session Management 2
Issues Introduction (3)? ( ),???? Signals and Session Management 3
(generation) (delivery) Signal? Original system V defined 15 signals 4BSD and SVR4 define 32 signals (some variants support more than 32 signals) POSIX 1003.1 signal symbolic name 102, 4-1 Signals and Session Management 4
(1), function signal handler default action Signals and Session Management 5
(2) 5 default actions for signals abort : terminate the process after generating a core dump exit : terminates the process without generating a core dump ignore : ignores the signal stop : suspends the process continue : resumes the process, if suspended( or else, ignores the signal) User defined function(signal handler) SIGKILL SIGSTOP ignore, block, signal handler 102, 4-1 Signals and Session Management 6
(3) issig(), issig() issig() TRUE, psig() psig(): sendsig() Signals and Session Management 7
(4) Signal delivered Execute normal code Signal handler runs Resume normal execution Signals and Session Management 8
(1) Major sources of signals (exceptions) (terminal interrupts) (job control) (quotas) (notifications) (alarms) Signals and Session Management 9
(2) kill sigsend Ctrl-C foreground process csh ksh foreground background process Signals and Session Management 10
(3) CPU Signals and Session Management 11
(1) Asynchronous events Ctrl-C foreground process SIGINT, (issig()) Signals and Session Management 12
(2) Synchronous events : exception, issig() Signals and Session Management 13
Sleep and Signals What happens sleeping process receives a signal? Two categories of sleep uninterruptible sleep sleep : mark the signal as pending interruptible sleep : issig() Signals and Session Management 14
Signals in SVR4 (1) Superset of the functionality of SVR3 and BSD signals sigprocmask(int how, const sigset_t * setp, sigset_t *osetp); how: SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK setp: new mask osetp: old mask if not nonnull pointer sigaltstack(stack, old_stack); sigsuspend(const sigset_t* sigmask); unblocking pause signal atomic operation sigpending(sigset_t* setp); block or pending signal check sigsendset(procset_t* procset, sig); process group sig Signals and Session Management 15
Signals in SVR4 (2) sigaction(int signo, const struct sigaction* act, struct sigaction oact); struct sigaction { } void (*sa_handler) (); /* addr of signal handler, or SIG_IGN, or SIG_DFL*/ sigset_t sa_mask; /* additional signals to block */ int sa_flags; /* signal options */ sa_mask signo act signo Signals and Session Management 16
Signals in SVR4 (3) SVR4 signal implementation BSD u area : contains information required to properly invoke the signal handlers, u_signal[ ]: vector of signal handlers for each signal u_sigmask[ ]: signal masks associated with each handler u_sigaltstack: pointer to the alternate signal stack u_sigonstack: mask of signals to handle on the alternate stack u_oldsig: set of handlers that must exhibit the old, unreliable behavior proc structure : contains fields related to generation and posting of signals p_cursig: current signal being handled p_sig: pending signal mask p_hold: blocked signal mask p_ignore: ignored signal mask Signals and Session Management 17
#include <signal.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> void sigusr1_handler (int signal_number) { printf( Ctrl-C!!\n ); printf( --> \n ); exit(1); } int main (void) { struct sigaction sa; memset (&sa, 0, sizeof (sa)); sa.sa_handler =&sigint_handler; sigaction (SIGINT,&sa,NULL); while (1) printf( * ); printf ( \n ); return 0; } Signals and Session Management 18
UNIX, Signals and Session Management 19
(1) (Process groups) ID process group ID event PID ID, /dev/tty Signals and Session Management 20
(2) tty t_pgrp SIGINT, SIGQUIT fg (SIGCONT) Ctrl-Z (SIGTSTP) bg Signals and Session Management 21
SVR3 Process group is the same as a terminal login session P P P P P Process Foreground Process Proces group Login session Signals and Session Management 22
SVR4 Introduces a session structure Session = a collection of process groups P P P P P P Session Object P P Process Foreground Process Proces group Login session Signals and Session Management 23