8-2
8-2
8-2
Lecture eight
part2
Dr jamal altuwaijari
Synchronization hardware 8.7
The feature of the h/w can make the programming task easier and
improve system efficiency. The critical- section problem could be
solved simply in as uniprocessor environment if we could disallow
interrupts to occur while a shared variable is being modified. Many
machines therefore provide special h/w instructions that allow us
either to lest and modify the content of a word or to swap the contents
.of two words
We can use these special instructions to solve the critical section
.peoblem
8.7.1. the test- and set instruction
Can be defined as shorn in the tigure8.5 below, where we can
implement mutual exclusion by declaring a Boolean -variable lock
;initialized to false. Function test-and-set (Var target Boolean): Boolean
Semaphores 8.7.2
Another solution to the critical • section problem is the using a synchronization tool
called a
.Semaphore
A semaphore S is integer variable that a part from initialization is accessed only
through two standard atomic operations: wait and signal. These operations were
originally termed p (for wait from proberen to test) and V (for signal from verhogen
.to increment)
:The classical definitions of wait and signal are
;Wait (S): w Nile S <= 0 do no-op
:S:=S-1
;Signal(S): S:= S +1
Modification to the integer value Of the semaphore in the wait and signal operation
must he executed indivisibly. That is when one process modifies the semaphore
value no other process, - can at the some time modify that same semaphore value.
In addition in the case of wait(S) the testing instructions(S<=0) and(S:=S-I ) must also
The semaphore usage
We can use semaphores to deal with the n-process critical-section
problem. The n-processes share a semaphore mutes (for mutual
.exclusion) initialized to I
.Each process pi is organized as the figurc8.7
.Repeat
Example (1): Consider two concurrently running processes: pi with a
statement Si and P1 with a statement S•. Suppose that we xan
implement this scheme by letting Pi and Pi share a common semaphore
.synch initialized to 0 and by inserting the statements in process PI
:Example(2)
The main disadvantage of mutual-excl. solutions defined previously is that they all require
busy waiting. The busy wailing wastes CPU cycles that some other process might be able
to use the CPU. To overcome the need for busy waiting we can modify the definition the
wait and signal semaphore operations. To implement semaphores under this definition
we define a semaphore as a record: Each semaphore has an integer value and a list of
processes .when a process must wait on a semaphore it is added to the list processes. A
signal operation removes one process from the list of waiting processes' and awakens
.that process . the semaphore -operations-can now be defined as in the figure 8.8
There are two types of semaphores:
a. binary semaphores: can assume only the value 0 or the value I.
b.Counting semaphores(also called general semaphores ) can assume
only non negative integer values.
This type of semaphores are useful when a resource is to be allocated
from a pool of identical resources. The semaphore is initialized to the
number of resources in the pool. 'Where process P decrements the
semaphore by 1, and process V increments by I
8.8.Concurrent programming:
Concurrent programming is much more difficult than sequential
programming. Concurrent programs are harder to write debug, modify
and prove correct. Concurrent programming is very important today
because it enable us to express solutions more naturally to certain
kinds of problems that are related to paralley hardware architectures
.such as multi processors and distributed systems
Monitors
A monitor is a concurrency that contains both data and procedures
needed to perform allocation of particular serially reusable shared
.resource or group of serially reusable shared resources
A monitor is defined by set of programmer- defined operators, and the
:representation of a monitor types consists of
a. declaration of a variables whose VAUCS define the state of an
.instance of the type
b. The bodies of procedures of functions that implement operations on
the type
Monitors
.c. The syntax of monitor is
Type monitor-nme=monitor
Variable declarations
;)...(Procedure entry P1
;)...(Begin...end; Procedure entry P2
;Begin...end
;)...(.Procedure entry P2
;Begin... end
Begin
Initialization code
End
Monitors
A procedure defined within monitor can access only those variables declared locally within
the monitor and the format parameters. Where there is no way for processes outside the
.monitor to access monitor data
The monitor construct ensures that only one process at a time can be active within the
monitor' the monitor resources additional construct used for synchronization called
.condition construct
Example
;Var
x,y: condition
The only operations that can be involved on a condition variable are wait and signal
;x.wait
.means that the process invoking this operation is suspended until another process invokes
;x.signal
The x.signal' operation resumes exactly one suspended process. If on such process then the
signal operation has no effect that is the stare of x is as though the operation was never
executed. The schematic view of a monitor with condition variables as in the figure 8.9
Monitors
The wait and signal operations arc modified to include the name of the condition variable being
.waited or signaled
Figure 8.9: Monitor with conditions variables
Wait (condition variable name)
Signal(condition variable name)
When a condition variable is defined a queue is established a process calling wait is threaded into
the queue, a process calling a signal causes a waiting process to remove from the queue and to
Simple resource allocation with monitors
Suppose several processes need access to a certain resource that may
.be used be only one process at a time
A simple monitor for handling the assignment and disassignment of
.such a resource as described below, in figure 8.10