Do 5.7 page 160 Midterm will cover through chapter 6. Really section 6.7 as we are skipping the rest of chapter 6 Real-time scheduling Have DEADLINES Hard realtime vs soft realtime For hard realtime normally reserve resourses, for example determine in advance when and for how long each process will run. Want to bound delays. So put preemption points in long syscalls. For soft realtime have serious external priorities. Priority inversion. A > B > C. B running. C holds resource needed by A Soln. Temporally give C the priority of A until it releases resource. Evaluating Algorithms Fixed workload (how choose it) Analytical modeling (queuing theory) Arrival rates service times Little's formula Simulation Not determanistic in that use pseudo random numbers say for cpu burst, I/O wait, etc. HOMEWORK 5.8, 5.9, 5.10 ---------------- End of Chapter 5 ---------------- ---------- Start of Chapter 6 Process Synchronization ------ Error with concurrently incrementing and decrementing a shared variable. Critical Section Problem loop Entry section Critical section Exit section Remainder section (non-critical section, NCS) solution requires Mutual exclusion for the critical sections Progress Weaker than book If no process in CS and >0 processes in entry, eventually will have one process in CS Book adds that processes in the NCS cannot effect decision of which process will get into critical section. This is false for the book's soln using test and set. Desirable Properties Some kind of fairness Book (actually requires) bounded waiting, i.e. after process P enters the entry section it will enter the critical section after a bounded number of exectutions of the critical section by other processes. Real nice is linear waiting, the bounded number above is just a multiple of the number of processes. Even better is if the multiple is 1. Even better still is FCFS Solution to Critical Section Problem (some wrong) Soln 1 for 2 processes i and j; code is for i; code for j analogous loop while turn != i CS turn <- j NCS WRONG doesn't satisfy bounded waiting requires strict alternation Soln 2 for 2 processes loop flag[i] <- true while flag[j] CS flag[j] <- false NCS WRONG lock step deadlocks Soln 3 for 2 processes loop while flag[j] flag[i] <- true CS flag[j] <- false NCS WRONG lock step lets both in Soln 4 for 2 processes loop flag[i] <- true turn <- j while (flag[j] & turn=j) CS flag[i] <- false NCS This one is correct and clever. A sensation. Previous solns were MUCH harder Proof of mutual exclusion in book is incomplete. Here is a different (and complete) proof. Write on board two versions of code (for proc 0 and 1) Label the while loops A and B Assume both in CS Assume turn is 1 (turn=0 similar) So turn=1 when 0 was at A So flag[0]=false @ A So when 0 was at A, 1 was in NCS So 1 cannot get into CS ==><== Bakery algorithm for N processes Normal bakery uses fetch-and-add We fake it with (non-atomic) max (a,b) < (c,d) means lexicographically (not atomic) loop choosing[i] <- true number[i] <- max(number[0],...,number[n-1]) + 1 choosing[i] <- false for j = 0 to n-1 while choosing[j] while number[j]!=0 & (number[j],j)<(number[i],i] CS number[i] <- 0 NCS Mutual exclusion Assume A in CS when B wants to enter Book correctly claims B will find A smaller and waits, but why can't B think B < A? When A checked, A < B. A hasn't changed since then If B checkes after A checked, only trouble would be if B got smaller (A stayed the same). But can't (look at code). Similarly, if B checked after A checked, B stays the same and A can't get smaller Easy to see fair (A can't get out and back in while B is waiting) Hardware assist Test-and-set(x) oldx <- x x <- true return oldx loop while TAS(lock) CS lock <- false NCS This does NOT satisfy condition that threads in NCS can't affect who someone trying to get in. The thread in NCS can finish NCS and contend and win. swap(a,b) olda <- a a <- b b <- olda loop key <- true while key swap(key,lock) CS lock <- false NCS Skip bounded waiting mutual exclusion with test-and-set ---------------- Semaphores ---------------- Integer variable that is initialized and then only accessed by wait and signal, which are ATOMIC! wait(S) while S < 0 S <- S-1 S-- in C signal(S) S++ loop wait(mutex) CS signal(mutex) NCS TO IMPLEMENT THE ABOVE DEF OF WAIT AND SIGNAL YOU NEED MUTUAL EXCLUSION HOMEWORK 6.6 The above is a busy waiting def. A busy-waiting mutual exclusion lock is called a spinlock Now do process switching (aka blocking) semaphore wait(S) ***** this must be atomic S.value-- if (S.value < 0) add proc to S.L block signal(S) S.value++ if (S.value <=0) remove a process P from S.L wakeup P