1 / 26

Critical Section Problem

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.

jewel
Download Presentation

Critical Section Problem

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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?

  4. Implementing Threads in User Space A user-level threads package

  5. Implementing Threads in the Kernel A threads package managed by the kernel

  6. Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

  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!

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

  9. insert (head, elem) { Elem->next := head; head := elem; } void *removed (head) { void *tmp; tmp := head; head := head->next; return t; } More Concurrent Programming: Linked lists (head is shared) Assume one thread calls insert and one calls remove.

  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: • two or more threads access a shared variable with no synchronization, and • at least one of the threads writes to the variable

  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?

  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.

  13. Structure of threads for Critical Section Problem • Threads do the following While (1) { call entry(); critical section call exit(); do other stuff; }

  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.

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

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

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

  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

  19. Critical Section Problemmultiple threads solutions • Bakery algorithm • It’s a mess • Trying to prove it correct is a headache

  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.

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

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

  23. CS solution with Test-and-Set Initially, s == false; entry () { bool spin; spin = TestAndSet(s); while (spin) spin = TS(s); } exit() { S = false; }

  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.

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

  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

More Related