Unformatted text preview:

SignalsSignal HandlingSignal HandlersMasking SignalsPOSIX Required SignalsPOSIX Job Control SignalsGeneration of Signalskill CommandSignal_Name ExampleSignal_Number ExampleSignal Symbolic Nameskill System Callerrno for kill System Callkill System Call Exampleraise System Callstty –aOutput 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 1sigsuspend – Example 2sigsuspend – Example 3sigsuspend – Example 4sigsuspend – Example 5sigwaitsigwait – Counts SIGUSR1 OccurancesAsync-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 Required SignalsSymbol MeaningSIGABRTSIGBUSSIGALRMSIGFPESIGHUPSIGILLSIGINTSIGKILLSIGPIPESIGQUITSIGSEGVSITTERMSIGURGSIGUSR1SIGUSR2Abnormal termination as initiated by abortAccess undefined part of memory objectTimeout 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 referenceTerminationHigh bandwidth data available at a socketUser-defined signal 1User-defined signal 2POSIX 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 CommandSYNOPSISkill –s signal_name pid…kill –l [exit status]kill [-signal_name] pid…kill [-signal number] pid…POSIX: Shell and UtilitiesSignal_Name Examplekill –USR1 3423 or kill –s USR1 3423•Sends SIGUSR1 to process 3423•USR1 is a symbolic name for the signal SIGUSR1Signal_Number Examplekill –9 3423•Sends signal number 9 (SIGKILL) to process 3423•The only supported signal number values are:0 for signal 01 for signal SIGHUP2 for signal SIGINT3 for signal SIGQUIT6 for signal SIGABRT9 for signal SIGKILL14 for signal SIGALRM15 for signal SIGTERMSignal Symbolic NamesTyping kill –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 <signal.h>int kill(pid_t pid, int sig);POSIX:CXerrno for kill System Call errno causeEINVAL sig is an invalid or unsupported signalEPERM caller does not have the appropriate privilegesESRCH no process or process group corresponds to pidkill System Call Example#include <stdio.h>#include <sys/types.h>#inlcude <signal.h>if (kill(3423, SIGUSR1) == -1)perror(“Could not send SIGUSR1”);if (kill (getppid(), SIGTERM) == -1)perror(“Error in kill”);raise System CallSYNOPSIS#include <signal.h>int raise(int sig);POSIX:CXA process can send a signal to itself with a raise system call – Example:if (raise (SIGUSR1) != 0)perror (“Failed to raise SIGUSR1”);stty –a Among other things, the stty –a output associates signals with control keystroke inputsOutput 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:#include <unistd.h>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


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?