You are on page 1of 21

CONCURRENT PROCESS

Introduction
In theory, many of the processes could run in parallel and
could communicate with one another while executing.
This leads to the concept of concurrency which means the
existence of several simultaneous process.

Situations for concurrency


1. multi-user operating system where different users execute
programs concurrently.
2. a single-user operating system where input/output activities go
simultaneously with program execution.
3. some processors whose activities are concurrent.
Ex. The fetch next instruction is done concurrently with the execution of
an instruction.
Forms of Concurrency
True concurrency happens in a multi processor
systems where several different processes are
executing at the same time.
Simulated concurrency can be found in single
processor systems where the system is made to give
impression of executing several programs at once.
If each processes is independent of each other,
concurrent processes will not be a problem.
However, most processes in the system are not
independent and usually they would need to
communicate with each other.

There are two types of communications:


1. competing processes
2. cooperating processes
Communicating Processes
Competing processes communicate concerning
the status of the resource for which they are
competing (mutual exclusion).
It ensures that only one process at a time uses the resource.
Cooperating processes transfer the information
they are processing from one another (process
synchronization).
Cooperating processes need to keep track of each others
progress.
Solutions to the Concurrency Problem
1. Busy waiting
2. Semaphores
3. Monitors
4. Message passing
5. Rendezvous
6. Remote procedure calls
Petersons Solution
Petersons Solution is a software-based solution that
allows two processes to share a single-use resource
without conflict, using only shared memory for
communication.
Because of the way modern computer architectures
perform basic machine-language instructions, such as
load and store, there are no guarantees that
Petersons solution will work correctly on such
architectures.
Petersons Solution
Petersons solution is restricted to two processes that
alternate execution between their critical sections and
remainder sections.
Petersons solution requires the two processes to
share two data items:
int turn;
boolean flag[2];
The variable turn indicates whose turn it is to enter its
critical section.
The flag array is used to indicate if a process is ready
to enter its critical section.
Synchronization Hardware
Synchronization hardware is concerned with the use
of hardware in critical section problems that work on
modern computer architectures.
This is based on the premise of locking that is,
protecting critical regions through the use of locks.
Hardware features can make any programming task
easier and improve system efficiency.
The critical-section problem could be solved simply in
a single-processor environment if we could prevent
interrupts from occurring while a shared variable was
being modified.
Synchronization Hardware
Unfortunately, this solution is not as feasible in a
multiprocessor environment.
Disabling interrupts on a multiprocessor can be time
consuming, since the message is passed to all the
processors.
Rather than discussing one specific instruction for
one specific machine, we abstract the main concepts
behind these types of instructions by describing the
test and set() and compare and swap() instructions.
Synchronization Hardware
Synchronization Hardware
Mutex Locks
Mutex lock is used to protect critical regions and thus
prevent race conditions.
That is, a process must acquire the lock before entering a
critical section; it releases the lock when it exits the
critical section.
The acquire()function acquires the lock, and the release()
function releases the lock,
Mutex Locks
The main disadvantage of the implementation given here
is that it requires busy waiting.
While a process is in its critical section, any other process
that tries to enter its critical section must loop
continuously in the call to acquire().
In fact, this type of mutex lock is also called a spinlock
because the process spins while waiting for the lock to
become available.
This continual looping is clearly a problem in a real
multiprogramming system, where a single CPU is shared
among many processes.
Mutex Locks
Semaphores
A semaphore S is an integer variable that, apart from
initialization, is accessed only through two standard
atomic operations: wait() and signal().
The wait() operation was originally termed P (from the
Dutch proberen, to test); signal() was originally called V
(from verhogen, to increment).
Semaphores
The value of a counting semaphore can range over an
unrestricted domain.
The value of a binary semaphore can range only between 0 and
1. Thus, binary semaphores behave similarly to mutex locks.
Counting semaphores can be used to control access to a given
resource consisting of a finite number of instances.
The implementation of a semaphore with a waiting queue may
result in a situation where two or more processes are waiting
indefinitely for an event that can be caused only by one of the
waiting processes (deadlock)
Another problem related to deadlocks is indefinite blocking or
starvation, a situation in which processes wait indefinitely
within the semaphore.
Dining Philosophers Problem
Five (n>1) philosophers are seated around a circular
table.
Each philosopher needs
two forks to eat spaghetti .
Between each plate is a fork
The life of a philosopher
consists of alternate periods of
eating and thinking.
Dining Philosophers Problem
When a philosopher gets hungry, he tries to acquire
his left and right fork, one at a time in either order.
If successful acquiring two forks, he eats for a while
and puts down forks and continues to think.
Devise a protocol that will allow philosophers to eat so
that nobody dies in hunger.
Solution with a deadlock
- after thinking, a philosopher waits for the fork at
the left to be free, once it is free seizes it. Wait for the
fork at the right to be free. Eat. Put the forks down
after eating.
Dining Philosophers Problem
Correct solution
- place all the plates and forks in a room. Philosophers
must get out of the room. Before he can eat, he must
get inside the room. Allow only n-1 philosophers
inside the room. After eating, the philosopher waiting
outside can now get in. Process is repeated.
END OF CHAPTER 7

You might also like