Carnegie Mellon Introduction to Computer Systems 15 213 18 243 spring 2009 16th Lecture Mar 17th Instructors Gregory Kesden and Markus P schel Carnegie Mellon Signals Kernel Process Process Process using kill 32 types of signals Sent by updating bit in pending vector You can write your own signal handlers ID 2 9 11 14 17 Name SIGINT SIGKILL SIGSEGV SIGALRM SIGCHLD Default Action Terminate Terminate Terminate Dump Terminate Ignore Corresponding Event Interrupt e g ctl c from keyboard Kill program cannot override or ignore Segmentation violation Timer signal Child stopped or terminated Carnegie Mellon Signal Handlers as Concurrent Flows Process A Signal delivered Icurr Process B user code main kernel code context switch user code main kernel code Signal received user code handler kernel code Inext user code main context switch Carnegie Mellon Today More on signals Long jumps Virtual memory VM Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation Carnegie Mellon Sending Signals from the Keyboard Typing ctrl c ctrl z sends a SIGINT SIGTSTP to every job in the foreground process group SIGINT default action is to terminate each process SIGTSTP default action is to stop suspend each process pid 10 pgid 10 pid 20 pgid 20 Background job 1 Foreground job Child Child pid 21 pgid 20 pid 22 pgid 20 Foreground process group 20 Shell pid 32 pgid 32 Background process group 32 Background job 2 pid 40 pgid 40 Background process group 40 Carnegie Mellon Example of ctrl c and ctrl z bluefish forks 17 Child pid 28108 pgrp 28107 Parent pid 28107 pgrp 28107 types ctrl z Suspended bluefish ps w PID TTY STAT TIME COMMAND 27699 pts 8 Ss 0 00 tcsh 28107 pts 8 T 0 01 forks 17 28108 pts 8 T 0 01 forks 17 28109 pts 8 R 0 00 ps w bluefish fg forks 17 types ctrl c bluefish ps w PID TTY STAT TIME COMMAND 27699 pts 8 Ss 0 00 tcsh 28110 pts 8 R 0 00 ps w STAT process state Legend First letter S sleeping T stopped R running Second letter s session leader foreground proc group See man ps for more details Carnegie Mellon Signal Handler Funkiness int ccount 0 void child handler int sig int child status pid t pid wait child status ccount printf Received signal d from process d n sig pid void fork14 pid t pid N int i child status ccount N signal SIGCHLD child handler for i 0 i N i if pid i fork 0 sleep 1 deschedule child exit 0 Child Exit while ccount 0 pause Suspend until signal occurs Pending signals are not queued For each signal type just have single bit indicating whether or not signal is pending Even if multiple processes have sent this signal Carnegie Mellon Living With Nonqueuing Signals Must check for all terminated jobs Typically loop with wait void child handler2 int sig int child status pid t pid while pid waitpid 1 child status WNOHANG 0 ccount printf Received signal d from process d n sig pid void fork15 signal SIGCHLD child handler2 Carnegie Mellon Signal Handler Funkiness Cont Signal arrival during long system calls say a read Signal handler interrupts read call Linux upon return from signal handler the read call is restarted automatically Some other flavors of Unix can cause the read call to fail with an EINTER error number errno in this case the application program can restart the slow system call Subtle differences like these complicate the writing of portable code that uses signals Carnegie Mellon A Program That Reacts to Externally Generated Events Ctrl c include stdlib h include stdio h include signal h void handler int sig printf You think hitting ctrl c will stop the bomb n sleep 2 printf Well fflush stdout sleep 1 printf OK n exit 0 main signal SIGINT handler installs ctrl c handler while 1 Carnegie Mellon A Program That Reacts to Internally Generated Events include stdio h include signal h int beeps 0 SIGALRM handler void handler int sig printf BEEP n fflush stdout if beeps 5 alarm 1 else printf BOOM n exit 0 main signal SIGALRM handler alarm 1 send SIGALRM to process in 1 second while 1 handler returns here linux a out What happens Carnegie Mellon A Program That Reacts to Internally Generated Events include stdio h include signal h int beeps 0 SIGALRM handler void handler int sig printf BEEP n fflush stdout if beeps 5 alarm 1 else printf BOOM n exit 0 main signal SIGALRM handler alarm 1 send SIGALRM to process in 1 second while 1 handler returns here linux a out BEEP BEEP BEEP BEEP BEEP BOOM bass Carnegie Mellon Summary Signals provide process level exception handling Can generate from user programs Can define effect by declaring signal handler Some caveats Very high overhead 10 000 clock cycles Only use for exceptional conditions Don t have queues Just one bit for each pending signal type Carnegie Mellon Today More on signals Long jumps Virtual memory VM Overview and motivation VM as tool for caching VM as tool for memory management VM as tool for memory protection Address translation Carnegie Mellon Nonlocal Jumps setjmp longjmp Powerful but dangerous user level mechanism for transferring control to an arbitrary location Controlled to way to break the procedure call return discipline Useful for error recovery and signal handling int setjmp jmp buf buf Must be called before longjmp Identifies a return site for a subsequent longjmp Called once returns one or more times Implementation Remember where you are by storing the current register context stack pointer and PC value in jmp buf Return 0 Carnegie Mellon setjmp longjmp cont void longjmp jmp buf buf int i Meaning return from the setjmp remembered by jump buffer buf again this time returning i instead of 0 Called after setjmp Called once but never returns longjmp Implementation Restore register context stack pointer base pointer PC value from jump buffer buf Set eax the return value to i Jump to the location indicated by the PC stored in jump buf buf Carnegie Mellon setjmp longjmp Example include setjmp h jmp buf buf main if setjmp buf 0 printf back in main due to an error n else printf first time through n p1 p1 calls p2 which calls p3 p3 error checking code if error longjmp buf 1 Carnegie Mellon Limitations of Nonlocal Jumps Works within stack discipline Can only long jump to environment of function that has been called but not yet completed jmp buf env P1 if setjmp buf long jump to here else P2 P2 P2 P3 P3 longjmp buf 1 Before longjmp After longjmp buf P1 P2 P2 P2 P3 P1 Carnegie Mellon Limitations of Long Jumps cont Works within stack discipline Can only long
View Full Document