default action description signal

Download Report

Transcript default action description signal

Operating Systems
Recitation 4, April 14-15th, 2002
Signals
Motivation
• Notify a process or a group of processes
that a specific event occurred.
• Force a process to execute a signal handler
function included in its code.
generated by event
sent to process
received & handled
by process
associated
default action
abort
dump
ignore
catch by invoking
user-defined
handler
continue
stop
ignore
Signals
• Represented by a number identifying the
signal.
• May be sent to a process at any time at
any state (signals sent to a non-running
process must be saved by the kernel until
that process resumes execution).
• Consumable, each signal sent can be
received once.
Generating a signal
• By a process or internally generated by
the kernel.
• Some examples: divide by 0 (hardware
exception), child terminated, a timer
expired, terminal generated signal, calling
the kill function.
Sending a signal
Kernel operations
• Check whether a process has the
privileges to send a signal to another
process.
• Update destination process descriptor.
• May wake up the process and force it to
receive the signal.
• Check whether the signal nullifies other
pending signals for a process.
Receiving a signal
•
•
•
Kernel forces the destination process to react
to the signal by changing its execution state or
by starting the execution of a specified signal
handler (or both).
For example, the kernel noticed the arrival of
the signal, updated the process descriptor of
the process that is supposed to receive the
signal. In case that process was not running on
the CPU at the moment, the kernel deferred the
task of waking it up until now.
Check for non-blocked pending signals, loop
until no more are left.
Handling options
•
•
Ignore.
Perform default action predefined by the kernel
depending on the type of signal:
–
–
–
–
–
•
Abort: process is terminated
Dump: abort and a core file containing execution
context is created (for debug purposes), contains
process address space and CPU registers.
Ignore.
Stop: put process in stopped state.
Continue: if stopped then back to running state.
Catch the signal by calling a user-defined
function, the signal handler.
Blocking specific signals
• Signals of a specific type can be selectively
blocked by a process and not received.
• Different from ignoring a signal that is received
(as ignoring an email or a ringing phone is
different from blocking an email such that we
don’t receive it).
sigprocmask system call - examine or modify the
set of blocked signals
Pending signals
• Signals sent but not yet received are pending.
• Only one pending of any given type for each process,
additional pending are discarded.
Examples
• If a signal that is blocked is generated for a process & if
action for that signal is default action or to catch, then
the signal remains pending, until the process unblocks it
or changes its action to ignore.
• Non-blocked pending signal
sigpending system call – examine the set of
pending blocked signals.
Signal handler
• User-defined function.
• Cannot be interrupted by another
occurrence of the handled signal. When a
process executes a signal-handler function
it masks the corresponding signal
(automatically blocking the signal until the
handler terminates).
Signals
#
description
default action
17 SIGCHLD
sent to parent when child terminates
ignore
8
SIGFPE
arithmetic exception (for example divide by 0)
dump = abort +
core
SIGINT
terminal interrupt key (delete, Ctrl+c)
abort = terminate
SIGKILL
terminate a process
abort
13 SIGPIPE
writes to a pipe but the reader has terminated
abort
30 SIGPWR
power failure (for example to switch to UPS)
ignore
11 SIGSEGV
process has made invalid memory reference
dump
9
signal
15 SIGTERM termination a process (can be ignored)
abort
10 SIGUSR1
abort
available to processes
Tasks
• Remember which signals are blocked by
which process.
• Determine whether the signal can be
ignored.
• Handle the signal, which may require
switching the process to a handler function
at any point during its execution and
restoring the original execution context
after the function returns.
Data structures
•
•
•
•
•
•
Signals sent to a process: array of bits, one for each
signal type.
Blocked signals: array of bits.
Flag set if one or more non-blocked signals are
pending.
Pointer to a signal data structure describing how each
signal is handled.
A structure that is shared, specifying how signals are
handled by several process.
Functions and macros that operate on these
structures, set bits in arrays, logical operations (for
example AND of pending signals with blocked signals,
and see what is left)
Signal function
#include <signal.h>
void (*signal (int signo, void (*func)(int)))(int);
• Returns previous disposition of signal if OK, SIG_ERR
on error.
• signo – integer, name of the signal
• func – pointer to a function that takes a single integer
and returns nothing.
– SIG_IGN, ignore
– SIG_DFL, default action
– address of function to be called when the signal occurs, handler
• Returns a pointer to a function that returns nothing,
which is the previous signal handler
Example
#include <signal.h>
main(int argc, char* argv[])
{
// install signal handlers
signal(SIGTERM, sig_handler); // sent by default kill command
signal(SIGSEGV, sig_handler); // segmentation fault
signal(SIGUSR1, sig_handler); // user defined signal
}
void sig_handler(int sig)
{
if (sig == SIGTERM) … // handle default kill command
else if (sig == SIGSEGV) … // handle segmentation fault
else if (sig == SIGUSR1) printf(“received SIGUSR1\n”);
…
}
kill command
• Send any signal to a process, misnomer
$ a.out &
[1]1019
$ kill –USR1 1019
received SIGUSR1
$ kill 1019
I will survive
kill function
#include <sys/types.h>
#include <signal.h>
…
int kill(pid_t pid, int signo);
• Returns 0 if OK, -1 on error.
sleep function
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
• Returns 0 or the number of un-slept seconds.
• Causes the calling process to be suspended
until either
– seconds has elapsed
– A signal is caught by the process and the signal
handler returns.
Exercise description
1. Write a program that types the string
“continue?” and waits for the input ‘y’. For any
other input print the question again and wait for
an answer. After receiving ‘y’ the program
continues, and puts a value in address 0:
char* ptr = NULL;
…
*ptr = ‘x’;
2. Run the program and when it waits for the
input send it a SIGTERM signal from another
window using the kill command.
3. Add a signal handler. Check the type of signal
and if its SIGTERM then print: I will survive.
Install the signal handler in the main. Explain
what happens when you run the program and
try to kill the process using the kill command
(SIGTERM).
4. Explain what happens when you try to kill the
process by using the command kill -9
(SIGKILL). Can this case be handled like
SIGTERM?
5. Handle the signal SIGSEGV as well, and print
the same string if it occurs. Explain what
happens when the program reaches the line
assigning a value to address 0.
6. Change the signal handler to execute gdb
when the program receives a SIGSEGV signal.
Store the process ID using getpid and create a
new process using fork. The child process
should use execv to run the debugger with the
ID of the parent, whereas the parent process
should sleep until the debugger is activated.
7. Explain what happens when you run the
program.
Exercise notes
• Before running the program, limit the number of
processes that can be created under the shell with the
command:
$ limit maxproc 10
where 10 is the limit on the number of processes.
• Use the following arguments to execute gdb:
–
–
–
–
–
–
“/usr/X11R6/bin/xterm” (which is also the 1st argument for execv)
“-e”
“gdb”
argv[0] of main
parents PID as a string (use the function sprintf to convert it).
NULL
gdb - GNU DeBugger
• Allows you to step through C,C++ programs in order to
find the point at which they break. The program to be
debugged is normally specified on the command, you
can also specify a core or, if you want to investigate a
running program, a process ID.
• Common commands:
run: execute the program.
c: continue execution from a breakpoint.
print: print the value of a variable or expression
where: in the program
kill: abort the process running under gdb’s control
quit: exit gdb
info, help
Exercise submission
• Monday, April 29th.
• Software
– directory: ~username/os02b/ex-sig
– files: ex-sig.c
– permissions: chmod ugo+rx (to above)
• Hardcopy
–
–
–
–
name, ID, login, CID
ex-sig.c
answers to question.
submit in mailbox 380, Elad Sarver,
[email protected]
References
• Operating Systems, chapter 4.11, Sivan
Toledo, 2001.
• Advanced Programming in the UNIX
Environment, chapter 10, Richard
Stevens, 1992.
• For more details see: Understanding the
Linux Kernel, chapter 9, Bovet & Cesati,
2000.