Unformatted text preview:

SignalsSignal HandlingSignal HandlersMasking SignalsPosix.1 Required SignalsPosix.1 Job Control SignalsGeneration of Signalskill CommandSignal Symbolic Nameskill System Callkill System Call Exampleraise System CallOutput of stty –a on ect-unix MachineSignal Generating Charactersalarm System CallSignal Mask and Signal SetsSignal Set Functionssigprocmask System Callsigprocmask – how parametersigprocmask – Example 1sigprocmask – Example 2sigprocmask – Example 3sigaction System Callsigaction – Example 1sigaction – Example 2Set Handler to Defaultpause System CallPause – Example 1pause – Example 2sigsuspend System Callsigsuspend – Example 1Biff get_file_sizeBiff Interrupt HandlersBiff notify_of_mailBiff main()Interrupted readTimeout read from pipeAsync-Signal SafeSignal Handling RulesAsync-Signal Safe FunctionssiglongjumpSignals•Notifies a process of an event•Generated when event that causes signal occurs•Delivered when the process takes action based on the signal•A signal is pending if it has been generated but is not yet delivered.•The lifetime of a signal is the interval between generation and delivery.Signal Handling•A process catches a signal if it executes a signal handler when the signal is delivered•A program installs a signal handler by making a call to the sigaction system callSignal HandlersThe sigaction system call installs either:•A user defined function•SIG_DFL – a default routine•SIG_IGN – ignore the signalMasking Signals•The action taken on receipt of a signal depends on the current signal handler for that signal and the signal mask for the process•The mask identifies the currently blocked signals•You change a signal mask with the sigprocmask system call•blocking is different from ignoring – you ignore a signal by installing SIG_IGN with sigactionPosix.1 Required SignalsSymbol MeaningSIGABRTSIGALRMSIGFPESIGHUPSIGILLSIGINTSIGKILLSIGPIPESIGQUITSIGSEGVSITTERMSIGUSR1SIGUSR2Abnormal termination as initiated by abortTimeout signal as initiated by alarmError in arithmetic operation as in division by zeroHang up (deat) on controlling terminal (process)Invalid hardware instructionInteractive attention signal Terminate (cannot be caught or ignored)Write on a pipe with no readersInteractive termination Invalid memory referenceTerminationUser-defined signal 1User-defined signal 2Posix.1 Job Control SignalsSymbol MeaningSIGCHLDSIGCONTSIGSTOPSIGTSTPSIGTTINSIGTTOUIndicates child process terminated or stoppedContinue if stopped (done when generated)Stop signal (cannot be caught or ignored)Interactive stop signalBackground process attempts to read from controlling terminalBackground process attempts to write to controlling terminalGeneration of Signals•Some signals are generated with errors occur (i.e., SIGFPE or SIGEGV)•A user can send signals to a process that it owns (the effective user ID of the sending and receiving processes are the same)•Signals can be generated from the shell with a kill system callkill CommandExample: kill –USR1 3423•Sends SIGUSR1 to process 3423•USR1 is a symbolic name for the signal SIGUSR1•To get a list of symbolic names for signals, type kill -lSignal Symbolic NamesTyping ls –l on the ect-unix machines causes the following to be output:EXIT HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAXkill System CallSYNOPSIS#include <sys/types.h>#include <sys/types.h>int kill(pid_t pid, int sig);POSIX.1 Spec 1170kill System Call Example#include <stdio.h>#include <sys/types.h>#inlcude <signal.h>if (kill(3423, SIGUSR1) == -1)perror(“Could not send signal”);if (kill (getppid(), SIGTERM) == -1)perror(“Error in kill”);raise System CallSYNOPSIS#include <signal.h>int raise(int sig);ISO C, Spec 1170A process can send a signal to itself with a raise system call – Example:raise (SIGUSR1);Output of stty –a on ect-unix Machinespeed 9600 baud; rows 24; columns 80;intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; dsusp = ^Y; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; -parenb parodd cs8 hupcl cstopb cread -clocal crtscts-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon ixof-iuclc -ixany -imaxbelopost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 f0isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprtechoctl echokeSignal Generating Characters•The INTR character, ctrl-c, generates SIGINT for the foreground process•The QUIT character, ctrl-|, generates SIGQUIT•The SUSP character, ctrl-z, generates SIGSTOP•The DSUP character, ctrl-y, generates SIGCONTalarm System CallSYNOPSISinclude <unistd.h>unsigned int alarm(unsigned int seconds);Posix.1 Spec 1170Causes SIGALRM signal to be sent to calling process after the specified number of seconds have elapsed – Example:void main(void) {alarm(10); for( ; ; ) {} }Signal Mask and Signal Sets•A process can temporarily prevent a signal from being delivered by blocking it•The process signal mask contains the set of signals that are currently blocked•When a process blocks a signal, an occurrence of the signal is held until the signal is unblocked (blocked signals do not get lost – ignored signals do get lost)Signal Set Functions•sigemptyset – remove all signals from the signal set•sigfillset – initializes a signal set to contain all signals•sigaddset – adds a specified signal to a signal set•sigdelset – removes a specified signal from a signal set•sigismember – tests to see if a specified signal is in a signal setsigprocmask System CallSYNOPSIS#include <signal.h>int sigprocmask(int how,const sigset_t *set, sigset_t *oset);Posix.1, Spec 1170sigprocmask – how parameter•SIG_BLOCK – add the collection of signals in the parameter set to those already in the signal set•SIG_UNBLOCK – delete the collection of signals in the parameter set from the signal set•SIG_SETMASK – replace the entire current signal set with the collection of signals in the parameter setsigprocmask – Example 1#include <stdio.h>#include <signal.h>sigset_t newsigset;sigemptyset(&newsigset); /* newsigset is empty */sigaddset(&newsigset, SIGINT); /* add SIGINT to newsigset */if (sigprocmask (SIG_BLOCK,


View Full Document

Chico CSCI 372 - Signals

Download Signals
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Signals and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Signals 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?