critical sections with lots of threads. announcements cs 4411 project due yesterday, wednesday, sept...
Post on 21-Dec-2015
217 views
TRANSCRIPT
![Page 1: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/1.jpg)
Critical Sections with lots of Threads
![Page 2: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/2.jpg)
Announcements
• CS 4411 project due yesterday, Wednesday, Sept 17th
• CS 4410 Homework 2 available, due Tuesday, Sept 23rd
![Page 3: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/3.jpg)
Review: Race conditions• Definition: timing dependent error involving shared state
– Whether it happens depends on how threads scheduled
• Hard to detect:
– All possible schedules have to be safe • Number of possible schedule permutations is huge
• Some bad schedules? Some that will work sometimes?
– they are intermittent
• Timing dependent = small changes can hide bug
![Page 4: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/4.jpg)
The Fundamental Issue: Atomicity
• Our atomic operation is not done atomically by machine– Atomic Unit: instruction sequence guaranteed to execute indivisibly– Also called “critical section” (CS)
When 2 processes want to execute their Critical Section,– One process finishes its CS before other is allowed to enter
![Page 5: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/5.jpg)
Revisiting Race Conditions
Process b: while(i > -10)
i = i - 1; print “B won!”;
Process a: while(i < 10)
i = i +1; print “A won!”;
– Who wins?– Will someone definitely win?
![Page 6: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/6.jpg)
Critical Section Problem
• Problem: Design a protocol for processes to cooperate, such that only one process is in its critical section– How to make multiple instructions seem like one?
Processes progress with non-zero speed, no assumption on clock speed
Used extensively in operating systems:Queues, shared variables, interrupt handlers, etc.
Process 1
Process 2
CS1
Time
CS2
![Page 7: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/7.jpg)
Solution StructureShared vars:
Initialization:
Process:. . . . . .
Entry Section
Critical Section
Exit Section
Added to solve the CS problem
![Page 8: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/8.jpg)
Solution Requirements
• Mutual Exclusion– Only one process can be in the critical section at any time
• Progress– Decision on who enters CS cannot be indefinitely postponed
• No deadlock
• Bounded Waiting– Bound on #times others can enter CS, while I am waiting
• No livelock
• Also efficient (no extra resources), fair, simple, …
![Page 9: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/9.jpg)
Refresher: Dekker’s Algorithm
• Assumes two threads, numbered 0 and 1 CSEnter(int i)
{inside[i] = true;while(inside[J]){ if (turn == J) { inside[i] = false; while(turn == J) continue; inside[i] = true; }}}
CSExit(int i){
turn = J;inside[i] = false;}
![Page 10: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/10.jpg)
Peterson’s Algorithm (1981)
CSEnter(int i){
inside[i] = true;turn = J;while(inside[J] && turn == J)
continue;
}
CSExit(int i){
inside[i] = false;}
• Simple is good!!
![Page 11: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/11.jpg)
Napkin analysis of Peterson’s algorithm:
• Safety (by contradiction): – Assume that both processes (Alan and Shay) are in their critical
section (and thus have their inside flags set). Since only one, say Alan, can have the turn, the other (Shay) must have reached the while() test before Alan set his inside flag.
– However, after setting his inside flag, Alan gave away the turn to Shay. Shay has already changed the turn and cannot change it again, contradicting our assumption.
Liveness & Bounded waiting => the turn variable.
![Page 12: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/12.jpg)
Can we generalize to many threads?
• Obvious approach won’t work:
• Issue: Who’s turn next?
CSEnter(int i){
inside[i] = true;for(J = 0; J < N; J++)
while(inside[J] && turn == J)continue;
}
CSExit(int i){
inside[i] = false;
}
![Page 13: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/13.jpg)
Bakery “concept”
• Described by Leslie Lamport
• Think of a popular store with a crowded counter, perhaps the pastry shop in Montreal’s fancy market– People take a ticket from a machine– If nobody is waiting, tickets don’t matter– When several people are waiting, ticket order
determines order in which they can make purchases
![Page 14: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/14.jpg)
Bakery Algorithm: “Take 1”
• int ticket[n];• int next_ticket;
CSEnter(int i){
ticket[i] = ++next_ticket;for(J = 0; J < N; J++)
while(ticket[J] && ticket[J] < ticket[i])continue;
}
CSExit(int i){
ticket[i] = 0;
}
• Oops… access to next_ticket is a problem!
![Page 15: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/15.jpg)
Bakery Algorithm: “Take 2”
• int ticket[n];
CSEnter(int i){
ticket[i] = max(ticket[0], … ticket[N-1])+1;for(J = 0; J < N; J++)
while(ticket[J] && ticket[j] < ticket[i])continue;
}
CSExit(int i){
ticket[i] = 0;
}
• Clever idea: just add one to the max.
Just add 1 to the max!
• Oops… two could pick the same value!
![Page 16: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/16.jpg)
Bakery Algorithm: “Take 3”
If i, j pick same ticket value, id’s break tie:
(ticket[J] < ticket[i]) || (ticket[J]==ticket[i] && J<i)
Notation: (B,J) < (A,i) to simplify the code:
(B<A || (B==A && J<i)), e.g.:
(ticket[J],J) < (ticket[i],i)
![Page 17: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/17.jpg)
Bakery Algorithm: “Take 4”
• int ticket[N];• boolean picking[N] = false;
CSEnter(int i){
ticket[i] = max(ticket[0], … ticket[N-1])+1;for(J = 0; J < N; J++)
while(ticket[J] && (ticket[J],J) < (ticket[i],i))
continue;
}
CSExit(int i){
ticket[i] = 0;
}
• Oops… i could look at J when J is still storing its ticket, and yet J could have a lower id than me (i)!
![Page 18: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/18.jpg)
Bakery Algorithm: Almost final
• int ticket[N];• boolean choosing[N] = false;
CSEnter(int i){
choosing[i] = true;ticket[i] = max(ticket[0], … ticket[N-1])+1;choosing[i] = false;for(J = 0; J < N; J++) {
while(choosing[J]) continue;while(ticket[J] && (ticket[J],J) <
(ticket[i],i))continue;
}}
CSExit(int i){
ticket[i] = 0;
}
![Page 19: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/19.jpg)
Bakery Algorithm: Issues?
• What if we don’t know how many threads might be running?– The algorithm depends on having an agreed
upon value for N– Somehow would need a way to adjust N when
a thread is created or one goes away
• Also, technically speaking, ticket can overflow!– Solution: Change code so that if ticket is “too
big”, set it back to zero and try again.
![Page 20: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/20.jpg)
Bakery Algorithm: Final• int ticket[N]; /* Important: Disable thread scheduling when changing N */• boolean choosing[N] = false;
CSEnter(int i){
do { ticket[i] = 0; choosing[i] = true; ticket[i] = max(ticket[0], … ticket[N-1])+1; choosing[i] = false;} while(ticket[i] >= MAXIMUM);for(J = 0; J < N; J++) {
while(choosing[J]) continue;while(ticket[J] && (ticket[J],J) < (ticket[i],i))
continue;
}}
CSExit(int i){
ticket[i] = 0;
}
![Page 21: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/21.jpg)
How do real systems do it?
• Some real systems actually use algorithms such as the bakery algorithm– A good choice where busy-waiting isn’t going to be
super-inefficient– For example, if you have enough CPUs so each
thread has a CPU of its own
• Some systems disable interrupts briefly when calling CSEnter() and CSExit()
• Some use hardware “help”: atomic instructions
![Page 22: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/22.jpg)
Critical Sections with Atomic Hardware Primitives
Process i
While(test_and_set(&lock));
Critical Section
lock = false;
Share: int lock;Initialize: lock = false;
Problem: Does not satisfy liveness (bounded waiting)(see book for correct solution, Figure 6.8)
Assumes that test_and_set is compiled to a special hardware instruction that sets the lock and
returns the OLD value (true: locked; false: unlocked)
![Page 23: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/23.jpg)
test_and_set Instruction
• Definition:
boolean test_and_set (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
![Page 24: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/24.jpg)
Solution using TestAndSet• Shared boolean variable lock., initialized to false.• Solution: while (true) { while ( TestAndSet (&lock )) ; /* do nothing
// critical section
lock = FALSE;
// remainder section }
![Page 25: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/25.jpg)
Swap Instruction
• Definition:
void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }
![Page 26: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/26.jpg)
Solution using Swap• Shared Boolean variable lock initialized to FALSE; Each
process has a local Boolean variable key.• Solution:
while (true) { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section
lock = FALSE;
// remainder section }
![Page 27: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/27.jpg)
Presenting critical sections to users
• CSEnter and CSExit are possibilities
• But more commonly, operating systems have offered a kind of locking primitive
• We call these semaphores
![Page 28: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/28.jpg)
Semaphores• Non-negative integer with atomic increment and decrement• Integer ‘S’ that (besides init) can only be modified by:
– P(S) or S.wait(): decrement or block if already 0– V(S) or S.signal(): increment and wake up process if any
• These operations are atomic (indivisible)
semaphore S;
P(S) { while(S ≤ 0) ; S--;}
V(S) { S++;}
Some systems use the operation wait() instead of P()
These systems use the operation signal() instead of V()
![Page 29: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/29.jpg)
Semaphore Types
• Counting Semaphores:– Any integer– Used for synchronization
• Binary Semaphores– Value is limited to 0 or 1– Used for mutual exclusion (mutex)
Shared: semaphore S
Init: S = 1;
Process i
P(S);
Critical Section
V(S);
![Page 30: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/30.jpg)
Semaphore Implementation
• Must guarantee that no two processes can execute P () and V () on the same semaphore at the same time– No process may be interrupted in the middle of these operations
• Thus, implementation becomes the critical section problem where the P and V code are placed in the critical section.– Could now have busy waiting in critical section implementation
• But implementation code is short• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical sections and therefore this is not a good solution.
![Page 31: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/31.jpg)
Semaphore Implementation with no Busy waiting
• With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items:– value (of type integer)– pointer to next record in the list
• Two operations:– block – place the process invoking the operation on
the appropriate waiting queue.– wakeup – remove one of processes in the waiting
queue and place it in the ready queue.
![Page 32: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/32.jpg)
Implementing Semaphores
• Busy waiting (spinlocks)Consumes CPU resourcesNo context switch overhead
• Alternative: Blocking• Should spin or block?
– Less time spin– More time block– A theory result:
• Spin for as long as block cost• If lock not available, then block• Shown factor of 2-optimal!
typedef struct semaphore {int value:ProcessList L;
} Semaphore;
void P(Semaphore *S) {S->value = S->value - 1;if (S.value < 0) {
add this process to S.L;block();
}}
void V(Semaphore *S) {S->value = S->value + 1;if (S->value <= 0) {
remove process P from S.L;
wakeup P}
}
![Page 33: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/33.jpg)
Implementing Semaphores
• Per-semaphore list of processes– Implemented using PCB link field– Queuing Strategy: FIFO works fine
• Will LIFO work?
![Page 34: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/34.jpg)
Common programming errors
Process i
P(S)CSP(S)
Process j
V(S)CSV(S)
Process k
P(S)CS
A typo. Process I will get stuck (forever) the second time it does the P() operation. Moreover, every other process will freeze up too when trying
to enter the critical section!
A typo. Process J won’t respect mutual exclusion even if the other
processes follow the rules correctly. Worse still, once we’ve done two
“extra” V() operations this way, other processes might get into the CS
inappropriately!
Whoever next calls P() will freeze up. The bug might be confusing
because that other process could be perfectly correct code, yet that’s the one you’ll see hung when you use the debugger to look at its state!
![Page 35: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/35.jpg)
More common mistakes
• Conditional code that can break the normaltop-to-bottom flow of codein the critical section
• Often a result of someonetrying to maintain aprogram, e.g. to fix a bugor add functionality in codewritten by someone else
P(S)if(something or other) return;CSV(S)
![Page 36: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/36.jpg)
What if buffer is full?
Producer
do { . . . // produce an item in nextp . . . P(mutex); P(empty); . . . // add nextp to buffer . . . V(mutex); V(full);} while (true);
What’s wrong?Shared: Semaphores mutex, empty, full;
Init: mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty bufs */ full = 0; /* number full bufs */
Consumer
do { P(full); P(mutex); . . . // remove item to nextc . . . V(mutex); V(empty); . . . // consume item in nextc . . . } while (true);
Oops! Even if you do the correct operations, the order in which you do semaphore operations can have an
incredible impact on correctness
![Page 37: Critical Sections with lots of Threads. Announcements CS 4411 project due yesterday, Wednesday, Sept 17 th CS 4410 Homework 2 available, due Tuesday,](https://reader036.vdocuments.site/reader036/viewer/2022062516/56649d6b5503460f94a49bbc/html5/thumbnails/37.jpg)
In Summary…• Fundamental Issue
– Programmers atomic operation is not done atomically– Atomic Unit: instruction sequence guaranteed to execute indivisibly– Also called “critical section” (CS)
• Critical Section Implementation– Software: Dekker’s, Peterson’s, Baker’s algorithm– Hardware: test_and_set, swap
• Hard for programmers to use
– Operating System: semaphores
• Implementing Semaphores– Multithread synchronization algorithms shown earlier– Could have a thread disable interrupts, put itself on a “wait queue”, then
context switch to some other thread (an “idle thread” if needed)– The O/S designer makes these decisions and the end user shouldn’t need
to know