+ All Categories
Home > Documents > Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Date post: 17-Jan-2016
Category:
Upload: meryl-mcbride
View: 222 times
Download: 3 times
Share this document with a friend
26
Operating Systems Recitation 4, April 14- 15 th , 2002 Signals
Transcript
Page 1: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Operating Systems

Recitation 4, April 14-15th, 2002

Signals

Page 2: Operating Systems Recitation 4, April 14-15 th, 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.

Page 3: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

generated by event

sent to process

received & handledby process

associateddefault action

catch by invokinguser-defined

handlerignore

abortdump

ignore stopcontinue

Page 4: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 5: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 6: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 7: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 8: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 9: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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

Page 10: Operating Systems Recitation 4, April 14-15 th, 2002 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.

Page 11: Operating Systems Recitation 4, April 14-15 th, 2002 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).

Page 12: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Signals# signal 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

9 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

15 SIGTERM termination a process (can be ignored) abort

10 SIGUSR1 available to processes abort

Page 13: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 14: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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)

Page 15: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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

Page 16: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Example#include <signal.h>

main(int argc, char* argv[]){

// install signal handlerssignal(SIGTERM, sig_handler); // sent by default kill commandsignal(SIGSEGV, sig_handler); // segmentation faultsignal(SIGUSR1, sig_handler); // user defined signal

}

void sig_handler(int sig){

if (sig == SIGTERM) … // handle default kill commandelse if (sig == SIGSEGV) … // handle segmentation faultelse if (sig == SIGUSR1) printf(“received SIGUSR1\n”);…

}

Page 17: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

kill command

• Send any signal to a process, misnomer$ a.out &

[1]1019

$ kill –USR1 1019

received SIGUSR1

$ kill 1019

I will survive

Page 18: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

kill function

#include <sys/types.h>

#include <signal.h>

int kill(pid_t pid, int signo);

• Returns 0 if OK, -1 on error.

Page 19: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 20: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 21: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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?

Page 22: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.

Page 23: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

Exercise notes

• Before running the program, limit the number of processes that can be created under the shell with the command:$ limit maxproc 10where 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

Page 24: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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 expressionwhere: in the programkill: abort the process running under gdb’s controlquit: exit gdbinfo, help

Page 25: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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]

Page 26: Operating Systems Recitation 4, April 14-15 th, 2002 Signals.

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.


Recommended