Exceptional Control FlowControl FlowSlide 4ExceptionsInterrupt VectorsAsynchronous Exceptions (Interrupts)Synchronous ExceptionsFault Example #1ECF Exists at All Levels of a SystemShell ProgramsSimple Shell eval FunctionProblem with Simple Shell ExampleSignalsSignal ConceptsSignal Concepts (cont.)Slide 27Receiving SignalsSending Signals with killSending Signals From the KeyboardDefault ActionsInstalling Signal HandlersSignal Handling ExampleSignal Handler FunkinessLiving With Nonqueuing SignalsSummaryExceptional Control FlowExceptional Control FlowTopicsTopicsExceptionsProcess hierarchyShellsSignalsCS 105“Tour of the Black Holes of Computing”– 2 –CS 105Control FlowControl Flow<startup>inst1inst2inst3…instn<shutdown>Computers do only one thingComputers do only one thingFrom startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a timeThis sequence is the system’s physical control flow (or flow of control)Physical control flowTime– 4 –CS 105Exceptional Control FlowExceptional Control FlowMechanisms for exceptional control flow exist at all levels of a computer systemLow-Level MechanismLow-Level MechanismExceptions Change in control flow in response to a system event (i.e., change in system state)Combination of hardware and OS softwareHigher-Level MechanismsHigher-Level MechanismsProcess context switchSignalsNonlocal jumps (setjmp/longjmp)—ignored in this courseImplemented by either:OS software (context switch and signals)C language runtime library: nonlocal jumps– 5 –CS 105ExceptionsAn An exceptionexception is a transfer of control to the OS in response is a transfer of control to the OS in response to some to some eventevent (i.e., change in processor state) (i.e., change in processor state)User Process OSexceptionexception processingby exception handlerexception return (optional)event currentnextThink of it as a hardware-initiated function callThink of it as a hardware-initiated function call– 6 –CS 105Interrupt VectorsInterrupt VectorsEach type of event has a unique exception number kIndex into jump table (a.k.a., interrupt vector)Jump table entry k points to a function (exception handler).Handler k is called each time exception k occurs. interruptvector012...n-1code for exception handler 0code for exception handler 0code for exception handler 1code for exception handler 1code forexception handler 2code forexception handler 2code for exception handler n-1code for exception handler n-1...Exception numbers– 7 –CS 105Asynchronous Exceptions (Interrupts)Asynchronous Exceptions (Interrupts)Caused by events external to processorCaused by events external to processorIndicated by setting the processor’s interrupt pin(s)Handler returns to “next” instruction.Examples:Examples:I/O interruptsHitting control-C (or any key) at the keyboardArrival of packet from networkArrival of data sector from diskHard-reset interruptHitting reset buttonSoft-reset interruptHitting control-alt-delete on a PC– 8 –CS 105Synchronous ExceptionsSynchronous ExceptionsCaused by events that occur as result of executing an Caused by events that occur as result of executing an instruction:instruction:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable)Either re-executes faulting (“current”) instruction or abortsAbortsUnintentional and unrecoverableExamples: parity error, machine checkAborts current program or entire OS– 10 –CS 105Fault Example #1Fault Example #1User Process OSpage faultCreate page and load into memoryreturnevent movlMemory ReferenceMemory ReferenceUser writes to memory locationThat portion (page) of user’s memory is currently on diskPage handler must load page into physical memoryReturns to faulting instructionSuccessful on second tryint a[1000];main (){ a[500] = 13;} 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10– 13 –CS 105ECF Exists at All Levelsof a SystemECF Exists at All Levelsof a SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumps (ignored in this class)Non-local jumps (ignored in this class)Application codeEvil in CC++/Python throw/catch– 21 –CS 105Shell ProgramsShell ProgramsA A shellshell is an application program that runs programs on is an application program that runs programs on behalf of the userbehalf of the usersh – Original Unix Bourne shellcsh – BSD Unix C shell, tcsh – Enhanced C shell (both deprecated)bash – “Bourne-Again” shell, zsh – “Z” shell int main() { char cmdline[MAXLINE]; while (1) {/* read */printf("> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0);/* evaluate */eval(cmdline); } }Execution is a sequence of Execution is a sequence of read/evaluate stepsread/evaluate steps– 22 –CS 105Simple Shell eval FunctionSimple Shell eval Functionvoid eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execvp() */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ execvp(argv[0], argv); fprintf(stderr, "%s: Command not found.\n", argv[0]); exit(1);}if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0)unix_error("waitfg: waitpid error");}else /* otherwise, don’t wait for bg job */ printf("%d %s", pid, cmdline); }}– 23 –CS 105Problem with Simple Shell ExampleProblem with Simple Shell ExampleShell correctly waits for and reaps foreground jobsShell correctly waits for and reaps foreground jobsBut what about background jobs?But what about background jobs?Will become zombies when they terminateWill never be reaped because shell (typically) will not terminateEventually you hit process limit and can’t do any workSolution: Reaping background jobs requires Solution: Reaping background jobs requires mechanism called a mechanism called a signalsignal– 24
View Full Document