DOC PREVIEW
Harvey Mudd CS 105 - Exceptional Control Flow

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 FlowTopicsTopicsExceptionsProcess hierarchyShellsSignalsCS 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 thingFrom startup to shutdown, a CPU simply reads and executes (interprets) a sequence of instructions, one at a timeThis sequence is the system’s physical control flow (or flow of control)Physical control flowTime– 4 –CS 105Exceptional Control FlowExceptional Control FlowMechanisms for exceptional control flow exist at all levels of a computer systemLow-Level MechanismLow-Level MechanismExceptions 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 MechanismsProcess context switchSignalsNonlocal jumps (setjmp/longjmp)—ignored in this courseImplemented 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 VectorsEach type of event has a unique exception number kIndex 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 processorIndicated by setting the processor’s interrupt pin(s)Handler returns to “next” instruction.Examples:Examples:I/O interruptsHitting control-C (or any key) at the keyboardArrival of packet from networkArrival of data sector from diskHard-reset interruptHitting reset buttonSoft-reset interruptHitting 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:TrapsIntentionalExamples: system calls, breakpoint traps, special instructionsReturns control to “next” instructionFaultsUnintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable)Either re-executes faulting (“current”) instruction or abortsAbortsUnintentional and unrecoverableExamples: parity error, machine checkAborts current program or entire OS– 10 –CS 105Fault Example #1Fault Example #1User Process OSpage faultCreate page and load into memoryreturnevent movlMemory ReferenceMemory ReferenceUser writes to memory locationThat portion (page) of user’s memory is currently on diskPage handler must load page into physical memoryReturns to faulting instructionSuccessful 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 SystemExceptionsExceptionsHardware and operating system kernel softwareConcurrent processesConcurrent processesHardware timer and kernel softwareSignalsSignalsKernel softwareNon-local jumps (ignored in this class)Non-local jumps (ignored in this class)Application codeEvil in CC++/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 usersh – Original Unix Bourne shellcsh – 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 terminateWill never be reaped because shell (typically) will not terminateEventually 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

Harvey Mudd CS 105 - Exceptional Control Flow

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Exceptional Control Flow
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptional Control Flow and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptional Control Flow 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?