0% found this document useful (0 votes)
75 views26 pages

Critical Section Problem: CIS 450 Winter 2003

The document discusses solutions to the critical section problem in concurrent programming. It introduces the concepts of thread control blocks, context switching, and different implementations of threads. It then covers the critical section problem, where a section of code must be executed by only one thread at a time to prevent race conditions. Several attempts at solutions are presented, including using flags, turns, and atomic test-and-set instructions. Hardware support for atomic operations can enable simpler solutions. Issues with busy waiting are also discussed.

Uploaded by

rahul c
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views26 pages

Critical Section Problem: CIS 450 Winter 2003

The document discusses solutions to the critical section problem in concurrent programming. It introduces the concepts of thread control blocks, context switching, and different implementations of threads. It then covers the critical section problem, where a section of code must be executed by only one thread at a time to prevent race conditions. Several attempts at solutions are presented, including using flags, turns, and atomic test-and-set instructions. Hardware support for atomic operations can enable simpler solutions. Issues with busy waiting are also discussed.

Uploaded by

rahul c
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Critical Section Problem

CIS 450 Winter 2003

Professor Jinhua Guo


1

Thread Control Block


OSs 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

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?
3

Implementing Threads in User Space

A user-level threads package


4

Implementing Threads in the Kernel

A threads package managed by the kernel

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

Independent vs. Cooperating Threads


Independent threads
No state shared separate address space

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

Example of Concurrent Program


int 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?
8

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.


9

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
10

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?

11

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 its safe.

12

Structure of threads for Critical Section Problem


Threads do the following While (1) { call entry(); critical section call exit(); do other stuff; }
13

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.
14

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 threads turn*/ }
15

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*/

16

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*/ }
17

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
18

Critical Section Problem multiple threads solutions


Bakery algorithm Its a mess
Trying to prove it correct is a headache

19

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.
20

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.

21

Test-and-Set
Many machines have it
bool TestAndSet(bool &target) { bool b = target; target = true; return b; }

Executes atomically
22

CS solution with Test-and-Set


Initially, s == false; entry () { bool spin; spin = TestAndSet(s); while (spin) spin = TS(s); }

exit() { S = false; }
23

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.
24

Other Atomic Instructions


The definition of the Swap instruction void Swap(bool &a, bool &b) { bool temp = a; a = b; b = temp; }
25

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


26

You might also like