Anda di halaman 1dari 40

Course 6: Concurrent programs and

synchronization

Course 5: Review
Scheduler - Selects from among the processes in memory that are ready

to execute (PCB queues), and allocates the CPU to one of them


Dispatcher gives control of the CPU to the process selected by the short-

term scheduler; this involves:

switching context

switching to user mode

jumping to the proper location in the user program to restart that


program

Criteria:

CPU utilization

Throughput

Turnaround time

Waiting time

Response time

Operating System Concepts 8th Edition

6.2

Silberschatz, Galvin and Gagne

Course 5: Review
Scheduling Algorithms:

FCFS batch

SJF TS

Round Robin

Multilevel

Priority

Operating System Concepts 8th Edition

6.3

Silberschatz, Galvin and Gagne

Course 6: Process Synchronization


Concurrent execution of processes
Interleaving semantics
Concurrent programming issues
Mutual exclusion
Solutions for mutual exclusion

Operating System Concepts 8th Edition

6.4

Silberschatz, Galvin and Gagne

Concurrent execution of processes

Operating System Concepts 8th Edition

6.5

Silberschatz, Galvin and Gagne

Concurrency background
Computations can take advantage of multiple processing

units (through multi-core processors)


Terminology:

Multiprocessing: the use of more than one


processing unit in a system

Parallel execution: processes running at the same


time

Operating System Concepts 8th Edition

6.6

Silberschatz, Galvin and Gagne

Concurrency background
Even on systems with a single processing unit we may give the

illusion of that several programs run at once


OS switches between executing different tasks

Terminology:

Interleaving: several tasks active, only one running at a time

Multitasking: OS runs interleaved executions

Concurrency: multiprocessing, multitasking, or any combination

Operating System Concepts 8th Edition

6.7

Silberschatz, Galvin and Gagne

Concurrent programs
A program which at runtime gives rise to a process

containing multiple threads is called a concurrent program.

Operating System Concepts 8th Edition

6.8

Silberschatz, Galvin and Gagne

Concurrent programs

Operating System Concepts 8th Edition

6.9

Silberschatz, Galvin and Gagne

Concurrent programs

Operating System Concepts 8th Edition

6.10

Silberschatz, Galvin and Gagne

Race conditions
If processes/threads are completely independent, concurrency is easy
Much more often threads interfere with each other, for example by

accessing and modifying the same variables or objects


The situation that the result of a concurrent execution is dependent on

the nondeterministic interleaving is called a race condition or data race


Such errors can stay hidden for a long time and are difficult to find by

testing

Operating System Concepts 8th Edition

6.11

Silberschatz, Galvin and Gagne

Atomic instructions
An instruction is atomic if its execution cannot be interleaved with other

instructions before its completion


We can choose different levels of atomicity. For example, the instruction

x := x + 1
is on many systems executed as:
temp := x -- LOAD R0, x
temp := temp + 1 -- ADD R0, 1

x := temp -- STORE R0, x


Convention: in our notation for concurrent programs, every numbered

line can be executed atomically

Operating System Concepts 8th Edition

6.12

Silberschatz, Galvin and Gagne

Atomic instructions
To reflect the different assumption on atomicity, the concurrent

program is restated:

One of the possible execution sequences:

Operating System Concepts 8th Edition

6.13

Silberschatz, Galvin and Gagne

Interleaving semantics

Operating System Concepts 8th Edition

6.14

Silberschatz, Galvin and Gagne

True-concurrency vs. interleaving


semantics
To describe concurrent behaviour, we need a model

True-concurrency semantics: assumption that true parallel


behaviours exist

Interleaving semantics: assumption that all parallel behaviour


can be represented by the set of all nondeterministic interleavings of atomic instructions

Operating System Concepts 8th Edition

6.15

Silberschatz, Galvin and Gagne

Interleaving semantics
The interleaving semantics provides a good model for concurrent

programs, in particular it can describe:

Multitasking: The interleaving is performed by the scheduler

Multiprocessing: The interleaving is performed by the hardware

By considering all possible interleavings, we can ensure that a

program runs correctly in all possible scenarios


Downside: The number of possible interleavings grows

exponentially in the number of concurrent processes (state space


explosion problem)

Operating System Concepts 8th Edition

6.16

Silberschatz, Galvin and Gagne

Transition systems
A formal model that allows us to express concurrent computation

are transition systems


They consist of states and transitions between them

A state is labeled with atomic propositions, which express concepts

such as:
P22 (the program pointer of P2 points to 2)
x = 6 (the value of variable x is 6)
There is a transition between two states if one state can be

reached from the other by executing an atomic instruction

Operating System Concepts 8th Edition

6.17

Silberschatz, Galvin and Gagne

Example: Transition systems

Operating System Concepts 8th Edition

6.18

Silberschatz, Galvin and Gagne

Concurrent programming issues

Operating System Concepts 8th Edition

6.19

Silberschatz, Galvin and Gagne

Synchronization
In order to solve the problem of data races, processes have to

synchronize with each other


Synchronization describes the idea that processes communicate with

each other in order to agree on a sequence of actions


In the above example, it could be agreed that only one process at a

time can hold the resource (have exclusive use of it)

How can processes communicate?

Shared memory: processes communicate by reading and writing


to shared sections of memory

Message-passing: processes communicate by sending messages


to each other

Operating System Concepts 8th Edition

6.20

Silberschatz, Galvin and Gagne

The problem of deadlock


The ability to hold resources exclusively is central to providing

process synchronization for resource access


Unfortunately, it brings about other problems
A deadlock is the situation where a group of processes blocks

forever because each of the processes is waiting for resources which


are held by another process in the group
An illustrative example of deadlock is the dining philosopher's

problem

Operating System Concepts 8th Edition

6.21

Silberschatz, Galvin and Gagne

The dining philosophers problem


n philosophers are seated around a table

and in between each pair of philosophers


there is a single fork.
A philosopher only engages in two

activities: thinking and eating.


In order to eat, each philosopher needs to

pick up both forks which are lying to his


sides, and thus philosophers sitting next
to each other can never eat at the same
time.
The problem consist of devising an

algorithm such that the following


properties are ensured:

Each fork is held by one philosopher


at a time

Philosophers don't deadlock

Operating System Concepts 8th Edition

6.22

Silberschatz, Galvin and Gagne

Dining philosophers: solution attempt 1


Each philosopher first picks up the right fork, then the left fork, and then

starts eating; after having eaten, the philosopher puts down the left fork,
then the right one
The philosophers can deadlock

Operating System Concepts 8th Edition

6.23

Silberschatz, Galvin and Gagne

The Coffman conditions


There are a number of necessary conditions for deadlock to occur

1. Mutual exclusion: processes have exclusive control of the


resources they require

2. Hold and wait: processes already holding resources may


request new resources

3. No preemption: resources cannot be forcibly removed from


a process holding it

4. Circular wait: two or more processes form a circular chain


where each process waits for a resource that the next process
in the chain holds

Attempts at avoiding deadlocks typically try to break one of these

conditions

Operating System Concepts 8th Edition

6.24

Silberschatz, Galvin and Gagne

Dining philosophers: solution attempt 2


Each philosopher picks up right fork and the left fork at the same time,

and then starts eating; after having eaten, the philosopher puts them
both back down
A philosopher could starve

Operating System Concepts 8th Edition

6.25

Silberschatz, Galvin and Gagne

Starvation
Even if no deadlock occurs, it may still happen that some processes

are not treated fairly


The situation that processes are perpetually denied access to

resources is called starvation

Operating System Concepts 8th Edition

6.26

Silberschatz, Galvin and Gagne

Mutual exclusion
Race conditions can corrupt the result of a concurrent computation if

processes are not properly synchronized


We want to develop techniques for ensuring mutual exclusion
Mutual exclusion: a form of synchronization to avoid the

simultaneous use of a shared resource


To identify the program parts that need attention, we introduce the

notion of a critical section


Critical section: part of a program that accesses a shared resource.

Operating System Concepts 8th Edition

6.27

Silberschatz, Galvin and Gagne

Mutual exclusion
We assume to have n processes of the following form:

Design the entry and exit protocols to ensure:

Mutual exclusion: At any time, at most one process may be in


its critical section

Freedom from deadlock: If two or more processes are trying


to enter their critical sections, one of them will eventually
succeed

Freedom from starvation: If a process is trying to enter its


critical section, it will eventually succeed

Operating System Concepts 8th Edition

6.28

Silberschatz, Galvin and Gagne

Mutual exclusion
Further important conditions:

Processes can communicate with each other only via


atomic read and write operations

If a process enters its critical section, it will eventually


exit from it

A process may loop forever or terminate while being in its

non-critical section
The memory locations accessed by the protocols may not

be accessed outside of them

Operating System Concepts 8th Edition

6.29

Silberschatz, Galvin and Gagne

Locks
Synchronization mechanisms based on the ideas of entry and exit-

protocols are called locks


They can typically be implemented as a pair of functions:

Operating System Concepts 8th Edition

6.30

Silberschatz, Galvin and Gagne

Busy waiting
We will use the following statement in pseudo code

await b

--which is equivalent to

while not b loop end


This type of waiting is called busy waiting or "spinning"
Busy waiting is inefficient on multitasking systems
Busy waiting makes sense if waiting times are typically so

short that a context switch would be more expensive


Therefore spin locks (locks using busy waiting) are often

used in operating system kernels

Operating System Concepts 8th Edition

6.31

Silberschatz, Galvin and Gagne

Solution attempt 1
First idea: use two variables enter1 and enter2; if enter is

true, it means that process Pi intends to enter the critical


section

Operating System Concepts 8th Edition

6.32

Silberschatz, Galvin and Gagne

Solution attempt 1
The solution attempt fails to ensure mutual exclusion
The two processes can end up in their critical sections at

the same time, as demonstrated by the following


execution sequence

Operating System Concepts 8th Edition

6.33

Silberschatz, Galvin and Gagne

Solution attempt 2
When analyzing the failure, we see that we set the

variable enteri only after the await statement, which is


guarding the critical section
Second idea: switch these statements around

Operating System Concepts 8th Edition

6.34

Silberschatz, Galvin and Gagne

Solution attempt 2
When analyzing the failure, we see that we set the

variable enteri only after the await statement, which is


guarding the critical section
Second idea: switch these statements around

Operating System Concepts 8th Edition

6.35

Silberschatz, Galvin and Gagne

Solution attempt 2
The solution provides mutual exclusion
However, the processes can deadlock:

Operating System Concepts 8th Edition

6.36

Silberschatz, Galvin and Gagne

Solution attempt 3
Third idea: let's try something new, namely a single

variable turn that has value i if it's Pi's turn to enter the
critical section

Operating System Concepts 8th Edition

6.37

Silberschatz, Galvin and Gagne

Solution attempt 3
Solution attempt III looks good to us, let's try to prove it

correct
Draw the related transition system; states are labelled

with triples (i, j, k): program pointer values P1i and P2j,
and value of the variable turn = k.

Operating System Concepts 8th Edition

6.38

Silberschatz, Galvin and Gagne

Solution attempt 3
Solution attempt III satisfies mutual exclusion - there are

no states of the form (2, 2, k)


Solution attempt III is deadlock-free - We have to

examine the states (1, 1, 1) and (1, 1, 2); in both cases,


one of the processes is enabled to enter its critical
section.
Starvation: A problematic case is (1, 4, 2): variable turn =

2, P1 trying to enter critical section (although not its turn),


P2 in noncritical section. If P2 terminates, turn will never
be set to 1: P1 will starve

Operating System Concepts 8th Edition

6.39

Silberschatz, Galvin and Gagne

End of Course 6

Anda mungkin juga menyukai