Presentation is loading. Please wait.

Presentation is loading. Please wait.

Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer.

Similar presentations


Presentation on theme: "Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer."— Presentation transcript:

1 Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer of control occurs here y = a + x; Answer: User events, such as... Hardware interrupts, such as...

2 Programming Language Support  ___________ (most assembler & machine languages, C, Modula-2, etc.)  _____________ (PL/1, early versions of Microsoft BASIC)  _______ (Java, Visual BASIC,.net, etc.)  _________ (Ada, Eiffel, Java, etc.)

3 computer memory 0 1 2 3 4 5 300 301 302 303... An interrupt is a hardware event capable of causing the processor to suspend normal execution and transfer control. An _____________, also called an ___________________________, is a code segment executed in response to an interrupt. Predefined for use by processor when a division by zero occurs. 300 RTI ISR instruction1 ISR instruction2 ISR instruction3 ISR The programmer must 1) write ISR code 2) ensure linkage to ISR ____ ≈ instruction produces a pseudo-interrupt

4 An ON condition is a software clause that may be included in a PL/1 program. The clause is executed when the specified condition occurs. Executing an ON condition instruction activates/reactivates the ON condition. Example ON SUBSCRIPTRANGE BEGIN; x = 3; END; suppose an array subscript violation occurs here ON SUBSCRIPTRANGE BEGIN; x = 4; END; suppose an array subscript violation occurs here

5 PL/1 even permits capturing variable changes via ON conditions. Example ON CHECK(myVar) PUT LIST(‘The myVar variable has changed’); Example ON KEY(12) GOSUB 123; Microsoft BASIC uses ON conditions to capture function key presses. 1-10 function keys F1 to F10 11 cursor up 12 cursor left 13 cursor right 14 cursor down

6 Events are an O-O way to process anticipated asynchronous behavior, especially from the user interface Each event causes the associated method to be called upon the proper object. An __________ such as the one built into the Java virtual machine “looks for” events. Sometimes the event-handling method is inherited & overridden. Sometimes the event-handling capability is ____________.

7 An exception is an abnormal, sometimes asynchronous, event. Ada was the first language to popularize exceptions. They are now widely used. String str = null; str = str.toLowerCase(); When an exception occurs, it is said that the exception is thrown. int j = 0; int k = 25/j; double[ ] arr = new double[3]; arr[3] = 29.4; Examples

8 throw ExceptionObject; Example It is also possible to simulate an exception by executing a throw instruction. where ExceptionObject is an object reference conforming to Exception (Exception is a class from java.lang.) public class runFractTest { public static void main(String[ ] args) { SiimpleFraction myFrac; myFrac = new SimpleFraction(1, 2); System.out.println(myFrac.realValue()); myFrac = new SimpleFraction(1, 0); System.out.println(myFrac.realValue()); } public class SimpleFraction { private int numerator, denominator; public SimpleFraction(int n, int d) { IllegalArgumentException error; if (d != 0) { numerator = n; denominator = d; } else { error = new IllegalArgumentException( “Fraction denominator of 0”); throw error; } public double realValue() { return numerator / denominator; }... }

9 try try { tryInstructionBody } catchClauses finallyClause...Body denotes a sequence of instructions. finallyClause is optional catchClauses (zero or more repetitions of the following) catch (exceptionClassName parmName ) { exceptionHandlerBody } finallyClause (optional) finally { finallyBody } exceptionClassName - class conforming to Throwable. parmName - serves as a parameter passed into catch.

10 try Semantics  Execution of the try begins by executing the tryInstructionBody.  During the execution of tryInstructionBody the catchClauses serve as exception handlers. The appropriate catchClause is selected by conformance to a catchClauseName  If a finallyClause is included, then it always executes after the try. System.out.println( “La Crosse” ); try { System.out.println( “River Falls” ); String str; System.out.println( str.trim() ); System.out.println( “Eau Claire” ); } catch ( NullPointerException e) { System.out.println( “Platteville” ); } catch ( ArithmeticException e) { System.out.println( “Oshkosh” ); } finally { System.out.println( “Whitewater” ); } Trace the following :

11 Details of Exception Handling Behavior When an exception is thrown execution proceeds as follows: 1)Normal instruction execution is suspended. 2)If the immediately enclosing try contains a matching catch, then the conforming catch clause serves as the exception handler. Otherwise, the current try body is aborted and the thrown exception is forwarded to the next instruction after the try. (If this is the last instruction in a method, then the exception is forwarded to the location of the call.) 3)Repeat Step 2 until an exception handler is located or all try instructions are exhausted. 4)The exception handler is executed, followed by finallyClause (if present). The remainder of the try instruction containing the catchClause is aborted and execution proceeds with the next instruction after the try. 5)If no matching catchClause is found, then this is considered to be an uncaught exception and the program terminates with a traceback.

12 A coroutine is a segment of program code. Coroutines transfer control back and forth among themselves. 1) The coroutine that issues the tranfer is suspended. 2) The coroutine that is the transfer’s target is resumed. Coroutine ACoroutine BCoroutine CCoroutine D wrLn(“A1”); wrLn(“B1”); wrLn(“C1”); wrLn(“D1”); resume B; resume C; resume D; resume A; wrLn(“A2”); wrLn(“B2”); wrLn(“C2”); wrLn(“D2”); resume D; resume C; resume B; resume C; wrLn(“A3”); wrLn(“B3”); wrLn(“C3”); wrLn(“D3”); resume C; resume C; resume B; resume B; Example


Download ppt "Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer."

Similar presentations


Ads by Google