Eiesenberg and McGuire (1972) solution for mutual Exclusion Problem

advertisement
EIESENBERG AND MCGUIRE (1972) SOLUTION FOR MUTUAL
EXCLUSION PROBLEM
//globals
int procphase[N]={out_cr,..out_cr}, turn=0;
Pi(){ //i-th process, i=0,..,N-1
//entry protocol
do{
procphase[i]=want_cr;
j=turn;
//1st while loop
while(j!=i){ //don’t check for j=i
if(procphase[j]==out_cr) j=(j+1)%N;
// % - remainder after integer
//division
else j=turn;
// restart scan
}
procphase [i]=claim_cr;
j=(i+1)%N; //no other willing processes
while(procphase[j]!=claim_cr)j=(j+1)%N;
//2nd while loop
// are there other competing processes?
}while((j!=i)||(turn!=i&&procphase[turn]!=out_cr));
turn=i;
critical section
j=(turn+1)%N; //exit protocol for i-th process
while(procphase[j]==out_cr)j=(j+1)%N;
turn=j;//if there is other interesting process, turn will be modified, otherwise
//it will get again value of i
procphase[i]=out_cr;
}
EVENTS
Events are similar to binary semaphores, and have 2 operations
defined on them: wait and post, but contrary to semaphores, when event is
post, all waiting it processes are resumed. Usually events are used to
synchronize main parent and child processes, for example, as in PL/1:
PTSK:
PROC MAIN (MAIN, TASK)
..
CALL A EVENT (V1)
CALL B EVENT (V2)
..
WAIT (V1, V2)
Events can’t solve mutual exclusion problem.
CRITICAL REGIONS
Critical region (section) is a part of code working with critical
resource. It may be explicitly declared:
VAR
V: SHARED INTEGER;
BEGIN
REGION V DO S1, …, SN; END;
Also, there may be used conditional critical regions:
REGION V WHEN B DO S1, …, SN; END;
MONITORS
Monitor’ procedures are mutually exclusive, i.e. two processes can’t
simultaneously run its procedures. If required resource is busy when process
calls monitor, process must be suspended, and other processes will be able to
monitors’ procedures. Operations wait and signal are applied to condition
variables guarding resources. Signal operation allows to activate one of
suspended via wait operation processes from the queue of processes to this
variable. Monitors are used in Modula 2, Java.
RESOURCE: MONITOR;
VAR
BUSY: BOOLEAN;
NONBUSY: CONDITION;
AQUIRE: PROC;
BEGIN
IF BUSY THEN NONBUSY.WAIT
ELSE BUSY:=TRUE
END;
RELEASE: PROC;
BEGIN
BUSY:=FALSE;
NONBUSY.SIGNAL;
END;
BEGIN
BUSY:=FALSE
END.
CONTROL PATH
Sequence of actions can be shown with the help of special operations:
";" – sequential execution;
"," – possibility of parallel execution;
"*" – multiple undetermined execution.
Thus, following graph representing required way of execution
A
B
C
D
may be depicted as
PATH
A;*(B, C);D
END;
ADA LANGUAGE’S RENDEZVOUS
ADA language was designed in the beginning of 1980-s (ADA-83) as
a respond for requirement made by US Department of Defense for standard
language for developing software applications, real-time applications in the
first turn. Development of this language is ADA-95, it supersedes ADA-83.
In comparison with ADA-83 it has new tasking features such as
- a pragma Task_Dispatching_Policy that controls how tasks are
dispatched
- a pragma Locking_Policy that controls way of locking shared
resources by tasks. Two mostly used approaches to such locking
are Original Ceiling Priority Protocol (OCPP) and Immediate
Ceiling Priority Protocol (ICPP).When either of these protocols are
used in a single-processor system, then a high-priority process can
be
blocked at most once during execution by lower-priority
process, deadlocks are prevented, transitive blocking is prevented
(process c is blocked by process b, being blocked by process a, and
so on), mutual exclusive access to resources is ensured.
ORIGINAL CEILING PRIORITY PROTOCOL (OCPP)
1. Each process has a static default priority assigned (for
example, by deadline monotonic scheme: less deadline –
greater priority, or by rate monotonic scheme: less period –
greater priority).
2. Each resource has a static ceiling value defied, this is the
maximum priority of the processes that use it (assume that
greater priority is represented by greater value).
3. A process has a dynamic priority which is the maximum of
its own static priority and any it inherits due to it blocking
higher-priority processes (when process blocks higherpriority task it gets temporarily this higher priority).
4. A process can only lock a resource if its dynamic priority is
higher than the ceiling of any currently locked resource
(excluding any that it has already locked).
Protocol ensures that if a resource is locked by process a, and that could lead
to the blocking of higher-priority process b, then no other resource that could
block process b is allowed to be locked by any process other than a. A
process can therefore be delayed not only due to attempting to lock a
previously locked resource, but also when the lock could lead to multiple
blocking on higher-priority processes.
PRIORITY INVERSION EXAMPLE
Priority inversion, i.e. blocking of higher-priority task by lower priority
ones. Static priorities assigned according to deadline monotonic scheme.
Tasks are as follows:
Process
Priority
Execution
Release time
sequence
A
1
EQQQQE
0
B
2
EE
2
C
3
EVVE
2
D
4
EEQVE
4
So, requests will appear at moments 0,2,2,4 (release times), and process D
has highest priority (more priority – more priority value). Processes during
time quantum either execute (E) not requiring access to shared critical
resources, either require access to critical resources (Q or V), their behavior
pattern is represented by ‘Execution sequence’ column in the table above.
Time diagram of their execution follows:
D
E
E
b
b
b
b
C
E
V
P
P
V
E
B
P
P
P
P
P
P
E
E
P
P
P
P
P
P
P
P
A
E
Q
b
b
b
Q
V
E
Q
Q
Q
P
P
P
‘P’ means preempted status of respective task, ‘b’ means blocking of the
task due to resource request.
E
ORIGINAL CEILING PRIORITY PROTOCOL (OCPP) EXAMPLE
(PRIORITY INHERITANCE)
D
E
E
b
b
Q
V
E
C
E
b
P
P
b
b
P
P
P
V
V
E
B
P
b
P
P
b
b
P
P
P
P
P
P
E
E
P
Q
P
P
Q
Q
P
P
P
P
P
P
P
P
A
E
Q
Process A again locks the 1st critical section Q, as no other resources have
been locked. It is again preempted by process C, but now attempt by C to
lock the second section (V) is not successful as its priority (3) is not higher
than the current ceiling (which is 4, as Q is to be locked and used by process
D). At time 3, A is blocking C, and hence runs with priority 3. The higherpriority process D preempts A at time 4, but is subsequently blocked when it
attempts to access Q. Hence A will continue (with priority 4) until it releases
its lock on Q and has its priority drop back to 1. Now, D will continue until
it completes (with a response time of 7). The benefit of the ceiling protocol
is that a high-priority process can only be blocked once (per activation) by
any lower-priority process. However, in the figure above we see processes B
and C suffering two blocks. What is actually happening is that a single block
is being broken in two by the preemption of process D.
E
IMMEDIATE CEILING PRIORITY PROTOCOL (ICPP)
1. Each process has a static default priority assigned.
2. Each resource has a static ceiling value defined, this is the
maximum priority of the processes that use it.
3. A process has a dynamic priority that is the maximum of its
own static priority and the ceiling values of any resources it has
locked.
Let’s again consider previous example, but now we shall apply to it ICPP
approach:
D
b
E
E
Q
V
E
C
b
b
b
P
P
P
P
P
E
V
V
E
B
b
b
b
P
P
P
P
P
P
P
P
P
E
E
Q
Q
Q
P
P
P
P
P
P
P
P
P
P
P
A
E
Q
Process A having locked Q at time 1, runs for the next 4 time quantum with
priority 4. Hence other processes are blocked. Once A unlocks Q (and has its
priority reduced), the other processes execute in priority order. For this
example we have blocking of processes before their actual execution, and
response time for D now is 6. ICPP is defined in ADA as
pragma Locking_Policy(Ceiling_Locking);
E
ADA LANGUAGE
Pragma Priority and Interrupt_Priority are used for tasks (processes) and
interruption handlers, for example:
task Controller is
pragma Priority(10);
end Controller;
If task-type definition contains such a pragma, then all tasks of that type will
have the same priority:
task type Servers(Task_Priority:System.Priority) is
entry Service1(..);
entry Service2(..);
pragma Priority(Task_Priority);
end Servers;
Pragma Task_Dispatching_Policy allows to select dispatching policy, for
example FIFO_Within_Priority means that tasks are queued in FIFO order.
Hence, as tasks become runnable, they are placed at the back of the notional
run queue for that priority level. One exception to this case is when task is
preempted; here the task is placed at the front of the notional run queue for
that priority level.
ADA LANGUAGE PROGRAMS
ADA programs have the following structure
declare
<declarative part>
begin
<sequence of statements>
exception
<exception handlers>
end;
declare
Temp:Integer:=A; --initial value is given to temporary variable;
begin
A:=B;
B:=Temp;
end; --Identifiers are not case-sensitive (a, A represent the same object).
type Dimension is (XPlane,YPlane,ZPLane);-- enumeration type
type Map is (XPlane, YPLane); -- enumeration type
Line, Force: Dimension;
Grid: Map;
begin
Line:=XPLane;
Force:=Dimension’Succ(XPlane);--assign YPlane to Force
Grid:=YPlane;--YPlane name is unambiguous as Grid is of type Map
Grid:=Line; --illegal – type clash
end;
TYPE AND SUBTYPES
May be introduced subtypes:
subtype Surface is Dimension range XPlane..YPlane;
Types may be derived
type New_Int is new Integer;
type Projection is new Dimension range XPlane..YPlane;
In ADA expressions objects of a type and its subtypes can be mixed, objects
of a type and a derived type cannot, such types are distinct:
D: Dimension;
S: Surface;
P: Projection;
begin
D:=S; --legal
S:=D; --legal but can cause run-time error if D has value of ZPlane
P:=D; -- illegal – type clash
P:=Projection(D); --legal, explicit type conversion
end;
Float types:
type New_Float is digits 10 range -1.0e18..1.0e18;
subtype Crude_Float is New_Float digits 2;
subtype Pos_New_Float is New_Float range 0.0..1000.0;
type Scaled_Int is delta 0.05 range -100.00..100.00;
ARRAYS AND RECORDS
Max: Const Integer:=10;
type Reading_T is array (0..Max-1) of Float;
Size: Const Integer:=Max-1;
type Switches_T is array(0..Size, 0..Size) of Boolean;
Reading: Reading_T;
Switches: Switches_T;
Records:
type Day_T is new Integer range 1..31;
type Month_T is new Integer range 1..12;
type Year_T is new Integer range 1900..2050;
type Date_T is record
Day: Day_T:=1;
Month: Month_T:=1;
Year: Year_T;
end;
D: Date_T;
begin
D.Year:=2003; -- dot notation
D:=(26,4,2003); --complete assignment
D:=(Year=>2003, Day=>26, Month=>4); --complete assignment using
name notation
end;
DYNAMIC DATA TYPES AND POINTERS
type Node; --incomplete declaration
type AcNode is access Node;
type Node is record
Value: Integer;
Next: AcNode;
end;
V: Integer;
Al: AcNode;
begin
Al:=new(Node); --construct 1st node
Al.Value:=V; --access variable is de-referenced and component defined
Al.Next:=null; -- predefined
..
end;
ADA’s ‘new’ operator allocates heap memory. There is no, however,
dispose operator. Rather, generic procedure Unchecked_Deallocation is used
for explicit de-allocation of the object. By default it is may be de-allocated
by implementation when it becomes inaccessible.
UNCHECKED_DEALLOCATION GENERIC PROCEDURE
generic
type OBJECT is limited private;
type NAME is access OBJECT;
procedure UNCHECKED_DEALLOCATION(X : in out NAME);
Unchecked storage deallocation of an object designated by a value of an
access type is achieved by a call of a procedure that is obtained by
instantiation
of
the
generic
procedure
UNCHECKED_DEALLOCATION. For
example:
procedure FREE is new UNCHECKED_DEALLOCATION
(object_type_name, access_type_name);
Such a FREE procedure has the following effect:
(a) after executing FREE(X), the value of X is null;
(b) FREE(X), when X is already equal to null, has no effect;
(c) FREE(X), when X is not equal to null, is an indication that the object
designated by X is no longer required, and that the storage it
occupies is to be reclaimed.
If X and Y designate the same object, then accessing this object through Y
is erroneous if this access is performed (or attempted) after the call
FREE(X); the effect of each such access is not defined by the language.
Download