View
227
Download
1
Embed Size (px)
Citation preview
Index
Objectives• Concurrent processes and
Asynchronous concurrent processes
• Process synchronisation
• Mutual exclusion
Index
Terminology• Critical Section– when it is accessing shared data in a
system.
• Semaphore– Used to implement the idea of mutual
exclusion due to multiple access
• Concurrent process– Processes are said to be concurrent if they
exist at the same time.
Index
Software implementationIdea of process synchronization
While gate = closed : DO null operation
(gate is open)
gate := closed : so that other cannot use it
………..
………..
Critical section
……….
gate := open : let other to use it
Above is impractical for multi-processes
Index
Process Type• Concurrent processes– processes exist at the same time– if on a single CPU system, then context
switching are required
• Asynchronous concurrent processes– concurrent processes function
independently– require occasional synchronization or
interprocess communication
Index
Mutual Exclusion• To ensure the integrity of data,
shared data cannot be used by more than ONE process at the same time
• Otherwsie data will be updated incorrectly
Index
Critical Sections• When a process is accessing shared
modifiable data, it is in “Critical Section”
Index
Features of Critical Sections• when a process is in the critical section,
other processes may continue execution outside the critical region.
• when a process leaves its critical section, one other process waiting to enter the critical section may proceed.
• Mutual exclusion can be implemented by means of SEMAPHORES.
Index
Synchronization• A process may not be able to
continue until a service is provided.
• It involves the signaling between two or more processes.
• Process synchronization can be achieved by semaphores.
Index
Semaphore• Semaphore was introduced by
Dijksta in 1965.
• A semaphore is a protected variable S.
• The value can be accessed and altered by three operations, namely, wait (P(S)), signal (V(S)) and initialization operations only.
Index
Characteristics of semaphores• Binary semaphores can assume only
the value 0 or 1
• Counting semaphores can assume only non-negative integer values
• P(S) and V(S) are the initial Dutch words. P(S) refers to wait; while V(S) is signal.
Index
Code for binary semaphoreP operation on variable S, P(S) is
• if S > 0
• then S := S - 1 (means no process is using)
• Else (wait on S to change value)
V operation (V means to signify), V(S) is
• If (one or more processes is/are waiting on S to change.)
• then (let one of theses processes proceed )
• else S:= S + 1 (no process in the semaphore queue)
Index
Software ExampleProgram example-one var active: semaphore;
Procedure process-A
begin
while true do
begin
P(active);
critical-sec;
:
V(active);
:
end
end;
Procedure process-B
begin
while true do
begin
:
P(active);
critical-sec;
:
V(active);
:
end
end;
Index
Software Example (Continue…)Begin
semaphore-init(active,1); /* initialise the semaphore variable active */
parbeginprocess-A;
process-B;
parend
end.
Index
Mutual Exclusion• When a resource can be accessed by at
least two processes, it can only be exclusively used by one of them until it is completed.
• In a system providing multiple processes, it is desirable to co-ordinate the activities of them
• The relationship can be described by a set of processes called producer-consumer.
IndexExample for Mutual ExclusionProcedure producer
While true do
{
....
produce an item
....
P(empty);
P(mutex);
....
add item to buffer;
....
V(mutex);
V(full);
}
Procedure consumer
While true do
{
P(full);
P(mutex);
....
remove an item from buffer to “local”
....
V(mutex);
V(empty);
....
consume the item in “local”
}