CSCI325 Ch. 14 Dr Ahmed Rafea 1
Chapter 14 Parallel Programming
- Introduction
- Synchronization
- Semaphores
- Monitors
Chapter 14 Parallel Programming Introduction Synchronization - - PDF document
Chapter 14 Parallel Programming Introduction Synchronization Semaphores Monitors CSCI325 Ch. 14 Dr Ahmed Rafea 1 Introduction Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language
CSCI325 Ch. 14 Dr Ahmed Rafea 1
CSCI325 Ch. 14 Dr Ahmed Rafea 2
Concurrency can occur at four levels:
Because there are no language issues in instruction- and program-level concurrency, they are not addressed here Categories of Concurrency:
processors ( multiple threads of control)
physical concurrency is presented by time- sharing one processor (software can be designed as if there were multiple threads of control)
CSCI325 Ch. 14 Dr Ahmed Rafea 3
can be very useful--many real-world situations involve concurrency
now widely used
Def: A task is a program unit that can be in concurrent execution with other program units
that:
control may not return to the caller Def: A task is disjoint if it does not communicate with or affect the execution of any other task in the program in any way
CSCI325 Ch. 14 Dr Ahmed Rafea 4
complete some specific activity before task A can continue its execution e.g., the producer-consumer problem
some resource that cannot be simultaneously used e.g., a shared counter
not atomic
mutually exclusive access (methods are discussed later)
CSCI325 Ch. 14 Dr Ahmed Rafea 5
for delaying task execution
program called the scheduler, which maps task execution onto available processors
states:
currently running (no available processor)
continue (usually waiting for some event to
CSCI325 Ch. 14 Dr Ahmed Rafea 6
may or may not have
eventually complete its execution
lose its liveness
liveness, it is called deadlock
CSCI325 Ch. 14 Dr Ahmed Rafea 7
CSCI325 Ch. 14 Dr Ahmed Rafea 8
ways to access the buffer
emptyspots and fullspots
the numbers of empty spots and full spots in the buffer
there is room in the buffer
decremented and the value is inserted
queue of emptyspots
the counter of fullspots
CSCI325 Ch. 14 Dr Ahmed Rafea 9
there is a value
is decremented and the value is removed
must be placed in the queue of fullspots
counter of emptyspots
semaphores are accomplished through two semaphore operations named wait and release 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 some ready task (If the task ready queue is empty, deadlock occurs)
end
CSCI325 Ch. 14 Dr Ahmed Rafea 10
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
control access (competition synchronization)
values 0 and 1
semaphore
Evaluation of Semaphores:
cooperation synchronization e.g., the buffer will overflow if the wait of fullspots is left out
competition synchronization e.g., The program will deadlock if the release of access is left out
CSCI325 Ch. 14 Dr Ahmed Rafea 11
CSCI325 Ch. 14 Dr Ahmed Rafea 12
CSCI325 Ch. 14 Dr Ahmed Rafea 13
puts the process that calls it in the specified queue and removes its exclusive access rights to the monitor’s data structure
always blocks the caller
it disconnects the caller from the monitor, thus freeing the monitor for use by another
parameter queue (if the queue isn’t empty) and starts it
has some effect (release does nothing if the queue is empty)
Evaluation of monitors:
great!
very similar as with semaphores, so it has the same problems