Unix Signals
Unix Signals
3
Types of Interrupts
• Hardware interrupts (also called
external/asynchronous interrupts), are ones in
which the notification originates from a hardware
device such as a keyboard, mouse or system
clock.
• Software interrupts include exceptions and traps
• Exceptions: trigerred by an action of the process
without its knowledge (division by zero, access to paged
memory, etc.)
• Traps: trigerred by the process using system calls
• Usually, no interrupt should be ignored by the OS.
4
Interrupts:
The basic mechanism
1. The CPU receives the interrupt,
2. Control is transferred to the OS,
3. Current state is saved,
4. The request is serviced,
5. Previous state is restored,
6. Control is returned.
5
Signals
• Signals are notifications sent to a process in order to notify
it of various "important" events.
• Signals cause the process to stop whatever it is doing at
the moment, and force the process to handle them
immediately.
• The process may configure how it handles a signal.
• (Except for some signals that it cannot configure)
• Signals are different from interrupts:
• Signals are generated by the OS,
and received and handled by a process.
• Interrupts are generated by the hardware,
and received and handled by the OS.
• Signals and interrupts are both asynchronous
• Signals in unix have names and numbers.
• Use ‘man kill’ to see the types of signals
Definition of Signal
Signal: A notification of an event
Event gains attention of the OS
OS stops the application process immediately, sending it a signal
Signal handler executes to completion
Application process resumes where it left off
Process
movl
void handler(int iSig)
pushl
{
call f …
addl }
movl
.
.
.
signal 7
Triggers for Signals
Some examples for signal triggers:
– Asynchronous input from the user such as ^C
(SIGINT), or typing ‘kill pid’ at the shell
– The system or another process, for instance if an
alarm set by the process has timed out
(SIGALRM)
– An exception in hardware caused by the process,
such as Illegal memory access (SIGSEGV)
• The exception causes a hardware interrupt,
• The hardware interrupt is received by the OS
• The signal is generated by the OS and 'sent' to the process
– A debugger wishing to suspend or resume the
process
Sources
* Hardware
for Generating Signals
- 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.
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;
(a file containing the current memory image of the process)
- terminate.
* 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.
Sending Signals Using the Keyboard
The most common way of sending signals to
processes is using the keyboard:
• Ctrl-C: Causes the system to send an INT
signal (SIGINT) to the running process.
• Ctrl-Z: causes the system to send a TSTP
signal (SIGTSTP) to the running process.
• Ctrl-\:causes the system to send a ABRT
signal (SIGABRT) to the running process.
Sending Signals from
the Command Line
•The kill command sends a signal to a process.
Example: killpipe.c
alarm API - alarm
* Function
The alarm API requests the kernel to send the SIGALRM
signal after a certain number of real clock seconds.
* Include: <signal.h>
* Summary: unsigned int kill ( 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 elapsed time,
after which the kernel will send the SIGALRM signal to the
calling process.
Signals: Summary
• Signals are notifications sent to a process
• The OS causes the process to handle a
signal immediately the next time it runs.
• There are default signal handlers for
processes.
• These handlers can be changed using
signal or sigaction.
• To avoid race conditions, one usually needs
to block signals some of the time using
sigprocmask and/or sigaction.