Carnegie MellonIntroduction to Computer Systems15-213/18-243, spring 200916thLecture, Mar. 17thInstructors:Gregory Kesden and Markus PüschelCarnegie MellonSignals Kernel → Process Process → Process (using kill) 32 types of signals Sent by updating bit in pending vector You can write your own signal handlersID Name Default Action Corresponding Event2 SIGINT Terminate Interrupt (e.g., ctl-c from keyboard)9 SIGKILL Terminate Kill program (cannot override or ignore)11 SIGSEGV Terminate & Dump Segmentation violation14 SIGALRM Terminate Timer signal17 SIGCHLD Ignore Child stopped or terminatedCarnegie MellonSignal Handlers as Concurrent FlowsSignal deliveredSignal receivedProcess A Process Buser code (main)kernel codeuser code (main)kernel codeuser code (handler)context switchcontext switchkernel codeuser code (main)IcurrInextCarnegie MellonToday 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 translationCarnegie MellonSending 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 processFore-groundjobBack-groundjob #1Back-groundjob #2ShellChild Childpid=10pgid=10Foreground process group 20Backgroundprocess group 32Backgroundprocess group 40pid=20pgid=20pid=32pgid=32pid=40pgid=40pid=21pgid=20pid=22pgid=20Carnegie MellonExample of ctrl-c and ctrl-zbluefish> ./forks 17Child: pid=28108 pgrp=28107Parent: pid=28107 pgrp=28107<types ctrl-z>Suspendedbluefish> ps wPID TTY STAT TIME COMMAND27699 pts/8 Ss 0:00 -tcsh28107 pts/8 T 0:01 ./forks 1728108 pts/8 T 0:01 ./forks 1728109 pts/8 R+ 0:00 ps wbluefish> fg./forks 17<types ctrl-c>bluefish> ps wPID TTY STAT TIME COMMAND27699 pts/8 Ss 0:00 -tcsh28110 pts/8 R+ 0:00 ps wSTAT (process state) Legend:First letter:S: sleepingT: stoppedR: runningSecond letter:s: session leader+: foreground proc groupSee “man ps” for more detailsCarnegie MellonSignal Handler Funkiness 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 signalint 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 */}Carnegie MellonLiving With Nonqueuing Signals Must check for all terminated jobs Typically loop with waitvoid 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 MellonSignal 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 signalsCarnegie MellonA Program That Reacts toExternally 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 MellonA 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); } } linux> a.out<What happens??>main() { signal(SIGALRM, handler); alarm(1); /* send SIGALRM to process in 1 second */while (1) { /* handler returns here */ } }Carnegie MellonA 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 MellonSummary 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 typeCarnegie MellonToday 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 translationCarnegie MellonNonlocal 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 0Carnegie Mellonsetjmp/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 bufCarnegie
View Full Document