You are on page 1of 19

FUNDAMENTAL ISSUES IN

CONCURRENT PROGRAMMING.

Why concurrency and at what price


YES, CONCURRENCY IS DIFFICULT!!!
Fundamental issues in concurrent
programming.
What makes concurrent programming difficult? What makes it different?

 Nondeterminism: two executions of the same program with


the same data may yield different results.

 (Need for) Synchronization: some tasks must be


performed in a precise order

 (Need for) Communication: sometimes data has to be


exchanged (and/or shared)

(Synchronization can be considered a special case of communication)


Nondeterminism
Nondeterminism is an inherent property of
concurrent programs. Two executions of the
same program with the same input data
may yield different, sometimes even
unpredictable, results.

Some reasons for nondeterminism are:


• progress rates of processes are unpredictable (remember that no assumptions
can be made regarding the behaviour of the scheduler...)
• communication latency is unpredictable
• the explicit use of nondeterministic programming constructs.
Nondeterminism and the number of
interleavings
The number of possible interleavings is huge,

very huge...
If there are n processes and each one has m atomic
instructions, the total number of possible interleavings is
( n  m)!
n
( m! )
Nondeterminism and the number of
interleavings
If there are n processes and each one has m atomic
instructions, the total number of possible interleavings is
( n  m)!
( m! ) n
Processes

2 3 4 5
2 6 90 2520 113400
instructions

3 20 1680 369600 1.6·108


4 70 34650 6.3·107 3·1011
5 252 756756 1.1·1010 6.2·1014
Synchronization

Synchronization In some situations it is


necessary to enforce a certain order to the
tasks performed by different processes. For
instance a certain task cannot be started until
another one has already been completed.

In general, the need for synchronization means


that a certain part of the code (task) cannot be
executed until a given condition is true.

Often, two different types of synchronization are considered:


• Mutual exclusion: certain parts of the code cannot be under
execution at the same time. They are incompatible. They cannot
interleave, they cannot overlap.
• Serialization: Task B cannot start until task A has completed. They
cannot interleave, they cannot overlap, precedence is important
Communication

Communication Often processes running in


parallel have to exchange or share data
• A process consumes (uses) data produced
by another process
• A process needs to know something
regarding some other process’s state

Two different means of communication are considered:


• Shared variables: a single variable can be read/written by more
than one process.
• Message passing: processes exchange messages in order to
communicate (especially in distributed systems, but not only)
Some simple examples will
help us understand and
fix the relevant ideas...
The simplest nondeterministic program
In a previous slide the following
nondeterministic program was given:

Can there be anything simpler than this?

When the processes PING


An even simpler non-deterministic and PONG run concurrently
program is this: the order of execution
depends on the scheduler.
For any given run, the result
can be “PING PONG” or
“PONG PING” but which
can’t be predicted in
advance
Alice, Bernard and the gallantry problem
(a serialization problem)

Alice and Bernard have been married for a time. Bernard is not only a peculiar guy but
also has a deep sense of chivalry. Bernard NEVER starts eating his dinner before Alice has
started eating hers (this peculiar rule does not apply to breakfast or lunch, only to dinner).
One day, Bernard has to go to work to another city...

This is the solution they have come up with...


With this solution, Alice and
Bernard are behaving as if in a
distributed system. They use
message-passing to solve their
synchronization (serialization,
more precisely) problem. Notice
that this solution does not impose
any particular ordering to
breakfast, lunch or ending dinner.
Alice, Bernard and the gallantry problem
(a serialization problem)

This solution is
correct but…
COULD
ANYTHING GO
WRONG?

YES...
Alice could STARVE (kill of hunger) Bernard!!!

This problem is known as STARVATION (a process endlessly waits for


something that is not going to happen)
Charles, Dora and the power plant problem
(a mutual exclusion problem)

Charles and Dora work in a nuclear power plant. They spend each working day in
two different rooms monitoring the lights and dials that show the state of the plant.
If anything goes wrong any of them can set off an alarm. Charles and Dora are a
“redundant system”: they do exactly the same in order to guarantee a higher
degree of reliability.
Charles and Dora can leave their room to have breakfast provided they do not
leave the plant unattended. Thus, at any given time only one can be having
breakfast.
In each room there is a red-or-green light. When it’s red it means it is not safe to
leave the room because the other is out. If it’s green then it is safe to leave. Also, in
each room there’s a switch that sets the light in the other room.
This is a mutual exclusion problem that takes place in
a shared-memory environment.
Breakfast is a “CRITICAL SECTION” only one of the
actors (processes) can be in it at any given time
Charles, Dora and the power plant problem
(a mutual exclusion problem)

This is the solution they have come up with...

... But, is it correct?


Charles, Dora and the power plant problem
(a mutual exclusion problem)

This solution is NOT


correct because
Charles and Dora
may go through
the WAIT “barrier”
at the same time

This situation is known as a RACE CONDITION

Race conditions may lead to very dangerous bugs. How


dangerous? Ask google for “Therac 25” (or click in the link)
Fundamental issues

e.g. PING PONG ; concurrent update

e.g. The gallantry problem (serialization)


e.g. The power plant problem (critical section)
Why concurrency?
Concurrency is difficult... So, why concurrency after all? Here
are some (good) reasons:
 Better utilization of resources, often leading to increased
efficiency => speed-up problem-solving. Split problem into
smaller parts and let each part be solved by a processor
and/or let one part be solved while others are idle (waiting
for something)
 Some problems are inherently concurrent (then concurrent
programming may provide a natural model for them) =>
Server systems serving several clients at a time (e.g.
Database systems executing transactions for different clients);
Real-time systems responding to external environment events
 Networked environments abound. Concurrency allows
computation to be distributed in networked environments (Again,
better utilization of resources) (e.g. SETI, blockchain, …)

You might also like