+ All Categories
Home > Documents > * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm...

* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm...

Date post: 04-Jan-2016
Category:
Upload: austen-greene
View: 213 times
Download: 0 times
Share this document with a friend
41
UNIT 5 : UNIX Signals
Transcript

Slide 1

UNIT 5 :UNIX Signals* POSIX-Defined Signals * Signaling Processes* Signal Mask* sigaction* kill and sigaction* alarm

Topics Signaling Processes* SignalA signal is a notification to a process that an event has occurred. Signals are sometimes called software interrupts.* Features of Signal- Signal usually occur asynchronously.- The process does not know ahead of time exactly when a signal will occur. - Signal can be sent by one process to another process (or to itself) or by the kernel to a process.

Sources for Generating Signals* Hardware- A process attempts to access addresses outside its own address space.- Divides by zero.* Kernel- Notifying the process that an I/O device for which it has been waiting is available.* Other Processes - A child process notifying its parent process that it has terminated.* User- Pressing keyboard sequences that generate a quit, interrupt or stop signal.POSIX-Defined Signals (1)* SIGALRM: Alarm timer time-out. Generated by alarm( ) API. * SIGABRT: Abort process execution. Generated by abort( ) API. CF* SIGFPE: Illegal mathematical operation. CF* SIGHUP: Controlling terminal hang-up.* SIGILL: Execution of an illegal machine instruction. CF* SIGINT: Process interruption. Can be generated by or keys. * SIGKILL: Sure kill a process. Can be generated by kill -9 command. CF* SIGPIPE: Illegal write to a pipe. CF* SIGQUIT: Process quit. Generated by keys. CF* SIGSEGV: Segmentation fault. generated by de-referencing a NULL pointer. CFPOSIX-Defined Signals (2)* SIGTERM: process termination. Can be generated by kill command. CF* SIGUSR1: Reserved to be defined by user.* SIGUSR2: Reserved to be defined by user.* SIGCHLD: Sent to a parent process when its child process has terminated.* SIGCONT: Resume execution of a stopped process.* SIGSTOP: Stop a process execution.* SIGTTIN: Stop a background process when it tries to read from from its controlling terminal.* SIGTSTP: Stop a process execution by the control_Z keys.* SIGTTOU: Stop a background process when it tries to write to its controlling terminal.Signal Sourcesa processwindowmanagershell commandterminaldrivermemorymanagementkernelother userprocessesSIGWINCHSIGKILLSIGINTSIGHUPSIGQUITSIGALRMSIGPIPESIGUSR1Three Courses of Action Process that receives a signal can take one of three action:* Perform the system-specified default for the signal- notify the parent process that it is terminating;- generate a core file; SIGSEGV (a file containing the current memory image of the process)- terminate. except SIGCHLD and SIGPWR* Ignore the signal A process can do ignoring with all signal but two special signals: SIGSTOP and SIGKILL.* Catch the SignalWhen a process catches a signal, except SIGSTOP and SIGKILL, it invokes a special signal handing routine.ExampleA Data base management process updating a database file, should not be interrupted until it is finished, otherwise the file will be corrupted.Hence, this process should specify that all interrupt signals (e.g., SIGINT and SIGTERM) are to be ignored before it starts updating the database file.And restore the signals later. The UNIX Kernel supports of signalsWhen signal is generated for a process corresponding signal flag is set in the Process Table slot of the recipient processKernel then checks process U-area that contains an array of signal handling specifications.If the array entry for the signal is 0, the process will accept default action of the signalIf 1, the process ignores the signal, and discards itIf any other value, user-defined signal handler is executed

If there are different signals pending on a process, the order I undefined.If multiple instances of a signal are pending on a process, it is implementation dependentA single instance delivered orMultiple instance

The UNIX Kernel supports of signals (1)The way caught signals handled is unreliable.In UNIS System V.2:When a signal is caught, kernel resets the signal handler first.Call the user signal handling function specified for that signal.So, if there are multiple instances of a signal to a process, the process will catch only the first, all other are handled in default mannerThe UNIX Kernel supports of signals (2)Remedy?In BSD and POSIX.1 system:When a signal is caught, the kernel does not reset the signal handler,So, no need to reestablish the signal handling method.The kernel will block further delivery of the same signal, until the signal function is completed.signal(): library callSpecify a signal handler function to deal with a signal type.

#include typedef void Sigfunc(int); /* my defn */

Sigfunc *signal( int signo, Sigfunc *handler );signal returns a pointer to a function that returns an int (i.e. it returns a pointer to Sigfunc)

Returns previous signal disposition if ok, SIG_ERR on error.Actual PrototypeThe actual prototype, listed in the man page is a bit perplexing but is an expansion of the Sigfunc type: void (*signal(int signo, void(*handler)(int)))(int);

In Linux:typedef void (*sighandler_t)(int);

sig_handler_t signal(int signo, sighandler_t handler);

Signal returns a pointer to a function that returns an intSignal HandlingUse the signal handling library: signal.h

Then can use the signal call:

#include

void (*signal( int sig, void (*handler)(int))) (int) ;

signal returns a pointer to the PREVIOUS signal handler

#include typedef void Sigfunc(int); /* my defn */ Sigfunc *signal( int signo, Sigfunc *handler );

Signal is a functionthat takes twoarguments: sig and handlerThe signal to becaught or ignoredis given as argumentsigThe function to be calledwhen the specified signal is received is given as apointer to the function handlerThe handler function Receives a single integer Argument and returns voidThe signal function itselfreturns a pointer to a function. The return type is the same as the function that is passed in,i.e., a function that takes anint and returns a voidThe returned functiontakes a integer parameter.Exampleint main(){ signal( SIGINT, foo ); :

/* do usual things until SIGINT */return 0;}

void foo( int signo ){ : /* deal with SIGINT signal */

return; /* return to program */}sig_examp.c#include #include #include

void sig_usr( int signo ); /* handles two signals */

int main(){ int i = 0;if( signal( SIGUSR1,sig_usr ) == SIG_ERR )printf( Cannot catch SIGUSR1\n );if( signal( SIGUSR2,sig_usr ) == SIG_ERR ) printf(Cannot catch SIGUSR2\n);:continued: while(1) { printf( %2d\n, I ); pause(); /* pause until signal handler * has processed signal */ i++; } return 0;}continuedvoid sig_usr( int signo )/* argument is signal number */{ if( signo == SIGUSR1 ) printf(Received SIGUSR1\n); else if( signo == SIGUSR2 ) printf(Received SIGUSR2\n); else printf(Error: received signal%d\n, signo);

return;}Usage$ sig_examp &[1] 4720 0$ kill -USR1 4720Received SIGUSR1 1$ kill -USR2 4720Received SIGUSR2 2$ kill 4720 /* send SIGTERM */[1] + Terminated sig_examp &$Signal MaskUNIX system has a signal mask that defines which signals are blocked when generated to a process.A blocked signal depends on the recipient process to unblock it and handle it accordingly.sigprocmask API - Signal Mask (1)* FunctionA process can query or set its signal mask via the sigprocmask API.* Include: * Summary: int sigprocmask ( int cmd, const sigset_t *new_mask, sigset_t *old_mask);* Return:Success: 0Failure: -1Sets errno: Yes

Arguments of sigprocmask API - Signal Mask (2)* new_mask: defines a set of signals to be set or reset in a calling process signal mask.new_mask = NULL, current process signal mask unaltered.* cmd: specifies how the new_mask value is to be used:- SIG_SETMASK: Overrides the calling process signal mask with the value specified in the new_mask argument.- SIG_BLOCK: Adds the signals specified in the new_mask argument to the calling process signal mask.- SIG_UNBLOCK: Removes the signals specified in the new_mask argument from the calling process signal mask* old_mask: Address of a sigset_t variable that will be assigned the calling processings original signal mask. old_mask = NULL, no previous signal mask will be return.sigsetops APIs - Signal Mask (3)* int sigemptyset (sigset_t* sigmask);Clears all signal flags in the sigmask argument.* int sigaddset (sigset_t* sigmask, const int signal_num);Sets the flag corresponding to the signal_num signal in the sigmask argument.* int sigdelset (sigset_t* sigmask, const int signal_num);Clears the flag corresponding to the signal_num signal in the sigmask argument.* int sigfillset(sigset_t* sigmask);Sets all the signal flags in the sigmask argument.* int sigismember(const sigset_t* sigmask,const int signal_num);Returns 1 if the flag corresponding to the signal_num signal in the sigmask argument is set; zero: not set; -1: the call fails.sigprocmask Example -Signal Mask (4)/* The example checks whether the SIGINT signal is present in a process signal mask and adds it to the mask if it is not there.It clears the SIGSEGV signal from the process signal mask. */int main( ){ sigset_tsigmask;sigemptyset(&sigmask);/*initialize set */if(sigprocmask(0,0,&sigmask)==-1)/*get current signal mask*/ {perro(sigprocmask); exit(1); } else sigaddset(&sigmask, SIGINT); /* set SIGINT flag*/sigdelset(&sigmask, SIGSEGV); /* clear SIGSEGV flag */if (sigprocmask(SIG_SETMASK,&sigmask,0) == -1)perro(sigprocmask); /* set a new signal mask */}sigpending API- Signal Mask (5)* FunctionThe sigpending API can find out whether one or more signals are pending for a process and set up special signal handing methods for those signals before the process calls the sigprocmask API to unblock them.* Include: * Summary: int sigpending (sigset_t* sigmask); * Return: Success: 0; Failure: -1; Sets errno: Yes* Argument sigmask argument, to the sigpending API, is the address of a sigset_t-type variable and is assigned the set of signals pending for the calling process by the API.

sigpending Example - Signal Mask (6)/* The example reports to the console whether the SIGTERM signal is pending for the process. */int main ( ) {sigset_tsigmask;sigemptyset(&sigmask); /* initialize set */if (sigpending(&sigmask) == -1) /* Any signal is pending */perro(sigpending);else cout


Recommended