Lecture 10 Signals and Signal Handlers Outline Signals
- Slides: 28
Lecture 10 Signals and Signal Handlers
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 1 November 28, 2020 this?
Signals • Notification to a process that an event has occurred • “software interrupt” • asynchronous (can happen at any time) 2 November 28, 2020
Signals • One process can send a signal to another process • Could be used as synchronization • . . . Or primitive form of IPC 3 November 28, 2020
Signal Generation Kernel is usually the signal’s source • Hardware exceptions, e. g. , • Divide by 0 • Segmentation fault • Malformed machine instruction • Software events, e. g. , • Input available on FD • Timers • Child process termination 4 November 28, 2020
Signal Definitions • Signals defined by unique integers starting at 1 • #include <signal. h> contains #define MACROS (SIGxxxx) • E. g. , CNTL-C in shell generates SIGINT (interrupt signal) • UNIX has 31 standard signals (1 – 31) • See Table 20 -1 5 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 6 November 28, 2020
Order of Events 1. Signal is generated 2. Signal is pending 3. Signal is delivered to process 4. Process takes action on signal 7 November 28, 2020
Order of Events • Normally, pending signal is delivered immediately to running process • However, process may be in code segment that cannot be interrupted • Processes can also block certain signals (using signal mask) 8 November 28, 2020
Signals NOT Queued • Multiple blocked signals will only (eventually) result in a single delivered signal ==> Signals are NOT queued. . . 9 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 10 November 28, 2020
Default Action Upon delivery, process carries out one of the following default actions, depending on the signal: • Signal is ignored • Process is terminated (killed) • Process is terminated and a core dump file is generated • Core dump file => image of process’s virtual memory state • Process is stopped • Process is resumed 11 November 28, 2020
Signal Disposition • Program can change action that occurs when signal delivered • Know as setting the disposition of the signal • Program can set the following dispositions: • Default action occurs • Signal is ignored • Signal handler is executed 12 November 28, 2020
Signal Handler • User-defined function that executes when signal delivered • E. g. , shell program handles SIGINT (CNTL-C) by returning to command prompt • E. g. , shell program handles SIGTSTP (CNTL-Z) by suspending the foreground process 13 November 28, 2020
Terminology • “Register a signal handler” => inform kernel that function should be invoked upon signal generation • “Signal has been caught” => registered function has been invoked in response to signal 14 November 28, 2020
Signal Handler Order of Events 15 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 16 November 28, 2020
signal() void ( * signal ( int sig , void ( *handler )( int ) ) ) ( int ) ; • • #include <signal. h> System call to change signal disposition Returns previous signal disposition on success Returns SIG_ERR on error 17 November 28, 2020
signal() void ( * signal ( int sig , void ( *handler )( int ) ) ) ( int ) ; • sig argument => signal whose disposition to change • handler => pointer to function that will be invoked • Returns nothing (void) • Takes an integer • void handler(int sig); 18 November 28, 2020
signal() Example • Register function fx() to handle SIGINT signal: //outside of main: void fx ( int sig ) { // code goes here } //somewhere in main: signal ( SIGINT, fx ); 19 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 20 November 28, 2020
kill() int kill ( pid_t pid, int sig ); • • • #include <signal. h> Allows one process to send any signal to another process Returns 0 on success Returns -1 on error pid => PID of process to send signal to (-1 for broadcast) sig => signal to be sent 21 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 22 November 28, 2020
raise() int raise ( int sig ); • • #include <signal. h> Process sends signal to itself Returns 0 on success Returns nonzero on error • raise( sig ) <==> kill ( getpid() , sig ) 23 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 24 November 28, 2020
pause() int pause( ); • • #include <signal. h> Suspends execution until call interrupted by signal Always returns -1 (with ERRNO set) E. g. , //do nothing, wait for signals while(1) pause(); 25 November 28, 2020
Outline • • Signals Order of Events Signal Disposition signal() kill() raise() pause() Live Coding 26 November 28, 2020
Live Coding • Create signal handler(s) for SIGxxxx • Use raise() to test within • Use kill() to send to another process (including broadcast) • Use pause() to “wait” for signals 27 November 28, 2020
- Communicative signals and informative signals
- Difference between communicative and informative signals
- Communicative and informative signals
- Baseband signal and bandpass signal
- Baseband signal and bandpass signal
- Product of two odd signal is
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Digital signal as a composite analog signal
- Nafdac food handlers test
- Food handlers can contaminate food when they:
- Classification of pipeline processors
- Gila county food handlers
- After which activity must food handlers wash their hands?
- 123 food handlers
- Payment handlers meaning
- Reportable symptoms of food handlers include
- Food handlers can contaminate food when they
- Lecture outline example
- Lecture outline example
- Lecture outline example
- Lecture outline meaning
- Sandwich
- Electricity and magnetism lecture notes
- Power system dynamics and stability lecture notes
- Microbial physiology and metabolism lecture notes
- Limits fits and tolerances
- Cloud computing lecture
- Fundamental deviation
- Financial intermediaries ppt