critical section problem

26
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo

Upload: jewel

Post on 08-Feb-2016

45 views

Category:

Documents


0 download

DESCRIPTION

Critical Section Problem. CIS 450 Winter 2003 Professor Jinhua Guo. Thread Control Block. OS’s representation of thread Registers, PC, SP, scheduling info, etc Several different lists of TCBs in OS Ready list Blocked lists Waiting for disks Waiting for input Waiting for events. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Critical Section Problem

1

Critical Section Problem

CIS 450 Winter 2003

Professor Jinhua Guo

Page 2: Critical Section Problem

2

Thread Control Block

• OS’s representation of thread– Registers, PC, SP, scheduling info, etc

• Several different lists of TCBs in OS– Ready list– Blocked lists

• Waiting for disks• Waiting for input• Waiting for events

Page 3: Critical Section Problem

3

Context Switching

• Switching between 2 threads– Change PC to current instruction of new

thread• might need to run old thread in the future

– Must save exact state of first thread– State saved into the TCB

• What must be saved?– Registers (including PC and SP)– What about stack itself?

Page 4: Critical Section Problem

4

Implementing Threads in User Space

A user-level threads package

Page 5: Critical Section Problem

5

Implementing Threads in the Kernel

A threads package managed by the kernel

Page 6: Critical Section Problem

6

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

Page 7: Critical Section Problem

7

Independent vs. Cooperating Threads

• Independent threads– No state shared – separate address space

• Cooperating threads– Same address space– Can affect one another– Must be careful!

Page 8: Critical Section Problem

8

Example of Concurrent Programint balance = 100;void deposit (int amount) {

balance += amount; exit(0);

}

int main () {threadCreate(deposit, 10);threadCreate(deposit, 20);waitForAllDone(); /*make sure all children finish*/printf (“The balance is %d”, balance);

}What are the possible output?

Page 9: Critical Section Problem

9

More Concurrent Programming: Linked lists (head is shared)

insert (head, elem) {

Elem->next := head;

head := elem;

}

void *removed (head) {

void *tmp;

tmp := head;

head := head->next;

return t;

}

Assume one thread calls insert and one calls remove.

Page 10: Critical Section Problem

10

Race Condition

• This kind of bug, which only occurs under certain timing conditions, is called a race condition.

• Output depends on ordering of thread execution

• More concretely:(1) two or more threads access a shared variable with

no synchronization, and

(2) at least one of the threads writes to the variable

Page 11: Critical Section Problem

11

Critical Section

• Section of code that:– Must be executed by one thread at a time– If more than one thread executes at a time,

have a race condition– Ex: linked list from before

• Insert/Delete code forms a critical section• What about just the Insert or Delete code?

Page 12: Critical Section Problem

12

Critical Section (CS) Problem

• Provide entry and exit routines– All threads must call entry before executing

CS.– All threads must call exit after executing CS– Thread must not leave entry rounte until it’s

safe.

Page 13: Critical Section Problem

13

Structure of threads for Critical Section Problem

• Threads do the following

While (1) {

call entry();

critical section

call exit();

do other stuff;

}

Page 14: Critical Section Problem

14

Properties of Critical Section Solution

• Mutual Exclusion: at most one thread is executing CS.

• Absence of Deadlock: two or more threads trying to get into CS => at least one succeeds.

• Absence of Unnecessary Delay: if only one thread trying to get into CS, it succeeds

• Bounded Waiting: thread eventually gets into CS.

Page 15: Critical Section Problem

15

Critical Section Solution Attempt #1(2 thread solution)

Intially, turn == 0

entry (id) {while (turn !=id); /* if not my turn, spin */

}

exit(id) {turn = 1 - id; /* other thread’s turn*/

}

Page 16: Critical Section Problem

16

Critical Section Solution Attempt #2(2 thread solution)

Intially, flag[0] == flags[1] == false

entry (id) {flag [id] = true; /* I want to go in */while (flag[1-id]); /* proceed if other not trying */

}

exit(id) {flag[id] = false; /* I am out*/

}

Page 17: Critical Section Problem

17

Critical Section Solution Attempt #3(2 thread solution)

Intially, flag[0] == flags[1] == false, turn

entry (id) {flag [id] = true; /* I want to go in */turn = 1 – id;while (flag[1-id] && turn == 1-id); /* proceed if

other not trying */}exit(id) {

flag[id] = false; /* I am out*/}

Page 18: Critical Section Problem

18

Satisfying the 4 properties

• Mutual exclusion– turn must be 0 or 1 => only one thread can be in CS

• Absence of deadlock– turn must be 0 or 1 => one thread will be allowed in

• Absence of unnecessary delay– only one thread trying to get into CS => flag[other] is

false => will get in

• Bounded Waiting– spinning thread will not modify turn– thread trying to go back in will set turn equal to

spinning thread

Page 19: Critical Section Problem

19

Critical Section Problemmultiple threads solutions

• Bakery algorithm

• It’s a mess– Trying to prove it correct is a headache

Page 20: Critical Section Problem

20

Hardware Support

• Provide instruction that is:– Atomic– Fairly easy for hardware designer to

implement

• Read/Modify/Write– Atomically read value from memory, modify it

in some way, write it back to memory

• Use to develop simpler critical section solution for any number of threads.

Page 21: Critical Section Problem

21

Atomic Operation

• An operation that, once started, runs to completion

• Indivisible

• Ex: loads and stores– Meaning: if thread A stores “1” into variable x

and thread B stores “2” into variable x about the same time, result is either “1” or “2”.

Page 22: Critical Section Problem

22

Test-and-Set

• Many machines have itbool TestAndSet(bool &target) {

bool b = target;

target = true;

return b;

}

• Executes atomically

Page 23: Critical Section Problem

23

CS solution with Test-and-Set

Initially, s == false;

entry () {bool spin;spin = TestAndSet(s);while (spin)

spin = TS(s);}

exit() {S = false;

}

Page 24: Critical Section Problem

24

Basic Idea With Atomic Instructions

• Each thread has a local flag

• One variable shared by all threads

• Use the atomic instruction with flag, shared variable– on a change, all thread to go in– Other threads will not see this change

• When done with CS, set shared varible back to initial state.

Page 25: Critical Section Problem

25

Other Atomic Instructions

The definition of the Swap instruction

void Swap(bool &a, bool &b) {

bool temp = a;

a = b;

b = temp;

}

Page 26: Critical Section Problem

26

Problems with busy-waiting CS solution

• Complicated

• Inefficient– Consumes CPU cycles while spinning

• Priority inversion problem– Low priority thread in CS, high priority thread

spinning can end up causing deadlock

Solution: block when waiting for CS