DOC PREVIEW
MIT 16 070 - Recitation IX

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]    16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]   Ada95 program is the execution of one or more tasks Each task represents a separate thread of control that proceeds independently and concurrently between the points where it interacts with other tasks. A task is an object of a task type. All objects of a given task type have the same entries (interface), and share the same code (body - execute the same algorithm) Different task objects of the same type may be parameterized using discriminants.16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected] When a server task accepts the entry call from a client task, the two tasks are said to be in a rendezvous.  At the beginning and the end of the rendezvous, data can be exchanged via parameters. When the rendezvous is over, the two tasks each continue execution in parallel. 16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected] Factorial istask type Task_Factorial is --Specificationentry Start( F:in Positive ); --Rendezvousentry Finish( Result:out Positive ); --Rendezvousend Task_Factorial;end Factorial;package Is_A_Prime istask type Task_Is_Prime is --Specificationentry Start( P:in Positive ); --Rendezvousentry Finish( Result:out Boolean ); --Rendezvousend Task_Is_Prime;end Is_A_Prime;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]package body Factorial istask body Task_Factorial is --ImplementationFactorial : Positive;Answer : Positive := 1;beginaccept Start( F:in Positive ) do --FactorialFactorial := F;end Start;for I in 2 .. Factorial loop --CalculateAnswer := Answer * I;end loop;accept Finish( Result:out Positive ) do --Return answerResult := Answer;end Finish;end Task_Factorial;end Pack_Factorial;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]with Ada.Text_Io, Ada.Integer_Text_Io;with Factorial;with Is_A_Prime;use Ada.Text_Io, Ada.Integer_Text_Io, Factorial, Is_A_Prime;procedure Main isThread_1 : Task_Factorial;Thread_2 : Task_Factorial;Thread_3 : Task_Is_Prime;Factorial: Positive;Prime : Boolean;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]beginThread_1.Start(5); --Start factorial calculationThread_2.Start(7); --Start factorial calculationThread_3.Start(97); --Start is_prime calculationPut("Factorial 5 is ");Thread_1.Finish( Factorial ); --Obtain resultPut( Factorial ); New_Line;Put("Factorial 8 is ");Thread_2.Finish( Factorial ); --Obtain resultPut( Factorial ); New_Line;Put("97 is a prime is ");Thread_3.Finish( Prime ); --Obtain resultif Prime then Put("True"); else Put("False"); end if;New_Line;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]Factorial 5 is 120Factorial 7 is 504097 is a prime is True  16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected] Read/WriteLike a procedure but may also have abarrier condition associated with the entry.If the barrier condition is false the entry isqueued until the barrier becomes trueEntryRead onlyA function may execute simultaneouslywith other executing functions. However, a function cannot execute if a procedure iscurrently executing.FunctionRead / WriteA procedure will only execute when noother units are being executed. If necessary the procedure will wait until the currently executing unit(s) have finishedProcedure16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected] !"protected Counter isprocedure Increment(New_Value: out Positive);privateData: Integer := 0;end Counter;protected body Counter isprocedure Increment(New_Value: out Positive) isbeginData := Data + 1;New_Value := Data;end Increment;end Counter;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected] ##protected Buffer isentry Put(X: in Item); entry Get(X: out Item); privateData: Item; Full: Boolean := False; End Buffer;protected body Buffer isentry Put(X: in Item) when not Full isbeginData := X; Full := True; end Put;entry Get(X: out Item) when Full isbeginX := Data; Full := False; end Get;end Buffer;16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]                                                                        end Producer ;task Consumer;task body Consumer isbegin          !   "                       end Consumer16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]    Issues a call of Get Acquires the lock  The barrier is false henceo Gets queued o Releases the lock. 16.070 — April 10/2003 — Jayakanth Srinivasan, [email protected]  Issues a first call of Put Acquires the lock Successfully executes the body of Put by Fills the buffer  Sets Full to False.  Before releasing the lock, Reevaluates the barriers  Checks the queues to see whether a suspended operation can now be performed.  Finds that it can and executes the body of the entry Get thereby emptying the buffer and causing the task Consumer to be marked as no longer blocked and thus eligible for


View Full Document

MIT 16 070 - Recitation IX

Documents in this Course
optim

optim

20 pages

Load more
Download Recitation IX
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 Recitation IX 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 Recitation IX 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?