Slide 1SignalsSignal Handlers as Concurrent FlowsTodaySending Signals from the KeyboardExample of ctrl-c and ctrl-zSignal Handler FunkinessLiving With Nonqueuing SignalsSignal Handler Funkiness (Cont.)A Program That Reacts to Externally Generated Events (Ctrl-c)A Program That Reacts to Internally Generated EventsA Program That Reacts to Internally Generated EventsSummaryTodayNonlocal Jumps: setjmp/longjmpsetjmp/longjmp (cont)setjmp/longjmp ExampleLimitations of Nonlocal JumpsLimitations of Long Jumps (cont.)Slide 20TodayVirtual Memory (Previous Lectures)Problem 1: How Does Everything Fit?Problem 2: Memory ManagementProblem 3: How To ProtectSolution: Level Of IndirectionAddress SpacesA System Using Physical AddressingA System Using Virtual AddressingWhy Virtual Memory (VM)?TodayVM as a Tool for CachingMemory Hierarchy: Core 2 DuoDRAM Cache OrganizationAddress Translation: Page TablesAddress Translation With a Page TablePage HitPage MissHandling Page FaultHandling Page FaultHandling Page FaultHandling Page FaultWhy does it work? LocalityTodayVM as a Tool for Memory ManagementVM as a Tool for Memory ManagementSimplifying Linking and LoadingTodayVM as a Tool for Memory ProtectionTodayAddress Translation: Page HitAddress Translation: Page FaultCarnegie MellonIntroduction to Computer Systems15-213/18-243, spring 200916th Lecture, Mar. 17thInstructors: Gregory Kesden and Markus PüschelCarnegie MellonSignalsKernel → ProcessProcess → Process (using kill)32 types of signalsSent by updating bit in pending vectorYou 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 MellonTodayMore on signalsLong jumpsVirtual memory (VM)Overview and motivationVM as tool for cachingVM as tool for memory managementVM as tool for memory protectionAddress translationCarnegie MellonSending Signals from the KeyboardTyping 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 w PID 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 w PID 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 FunkinessPending signals are not queuedFor each signal type, just have single bit indicating whether or not signal is pendingEven 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 SignalsMust check for all terminated jobsTypically 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() callLinux: upon return from signal handler, the read() call is restarted automaticallySome 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 callSubtle 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 MellonSummarySignals provide process-level exception handlingCan generate from user programsCan define effect by
View Full Document