PPL Unit-4
PPL Unit-4
PPL Unit-4
PROGRAMMING
• Everything is an object
– Advantage - elegance and purity
– Disadvantage - slow operations on simple objects
• Add objects to a complete typing system
– Advantage - fast operations on simple objects
– Disadvantage - results in a confusing type system (two
kinds of entities)
• Include an imperative-style typing system for
primitives but make everything else objects
– Advantage - fast operations on simple objects and a
relatively small typing system
– Disadvantage - still some confusion because of the two
type systems
• Semaphores
• Monitors
• Message Passing
• Dijkstra - 1965
• A semaphore is a data structure consisting of a
counter and a queue for storing task descriptors
• Semaphores can be used to implement guards on
the code that accesses shared data structures
• Semaphores have only two operations, wait and
release (originally called P and V by Dijkstra)
• Semaphores can be used to provide both
competition and cooperation synchronization
wait(aSemaphore)
if aSemaphore’s counter > 0 then
decrement aSemaphore’s counter
else
put the caller in aSemaphore’s queue
attempt to transfer control to a ready task
-- if the task ready queue is empty,
-- deadlock occurs
end
release(aSemaphore)
if aSemaphore’s queue is empty then
increment aSemaphore’s counter
else
put the calling task in the task ready queue
transfer control to a task from aSemaphore’s queue
end
task consumer;
loop
wait (fullspots);{wait till not empty}}
FETCH(VALUE);
release(emptyspots); {increase empty}
-- consume VALUE –-
end loop;
end consumer;
• Ada, Java, C#
• The idea: encapsulate the shared data and
its operations to restrict access
• A monitor is an abstract data type for
shared data
task Task_Example is
entry ENTRY_1 (Item : in Integer);
end Task_Example;
task Gas_Station_Attendant is
entry Service_Island (Car : Car_Type);
entry Garage (Car : Car_Type);
end Gas_Station_Attendant;
task Binary_Semaphore is
entry Wait;
entry release;
end Binary_Semaphore;
exception_choice form:
exception_name | others
try {
for (index = 0; index < 100; index++) {
…
if (…) {
return;
} //** end of if
} //** end of try clause
finally {
…
} //** end of try construct