Download presentation
Presentation is loading. Please wait.
Published byAusten Greene Modified over 9 years ago
2
* POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm Topics
3
Signaling Processes * Signal A 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.
4
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.
5
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. CF
6
POSIX-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.
7
Signal Sources a process window manager shell command terminal driver memory management kernel other user processes SIGWINCH SIGKILL SIGINTSIGHUP SIGQUIT SIGALRM SIGPIPE SIGUSR1
8
Three 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 Signal When a process catches a signal, except SIGSTOP and SIGKILL, it invokes a special signal handing routine.
9
Example A 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.
10
The UNIX Kernel supports of signals 1. When signal is generated for a process – corresponding signal flag is set in the Process Table slot of the recipient process 2. Kernel 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 signal If 1, the process ignores the signal, and discards it If any other value, user-defined signal handler is executed
11
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 dependent A single instance delivered or Multiple instance The UNIX Kernel supports of signals (1)
12
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 manner The UNIX Kernel supports of signals (2)
13
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.
14
signal(): library call Specify 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.
15
Actual Prototype The 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 int
16
Signal Handling Use 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 function that takes two arguments: sig and handler The signal to be caught or ignored is given as argument sig The function to be called when the specified signal is received is given as a pointer to the function handler The handler function Receives a single integer Argument and returns void The signal function itself returns a pointer to a function. The return type is the same as the function that is passed in, i.e., a function that takes an int and returns a void The returned function takes a integer parameter.
17
Example int main() { signal( SIGINT, foo ); : /* do usual things until SIGINT */ return 0; } void foo( int signo ) { : /* deal with SIGINT signal */ return; /* return to program */ }
18
sig_examp.c #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
19
: while(1) { printf( “%2d\n“, I ); pause(); /* pause until signal handler * has processed signal */ i++; } return 0; } continued
20
void 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; }
21
Usage $ sig_examp & [1] 4720 0 $ kill -USR1 4720 Received SIGUSR1 1 $ kill -USR2 4720 Received SIGUSR2 2 $ kill 4720 /* send SIGTERM */ [1] + Terminated sig_examp & $
22
Signal Mask UNIX 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.
23
sigprocmask API - Signal Mask (1) * Function A 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: 0 Failure: -1 Sets errno: Yes
24
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 processing’s original signal mask. old_mask = NULL, no previous signal mask will be return.
25
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.
26
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 */ }
27
sigpending API- Signal Mask (5) * Function The 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.
28
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<<”SIGTERM signal is:” << (sigismember (&sigmask,SIGTERM) ? “Set” : “No Set”); /* whether SIGTERM signal in the sigmask argument is set? */
29
sigaction API - sigaction (1) * Function.The sigaction API setups a signal handling method for each signal it wants to deal with and passes back the previous signal handling method for a given signal. The sigaction API blocks the signal it is catching allowing a process to specify additional signals to be blocked when the API is handling a signal. * Include: * Summary: int sigaction ( int signal_num, struct sigaction* action, struct sigaction* old_action); * Return: Success: 0; Failure: -1; Sets errno: Yes
30
struct sigaction - sigaction (2) * struct sigaction { void(*sa_handler) (int); sigset_tsa_mask; intsa_flag; } * sa_handler is the function pointer of a user-defined signal handler function, SIG_IGN (ignores a signal), or SIG_DFL (accepts the default action of a signal). * sa_mask specifies additional signals that a process wishes to block when it is handling the signal_num signal. * sa_flag specifies special handling for certain signals.
31
sa_flag value - sigaction (3) 0:when the signal_num is SIGCHLD, the kernel will send the SIGCHLD signal to the calling process whenever its child process is either terminated or stopped. SA_NOCLDSTOP: when the signal_num is SIGCHLD, the kernel will generate the SIGCHLD signal to the calling process whenever its child process has terminated, but not when the child process has been stopped. SA_RESETHAND: If signal_num is caught, the sa_handler is set to SIG_DFL before the signal handler function is called, and signal_num will not be added to the process signal mask SA_RESTART: If a signal is caught while a process is executing a system call, the kernel will restart the system call after the signal handler returns. If this flag is not set in the sa_flag, after the signal handler returns, the system call will be aborted with a return value of -1 and will set errno to EINTR (aborted due to a signal interruption).
32
Arguments of sigaction - sigaction (4) * signal_num The signal_num argument designates which signal handling action is defined in the action argument. * old_action The previous signal handling method for signal_num will be returned via the old_action argument if it is not a NULL pointer. * action action argument sets up a signal handling method for the signal_num argument. If the action argument is a NULL pointer, the calling process’s existing signal handling method for signal_num will be unchanged. ! Example: sigaction.C
33
SIGCHLD and waitpid API Sent to the parent when a child process terminates or stops Different event may occur Parent accepts the default action of the SIGCHLD The API returns child’s exit status and process ID to the parent, Kernel clears this PID in the parents Process table A parent process can call waitpid API repeatedly to wait for each child Parent ignores the SIGCHLD signal Signal is discarded, leaving parent undisturbed even if it is executing waitpid. Parent catches the SIGCHLD signal The signal handler function will be called in the parent process. The waitpid may restarted
34
sigsetjmp and siglongjmp Functions There is a problem in calling longjmp. When a signal is caught, the signal-catching function is entered with the current signal automatically being added to the signal mask of the process. This prevents subsequent occurrences of that signal from interrupting the signal handler. If we longjmp out of the signal handler, what happens to the signal mask for the process?
35
prototype #include int sigsetjmp(sigjmp_buf env, int savemask); Returns: 0 if called directly, nonzero if returning from a call to siglongjmp void siglongjmp(sigjmp_buf env, int val);
36
The only difference between these functions and the setjmp and longjmp functions is that sigsetjmp has an additional argument. If savemask is nonzero, then sigsetjmp also saves the current signal mask of the process in env. When siglongjmp is called, if the env argument was saved by a call to sigsetjmp with a nonzero savemask, then siglongjmp restores the saved signal mask. Usually called from user-defined signal handling fuctions, bez a process signal mask is modified when a signal handler is called. So, call siglongjmp to restore.
37
kill API - kill and sigaction(1) * Function - A process can send a signal to a related process via the kill API. - This is a simple means of interprocess communication or control. - The sender and recipient processes must be related such that either the sender process real or effective user ID matches that of the recipient process, or the sender process has superuser privileges. * Include: * Summary: int kill ( pid_t pid, int signal_num); * Return: Success: 0; Failure: -1; Sets errno: Yes
38
Arguments of kill - kill and sigaction (2) *int signal_num: the integer value of a signal to be sent to one or more processes designated by pid. *pid_t pid value - positive value: pid is process ID. Sends signal_num to that process. - 0: sends signal_num to all process whose group ID is the same as the calling process. - -1: Sends signal_num to all processes whose real user ID is the same as the effective user ID of the calling process. ! Example: kill.C (using kill and sigaction)
39
Raise The raise function allows a process to send a signal to itself. int raise(int signo); The call raise(signo); is equivalent to the call kill(getpid(), signo);
40
alarm API - alarm * Function The alarm API requests the kernel to send the SIGALRM signal after a certain number of real clock seconds. * Include: * Summary: unsigned int alarm ( unsigned int time_interval); * Return: Success: the number of CPU seconds left in the process timer; Failure: -1; Sets errno: Yes * Argument time_interval: the number of CPU seconds elapse time. After which the kernel will send the SIGALRM signal to the calling process.
41
pause Functions The pause function suspends the calling process until a signal is caught #include int pause(void); Returns: 1 with errno set to EINTR
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.