operating systems - synchronization

25
UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Synchronization

Upload: emery-berger

Post on 17-Dec-2014

1.503 views

Category:

Technology


1 download

DESCRIPTION

From the Operating Systems course (CMPSCI 377) at UMass Amherst, Fall 2007.

TRANSCRIPT

Page 1: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science

Emery Berger

University of Massachusetts Amherst

Operating SystemsCMPSCI 377

Synchronization

Page 2: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 2

Synchronization

Threads must ensure consistency

Else: race condition(non-deterministic result)

Requires synchronization operations

How to write concurrent code

How to implement synch. operations

Page 3: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 3

Synchronization – Motivation

“The too much milk problem”

Model of need to synchronize activities

Page 4: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 4

Synchronization Terminology

Mutual exclusion (“mutex”)– prevents multiple threads from entering

Critical section– code only one thread can execute at a time

Lock– mechanism for mutual exclusion

Lock on entering critical section, accessing shared data

Unlock when complete

Wait if locked

Page 5: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 5

Solving the Too Much Milk Problem

Correctness properties

Only one person buys milk

Safety: “nothing bad happens”

Someone buys milk if you need to

Progress: “something good eventually happens”

First: use atomic loads & stores as building blocks

“Leave a note” (lock)

“Remove a note” (unlock)

“Don’t buy milk if there’s a note” (wait)

Page 6: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6

Too Much Milk: Solution 1

thread A

if (no milk && no note)

leave note

buy milk

remove note

thread B

if (no milk && no note)

leave note

buy milk

remove note

Does this work?too much milk

Page 7: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 7

Too Much Milk: Solution 2

thread A

leave note A

if (no note B)

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Idea: use labeled notes

oops – no milk

Page 8: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 8

Too Much Milk: Solution 3

thread A

leave note A

while (note B)

do nothing

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Idea: wait for the right note

Exercise: verify this

Page 9: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 9

Too Much Milk: Solution 3

thread A

leave note A

while (note B)

do nothing

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Possibility 1: A first, then B

OK

Page 10: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 10

Too Much Milk: Solution 3

thread A

leave note A

while (note B)

do nothing

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Possibility 2: B first, then A

OK

Page 11: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 11

Too Much Milk: Solution 3

thread A

leave note A

while (note B)

do nothing

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Possibility 3: Interleaved – A waits & buys

OK

Page 12: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 12

Too Much Milk: Solution 3

thread A

leave note A

while (note B)

do nothing

if (no milk)

buy milk

remove note A

thread B

leave note B

if (no note A)

if (no milk)

buy milk

remove note B

Possibility 4: Interleaved – A waits, B buys

OK

Page 13: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 13

Too Much Milk: Solution 3

Solution 3:“Thread A waits for B, otherwise buys”

Correct – preserves desired properties

Safety: we only buy one milk

Progress: we always buy milk

But…

Page 14: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 14

Problems with this Solution

Complicated

Difficult to convince ourselves that it works

Asymmetrical

Threads A & B are different

Adding more threads = different code for each thread

Poor utilization

Busy waiting – consumes CPU, no useful work

Possibly non-portable

Relies on atomicity of loads & stores

Page 15: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 15

Language Support

Synchronization complicated

Better way – provide language-levelsupport Higher-level approach

Hide gory details in runtime system

Increasingly high-level approaches: Locks, Atomic Operations

Semaphores – generalized locks

Monitors – tie shared data to synchronization

Page 16: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 16

Locks

Provide mutual exclusion to shared data via two atomic routines acquire – wait for lock, then take it

release – unlock, wake up waiters

Rules: Acquire lock before accessing shared data

Release lock afterwards

Lock initially released

Page 17: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 17

Pthreads Syntax

POSIX standard for C/C++

Mutual exclusion locks

Ensures only one thread in critical section

pthread_mutex_init (&l);

pthread_mutex_lock (&l);

update data; /* critical section */

pthread_mutex_unlock (&l);

Page 18: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 18

Pthreads API

Page 19: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 19

Too Much Milk: Locks

Clean, symmetric - but how do we implement it?

thread A

p_m_lock(&l)

if (no milk)

buy milk

p_m_unlock(&l)

thread B

p_m_lock(&l)

if (no milk)

buy milk

p_m_unlock(&l)

Page 20: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 20

Implementing Locks

Requires hardware support (in general)

Can build on atomic operations:

Load/Store

Disable interrupts

Uniprocessors only

Test & Set, Compare & Swap

Page 21: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 21

Disabling Interrupts

Prevent scheduler from switching threads in middle of critical sections

Ignores quantum expiration (timer interrupt)

No handling I/O operations

(Don’t make I/O calls in critical section!)

Why not implement as system call?

Page 22: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 22

Disabling Interrupts

Class Lock {

private int value;

private Queue q;

Lock () {

value = 0; q = empty;

}

public void acquire () {

disable interrupts;

if (value == BUSY) {

add this thread to q;

enable interrupts;

sleep();

} else {

value = BUSY;

}

enable interrupts;

}

public void release () {

disable interrupts;

if (q not empty) {

thread t = q.pop();

put t on ready queue;

}

value = FREE;

enable interrupts;

}

Page 23: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 23

Locks via Disabling Interrupts

Page 24: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 24

Summary

Communication between threads:via shared variables

Critical sections = regions of code that modify or access shared variables

Must be protected by synchronization primitives that ensure mutual exclusion

Loads & stores: tricky, error-prone

Solution: high-level primitives (e.g., locks)

Page 25: Operating Systems - Synchronization

UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science

The End

25