Download presentation
Presentation is loading. Please wait.
1
1 Exceptions and Concurrency Exceptions and Concurrency
2
2 Error Handling Run-time errors happen Run-time errors happen File I/O errors File I/O errors Out of memory Out of memory Bugs Bugs How can we handle them How can we handle them Without obscuring the normal flow of control Without obscuring the normal flow of control Without duplicating a lot of code Without duplicating a lot of code At an appropriate place At an appropriate place Without causing more errors Without causing more errors Error handling code can be very error prone since Error handling code can be very error prone since The state of the system cannot be depended on The state of the system cannot be depended on It is difficult to test exhaustively It is difficult to test exhaustively
3
3 Exceptions General mechanism for handling abnormal conditions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors, other illegalities Predefined exceptions: constraint violations, I/O errors, communication errors, other illegalities User-defined exceptions for robust abstractions User-defined exceptions for robust abstractions Predefined exception raised by the runtime system. User-defined exception can be raised (thrown) by user code. Predefined exception raised by the runtime system. User-defined exception can be raised (thrown) by user code. Exception handlers specify remedial actions or proper shutdown Exception handlers specify remedial actions or proper shutdown Exceptions can be stored and re-raised later Exceptions can be stored and re-raised later
4
4 Predefined exceptions in Ada Defined in Standard: Defined in Standard: Constraint_Error : value out of range Constraint_Error : value out of range Program_Error : illegality not detectable at compile-time: unelaborated package, exception during finalization... Program_Error : illegality not detectable at compile-time: unelaborated package, exception during finalization... Storage_Error : allocation cannot be satisfied (heap or stack) Storage_Error : allocation cannot be satisfied (heap or stack) Tasking _Error : communication failure Tasking _Error : communication failure Defined in Ada.IO_Exceptions: Defined in Ada.IO_Exceptions: Data_Error, End_Error, Name_Error, Use_Error, Mode_Error, Status_Error, Device_Error Data_Error, End_Error, Name_Error, Use_Error, Mode_Error, Status_Error, Device_Error
5
5 Handling exceptions Any begin-end block can have an exception handler: Any begin-end block can have an exception handler: procedure test is procedure test is x : integer := 25; x : integer := 25; y : integer := 0; y : integer := 0; begin begin x := x / y; x := x / y; exception exception when Constraint_Error => Put_Line (“as expected”); when Constraint_Error => Put_Line (“as expected”); when others => Put_Line (“out of the blue!”); when others => Put_Line (“out of the blue!”); end; end;
6
6 A common idiom with Integer_Text_Io; use Integer_Text_Io; with Integer_Text_Io; use Integer_Text_Io; function Get_Data return Integer is function Get_Data return Integer is X : Integer; X : Integer; begin begin loop -- as long as input is not legal loop -- as long as input is not legal begin begin Get (X); Get (X); return X; -- if here, input is valid return X; -- if here, input is valid exception exception when others => Put_Line (“input must be integer, try again”); when others => Put_Line (“input must be integer, try again”); end; end; end loop; end loop; end; end;
7
7 Exception propagation When an exception is raised, control is passed directly to the nearest handler for that exception The current sequence of statements is abandoned The current sequence of statements is abandoned All functions on the stack called after the one containing the handler for the exception are discarded. All functions on the stack called after the one containing the handler for the exception are discarded. If no handler is found, the program terminates If no handler is found, the program terminates The current frame is never resumed The current frame is never resumed
8
8 Unwinding the Stack If the exception is raised in a block for which an exception handler is present: If the exception is raised in a block for which an exception handler is present: The handler is executed The handler is executed Control resumes at the next statement following the begin-end block Control resumes at the next statement following the begin-end block Otherwise, Otherwise, the current stack frame is discarded the current stack frame is discarded The caller is found by following a dynamic chain. The caller is found by following a dynamic chain. The exception is raised at the point of the call in the callers stack frame The exception is raised at the point of the call in the callers stack frame The process repeats until a handler is found. The process repeats until a handler is found.
9
9 User-defined Exceptions Client-server contract: if inputs are proper, either the output is correct or else client is notified of failure. The inputs are the responsibility of the client (the caller). Client-server contract: if inputs are proper, either the output is correct or else client is notified of failure. The inputs are the responsibility of the client (the caller). package Stacks is package Stacks is Stack_Empty : exception; Stack_Empty : exception; … package body Stacks is package body Stacks is procedure Pop (X : out Integer; From : in out Stack) is procedure Pop (X : out Integer; From : in out Stack) is begin begin if Empty (From) then raise Stack_Empty; if Empty (From) then raise Stack_Empty; else else......
10
10 The scope of exceptions Exception has the same visibility as other declared entities: to handle an exception it must be visible in the handler Exception has the same visibility as other declared entities: to handle an exception it must be visible in the handler An others clause can handle unameable exceptions partially An others clause can handle unameable exceptions partially when others => when others => Put_Line (“disaster somewhere”); Put_Line (“disaster somewhere”); raise; -- propagate exception, raise; -- propagate exception, -- program will terminate if exception is never caught -- program will terminate if exception is never caught
11
11 Exception information An exception is not a type: we cannot declare exception variables and assign to them An exception is not a type: we cannot declare exception variables and assign to them An exception occurrence is a value that can be stored and examined An exception occurrence is a value that can be stored and examined An exception occurrence may include additional information: source location of occurrence, contents of stack, etc. An exception occurrence may include additional information: source location of occurrence, contents of stack, etc. Predefined package Ada.Exceptions contains needed machinery. Predefined package Ada.Exceptions contains needed machinery.
12
12 Ada.Exceptions package Ada.Exceptions is package Ada.Exceptions is type Exception_Id is private; type Exception_Id is private; type Exception_Occurrence is limited private; type Exception_Occurrence is limited private; function Exception_Identity (X : Exception_Occurrence) function Exception_Identity (X : Exception_Occurrence) return Exception_Id; return Exception_Id; function Exception_Name (X : Exception_Occurrence) return String; function Exception_Name (X : Exception_Occurrence) return String; procedure Save_Occurrence procedure Save_Occurrence (Target : out Exception_Occurrence; (Target : out Exception_Occurrence; Source : Exception_Occurrence); Source : Exception_Occurrence); procedure Raise_Exception (E : Exception_Id; Message : in String := “”) procedure Raise_Exception (E : Exception_Id; Message : in String := “”)......
13
13 Using exception information exception when Expected : Constraint_Error => when Expected : Constraint_Error => Save_Occurrence (Event_Log, Expected); Save_Occurrence (Event_Log, Expected); when Trouble : others => when Trouble : others => Put_Line (“unexpected “ & Exception_Name (Trouble) & “ raised”); Put_Line (“unexpected “ & Exception_Name (Trouble) & “ raised”); Put_Line (“shutting down”); Put_Line (“shutting down”); raise; raise;......
14
14 Exceptions in C++ Same runtime model Same runtime model Exceptions are classes Exceptions are classes Handlers appear in try blocks Handlers appear in try blocks try { try { some_complex_calculation (); some_complex_calculation (); } catch (range_error) { // range error might be raised catch (range_error) { // range error might be raised // in some_complex_calculation // in some_complex_calculation cerr << “oops\n”; cerr << “oops\n”; catch (zero_divide) { // ditto for zero_divide catch (zero_divide) { // ditto for zero_divide cerr << “why is x zero?\n”; cerr << “why is x zero?\n”; }
15
15 Defining and throwing exceptions The program throws an object. There is nothing in the declaration to indicate it will be used as an exception. The program throws an object. There is nothing in the declaration to indicate it will be used as an exception. struct Zero_Divide { struct Zero_Divide { public: public: int lineno; // useful information int lineno; // useful information Zero_Divide () {…} // constructor Zero_Divide () {…} // constructor … try { try { … if (x == 0) throw Zero_Divide (..); // call constructor and go if (x == 0) throw Zero_Divide (..); // call constructor and go
16
16 Exceptions and inheritance A handler names a class, and can handle an object of a derived class as well: A handler names a class, and can handle an object of a derived class as well: class Matherr {}; // a bare object, no info class Matherr {}; // a bare object, no info class Overflow: public Matherr {…}; class Overflow: public Matherr {…}; class Underflow: public Matherr {…}; class Underflow: public Matherr {…}; class Zero_Divide: public Matherr {…}; class Zero_Divide: public Matherr {…}; try { try { weather_prediction_model (…); weather_prediction_model (…); // who knows what will happen // who knows what will happen } catch (Overflow) {… // e.g. change parameters in caller catch (Overflow) {… // e.g. change parameters in caller catch (Matherr) { … // Underflow, Zero_Divide handled her catch (Matherr) { … // Underflow, Zero_Divide handled her catch (…); // handle anything else (ellipsis) catch (…); // handle anything else (ellipsis)
17
17 Handling Exception Hierarchy In C++ Exceptions are handled in order In C++ Exceptions are handled in order try { myFile.open(); myFile.open(); buffer = myFile.read(128); buffer = myFile.read(128); } catch (FileNotFoundException e) { … } catch (EndOfFileException e) {… } catch {FileIOException e) { } catch {IOException e) {…}} Uses the first matching type (including base types) Uses the first matching type (including base types) So, more general exceptions must follow more specific ones So, more general exceptions must follow more specific ones
18
18 Exceptions in Java Model and terminology similar to C++: Model and terminology similar to C++: exceptions are objects that are thrown and caught exceptions are objects that are thrown and caught try blocks have handlers, which are examined in succession try blocks have handlers, which are examined in succession a handler for an exception can handle any object of a derived class a handler for an exception can handle any object of a derived class Differences: Differences: all exceptions are extension of predefined class Throwable all exceptions are extension of predefined class Throwable checked exceptions are part of method declaration checked exceptions are part of method declaration the finally clause specifies clean-up actions that are always executed the finally clause specifies clean-up actions that are always executed
19
19 If a method might throw an exception, callers should know about it public void replace (String name, Object newvalue) public void replace (String name, Object newvalue) throws NoSuchName { throws NoSuchName { Attribute attr := find (name); Attribute attr := find (name); if (attr == null) { if (attr == null) { throw new NoSuchName (name); throw new NoSuchName (name); } newvalue.update (attr); } newvalue.update (attr); } Caller must have a handler for NoSuchName, or else must be declared as throwing NoSuchName itself. Caller must have a handler for NoSuchName, or else must be declared as throwing NoSuchName itself. Only required for checked exceptions (not predefined ones, which are extensions of RuntimeException and Error). Only required for checked exceptions (not predefined ones, which are extensions of RuntimeException and Error).
20
20 And Finally… Some cleanups must be performed whether the method terminates normally or throws an exception. Some cleanups must be performed whether the method terminates normally or throws an exception. public void encrypt (String file) throws StreamException { public void encrypt (String file) throws StreamException { Stream input; Stream input; try { try { input = new Stream (file); input = new Stream (file); iterator Words = new iterator (input); iterator Words = new iterator (input); for (word w = Words.init (); Words.more(); w = Words.next()) { for (word w = Words.init (); Words.more(); w = Words.next()) { RSAencode(word); // may fail somewhere RSAencode(word); // may fail somewhere } finally { if (input != null) input.close(); }; //regardless of how we exit finally { if (input != null) input.close(); }; //regardless of how we exit
21
21 Tasks and Concurrency
22
22 Tasking Processes and Threads Processes and Threads Concurrent Programming Concurrent Programming Declaration, creation, activation, termination Declaration, creation, activation, termination Synchronization and communication Synchronization and communication Semaphores Semaphores Monitors Monitors Conditional communication Conditional communication Language Support Language Support C++ relies on libraries C++ relies on libraries ADA and Java include language support ADA and Java include language support What about purely functional programs? What about purely functional programs?
23
23 Processes v. Threads Each running program is a process that runs in a separate address space Each running program is a process that runs in a separate address space Process started by user or by another program Process started by user or by another program Communication between processes controlled by the Operating System Communication between processes controlled by the Operating System A Process may have many threads A Process may have many threads Threads are sometimes called lightweight processes Threads are sometimes called lightweight processes Each thread within a process Each thread within a process Shares the same address space (heap and static memory) Shares the same address space (heap and static memory) Has its own stack and local environment Has its own stack and local environment Has its own program counter Has its own program counter Executes independently of other threads Executes independently of other threads Often executes the same code as other threads Often executes the same code as other threads
24
24 Concurrent programming Synchronous and asynchronous models of communication Synchronous and asynchronous models of communication Description of simultaneous, independent activities Description of simultaneous, independent activities A task is an independent thread of control, with own stack, program counter and local environment A task is an independent thread of control, with own stack, program counter and local environment Tasks communicate through Tasks communicate through Rendez-vous Rendez-vous protected objects protected objects Shared variables Shared variables
25
25 Task Declarations in Ada A task type is a limited type A task type is a limited type task type worker; -- declaration; public interface task type worker; -- declaration; public interface type Worker_Id is access worker; -- a conventional access type type Worker_Id is access worker; -- a conventional access type task body worker is -- actions performed in lifetime task body worker is -- actions performed in lifetime begin begin loop -- forever. Will be shutdown from the outside. compute; loop -- forever. Will be shutdown from the outside. compute; end loop; end loop; end worker; end worker;
26
26 Task Declarations in Ada A task type can be a component of a composite. A task type can be a component of a composite. The number of tasks in a program is not fixed at compile-time. The number of tasks in a program is not fixed at compile-time. W1, W2 : Worker; -- two individual tasks W1, W2 : Worker; -- two individual tasks type Crew is array (Integer range <>) of worker; type Crew is array (Integer range <>) of worker; First_Shift : Crew (1.. 10); -- a group of tasks First_Shift : Crew (1.. 10); -- a group of tasks type Monitored is record type Monitored is record Counter : Integer; Counter : Integer; Agent : Worker; Agent : Worker; end record; end record;
27
27 Task Activation in Ada When does a task start running? When does a task start running? If statically allocated, at the next begin If statically allocated, at the next begin If dynamically allocated, at the point of allocation. If dynamically allocated, at the point of allocation. declare declare W1, W2 : Worker; W1, W2 : Worker; Joe : Worker_Id := new Worker; -- Starts working at once Joe : Worker_Id := new Worker; -- Starts working at once Third_Shift : Crew (1..N); -- some number of them Third_Shift : Crew (1..N); -- some number of them begin -- activate W1, W2, and the third_shift begin -- activate W1, W2, and the third_shift … end; -- wait for them (not Joe) -- to complete end; -- wait for them (not Joe) -- to complete
28
28 Task Services in Ada A task can perform some actions on request from another task A task can perform some actions on request from another task The interface (declaration) of the task specifies the available actions (entries) The interface (declaration) of the task specifies the available actions (entries) A task can also execute some actions on its own behalf, without external requests or communication. A task can also execute some actions on its own behalf, without external requests or communication. task type Device is task type Device is entry Read (X : out Integer); entry Read (X : out Integer); entry Write (X : Integer); entry Write (X : Integer); end Device; end Device;
29
29 Tasks in Java Code to run in thread is in a class that implements the interface Runnable and its run method Code to run in thread is in a class that implements the interface Runnable and its run method Start Thread by Start Thread by Creating a Thread object from the Runnable object Creating a Thread object from the Runnable object Calling Thread.start Calling Thread.start class f implements Runnable { void run() {…} } new Thread(f).start(); The Thread class provides methods to name, group, suspend, or interrupt the thread.d The Thread class provides methods to name, group, suspend, or interrupt the thread.d
30
30 Synchronization in Java Synchronize keyword is applied to objects Synchronize keyword is applied to objects Synchronized methods apply to “this” object Synchronized methods apply to “this” object Synchronized static methods apply to “Class” object Synchronized static methods apply to “Class” object Introduces a monitor for code that reads and writes to common memory Introduces a monitor for code that reads and writes to common memory Does not prevent deadlocks Does not prevent deadlocks
31
31 Shared Variables 1. void LinkList::insert(Element newElt, Element after) { 2. newElt.next = after.next; 3. newElt.prev = after; 4. after.next.prev = newElt; 5. after.next = newElt; 6 } What if another thread gains control at line 4 and inserts another element at the same place? What if another thread gains control at line 4 and inserts another element at the same place? Need to be able to lock an object so other threads cannot interfere. Need to be able to lock an object so other threads cannot interfere. lock list; lock list; list.insert(newElt, after); list.insert(newElt, after); unlock list; unlock list; What if we need to lock two lists in two threads? What if we need to lock two lists in two threads? // Thread A// Thread B lock list a; lock list b; lock list b;lock list a; If thread B interrupts thread A between lock statements – Deadlock! If thread B interrupts thread A between lock statements – Deadlock!
32
32 Spin Locks A spin lock is an operating system primitive A spin lock is an operating system primitive One thread obtains a spin lock One thread obtains a spin lock Other threads awaiting the lock “spin”, until the lock is released Other threads awaiting the lock “spin”, until the lock is released Raises IRQ and Dispatch level so that no other threads can run. (adequate for single processor) Raises IRQ and Dispatch level so that no other threads can run. (adequate for single processor) Threads running on other processors must use atomic instructions (e.g., test-and-set) to determine when lock is freed. Threads running on other processors must use atomic instructions (e.g., test-and-set) to determine when lock is freed. Can stop all other processing while it is held Can stop all other processing while it is held Does not require overhead of a context switch Does not require overhead of a context switch
33
33 Semaphores Developed by Dijkstra in the 1960’s Developed by Dijkstra in the 1960’s Semaphore is a structure S containing Semaphore is a structure S containing A counter s initialized to some positive value A counter s initialized to some positive value A queue q of threads waiting to enter the guarded section of code. A queue q of threads waiting to enter the guarded section of code. P(S) Guards entry to a section S of code P(S) Guards entry to a section S of code P(S) := s -= 1; if (s < 0) add task to queue q and block P(S) := s -= 1; if (s < 0) add task to queue q and block V(S) V(S) V(S) := s += 1; if (s <= 0) activate a task in queue, q V(S) := s += 1; if (s <= 0) activate a task in queue, q If s is negative, |s| is the number of queued tasks If s is negative, |s| is the number of queued tasks If s is positive, s is the number of simultaneous threads allowed in critical section (usually 1) If s is positive, s is the number of simultaneous threads allowed in critical section (usually 1) The definitions of P and V are considered atomic operations The definitions of P and V are considered atomic operations
34
34 Mutex and Critical Region Mutex (Mutual-Exclusion) Mutex (Mutual-Exclusion) Ensures that only one thread is executing in a critical region Ensures that only one thread is executing in a critical region Critical Region Critical Region A region of code protected by mutual exclusion A region of code protected by mutual exclusion EnterCriticalRegion(); EnterCriticalRegion(); doSomething()… doSomething()… ExitCriticalRegion ExitCriticalRegion Programmer has to make sure no execution (including exceptions) path fails to close the mutex Programmer has to make sure no execution (including exceptions) path fails to close the mutex
35
35 Monitors Protects a section of code so that only one thread can execute the code at a time Protects a section of code so that only one thread can execute the code at a time Defines a syntactic unit, avoids problem of having to explicitly end a critical region Defines a syntactic unit, avoids problem of having to explicitly end a critical region Uses semaphores to block additional threads Uses semaphores to block additional threads Used by Java Synchronization Used by Java Synchronization
36
36 Synchronization: Rendezvous 1. Caller makes explicit request: entry call 2. Callee (server) states its availability: accept statement 3. If server is not available, caller blocks and queues up on the entry for later service 4. If both present and ready, parameters are transmitted to server. 5. Server performs action 6. Out parameters are transmitted to caller 7. Caller and server continue execution independently
37
37 Example Simple mechanism to create critical sections: section of code that must be executed by only one task at a time Simple mechanism to create critical sections: section of code that must be executed by only one task at a time task type semaphore is task type semaphore is entry P; -- Dijkstra’s terminology entry P; -- Dijkstra’s terminology entry V; -- from the Dutch entry V; -- from the Dutch end semaphore; end semaphore; task body semaphore is task body semaphore is begin begin loop loop accept P; -- won’t accept another P until a caller asks for V accept P; -- won’t accept another P until a caller asks for V accept V; accept V; end loop; end loop; end semaphore; end semaphore;
38
38 Using a semaphore A task that needs exclusive access to the critical section executes: A task that needs exclusive access to the critical section executes: Sema.P; Sema.P; -- critical section code -- critical section code Sema.V; Sema.V; If in the meantime another task calls Sema.P, it blocks, because the semaphore does not accept a call to P until after the next call to V: the other task is blocked until the current one releases by making an entry call to V. If in the meantime another task calls Sema.P, it blocks, because the semaphore does not accept a call to P until after the next call to V: the other task is blocked until the current one releases by making an entry call to V. programming hazards: programming hazards: someone else may call V : race condition someone else may call V : race condition no one calls V: other callers are deadlocked. no one calls V: other callers are deadlocked.
39
39 Conditional Communication Need to protect against excessive delays, deadlock, starvation, caused by missing or malfunctioning tasks. Need to protect against excessive delays, deadlock, starvation, caused by missing or malfunctioning tasks. Timed entry call: caller waits for rendezvous a stated amount of time: Timed entry call: caller waits for rendezvous a stated amount of time: select select Disk.Write (value => 12, Track => 123); -- Disk is a task Disk.Write (value => 12, Track => 123); -- Disk is a task or or delay 0.2; delay 0.2; end select; end select; If Disk does not accept within 0.2 Secs, go do something else. If Disk does not accept within 0.2 Secs, go do something else.
40
40 Conditional Communication (ii) Conditional entry call: caller ready for rendezvous only if no one else is queued, and rendezvous can begin at once: Conditional entry call: caller ready for rendezvous only if no one else is queued, and rendezvous can begin at once: select select Disk.Write (value => 12, Track => 123); Disk.Write (value => 12, Track => 123); else else Put_Line (“device busy”); Put_Line (“device busy”); end select; end select; Print message if call cannot be accepted immediately. Print message if call cannot be accepted immediately.
41
41 Conditional communication (iii) The server may accept a call only if the internal state of the task is appropriate: The server may accept a call only if the internal state of the task is appropriate: select select when not full => accept Write (Val : Integer); when not full => accept Write (Val : Integer); or or when not empty => accept Read (Var : out Integer); when not empty => accept Read (Var : out Integer); or or delay 0.2; -- maybe something will happen delay 0.2; -- maybe something will happen end select; end select; If several guards are open and callers are present, any one of the calls may be accepted : non-determinism. If several guards are open and callers are present, any one of the calls may be accepted : non-determinism.
42
42 Ada Protected Objects Protected types contain data that tasks can access only through a set of protected operations. Similar to Java Synchronized objects. Protected types contain data that tasks can access only through a set of protected operations. Similar to Java Synchronized objects. Protected functions Protected functions Read-only access to the internal data. Read-only access to the internal data. Multiple tasks may simultaneously call a protected function. Multiple tasks may simultaneously call a protected function. Protected procedures Protected procedures Exclusive read-write access to the internal data. Exclusive read-write access to the internal data. Only one task at a time can interact with the protected type. Only one task at a time can interact with the protected type. Protected entries Protected entries Like protected procedures except that they add a barrier condition. Like protected procedures except that they add a barrier condition. A barrier is a Boolean expression that must become true before the caller may proceed. A barrier is a Boolean expression that must become true before the caller may proceed. If the barrier is not true when the caller makes a request, the caller is placed in a queue to wait until the barrier becomes true. If the barrier is not true when the caller makes a request, the caller is placed in a queue to wait until the barrier becomes true.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.