Slides 6 - Signals
Slides 6 - Signals
CONTENTS
• Introduction
• List of Signals
• Sending Signals via Keystrokes and via Commands
• Sending Signals via Function Call
• Signal Delivery and Handling
• Installing a Signal Handler
• Blocking Signals
• Alarms
• Interval Timers 2
Introduction
• Introduced in UNIX systems to simplify inter-process
communication.
• Used by the kernel to notify processes of system events.
• A signal is a short message sent to a process, or group of
processes, containing a number identifying the signal.
• A process can send a signal and interrupt another process.
• The process receiving the signal can provide a handler (function)
for handling this signal.
• Signals can be blocked, i.e., prevented from being received.
3
• A signal is a notification of an event
• Event gains attention of the OS
• OS stops the application process immediately, sending it a signal
• Kernel forces target process to “handle” signal.
• Signal handler executes to completion
• Application process resumes where it left off
4
List of Signals
Signal Value Action Comment
• SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process
• SIGINT 2 Term Interrupt from keyboard
• SIGQUIT 3 Core Quit from keyboard
• SIGILL 4 Core Illegal Instruction
• SIGABRT 6 Core Abort signal from abort(3)
• SIGFPE 8 Core Floating point exception
• SIGKILL 9 Term Kill signal
• SIGSEGV 11 Core Invalid memory reference
• SIGPIPE 13 Term Broken pipe: write to pipe with no readers
5
List of Signals (continued)
Signal Value Action Comment
• SIGALRM 14 Term Timer signal from alarm(2)
• SIGTERM 15 Term Termination signal
• SIGUSR1 30,10,16 Term User-defined signal 1
• SIGUSR2 31,12,17 Term User-defined signal 2
• SIGCHLD 20,17,18 Ign Child stopped or terminated
• SIGCONT 19,18,25 Cont Continue if stopped
• SIGSTOP 17,19,23 Stop Stop process
• SIGTSTP 18,20,24 Stop Stop typed at tty
• SIGTTIN 21,26 Stop tty input for background process
• SIGTTOU 22,27 Stop tty output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
6
Sending Signals via Keystrokes and via Commands
• Type Ctrl-c
• Keyboard sends hardware interrupt
• Hardware interrupt is handled by OS
• OS sends a 2/SIGINT signal
• Type Ctrl-z
• Keyboard sends hardware interrupt
• Hardware interrupt is handled by OS
• OS sends a 20/SIGTSTP signal
• Issue a “kill –sig pid” command
• OS sends a sig signal to the process whose id is pid
• Example: kill -SIGINT 1234 /*
• Issue a “fg” or “bg” command to a job
• OS sends a 18/SIGCONT signal
7
Sending Signals via Function Call
kill()
int kill(pid_t iPid, int iSig);
Example
pid_t iPid = getpid(); /* Process gets its id.*/
kill(iPid, SIGINT); /* Process sends itself a SIGINT signal */
8
Signal Handling
• Each signal type has a default handler
• Most default handlers exit the process
• A program can install its own handler for signals of any type
• Exceptions: A program cannot install its own handler for signals of type:
• 9/SIGKILL
• Default handler exits the process
• Catchable termination signal is 15/SIGTERM
• 19/SIGSTOP
• Default handler suspends the process
• Can resume the process with signal 18/SIGCONT
• Catchable suspension signal is 20/SIGTSTP
9
Signal Delivery and Handling
10
Signal-Related Data Structures
11
Installing a Signal Handler
• sigaction()
int sigaction(int iSig, const struct sigaction *psAction,
struct sigaction *psOldAction);
• iSig: The type of signal to be affected
• psAction: Pointer to a structure containing instructions on how to handle signals of
type iSig, including signal handler name and which signal types should be blocked
• psOldAction: (Irrelevant for our purposes)
• Installs an appropriate handler
• Automatically blocks signals of type iSig
• Returns 0 if successful
12
Example
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
13
/* Using the POSIX function sigaction() */ /* Using the old C90 signal() function.
Its behavior varies between
int main(void) { Linux versions.
It is advised to avoid its use. */
struct sigaction sAction;
sAction.sa_flags = 0;
sAction.sa_handler = myHandler; int main(void) {
sigemptyset(&sAction.sa_mask);
sigaction(SIGINT, &sAction, NULL); signal(SIGINT, myHandler);
14
Blocking Signals
Each process has a signal mask in the kernel
• OS uses the mask to decide which signals to deliver
• User program can modify mask with sigprocmask()
sigprocmask()
int sigprocmask(int iHow, const sigset_t *psSet, sigset_t *psOldSet);
• psSet: Pointer to a signal set
• psOldSet: (Irrelevant for our purposes)
• iHow: How to modify the signal mask
• SIG_BLOCK: Add psSet to the current mask
• SIG_UNBLOCK: Remove psSet from the current mask
• SIG_SETMASK: Install psSet as the signal mask
16
Alarms
alarm()
unsigned int alarm(unsigned int tsec);
• Sends signal 14/SIGALRM after tsec seconds
• Cancels pending alarm if tsec is 0
• Uses real time
• Time spent executing other processes counts
• Time spent waiting for user input counts
• Return value is meaningless
Used to implement time-outs 17
Alarms Example 1
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
signal(SIGALRM, myHandler);
alarm(2); /* Set an alarm. */
printf("Entering an infinite loop\n");
for (;;) ;
return 0;
} 19
Alarms Example 2
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
20
…
int main(void){
int i;
sigset_t sSet;
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/time.h>
23
…
int main(void)
{
struct itimerval sTimer;
signal(SIGPROF, myHandler);