Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OS Winter 06
OS Winter 03
Choosing a ticket
Shared : int number[ n ]; boolean choosing [n];
number [i ] ! max( number[0], number [1], - number [ n 1]) 1;
Basic Lemma
Lemma 1:
For any i { j , i
i
is in C and
is in C (T - D), then
OS Winter 03
Lemma 1 - proof
Denote s time point where lemmas conditions
hold At time s, P is in C (critical section) i
number[i] has been selected and still exists number[j] has been selected and still exists Exist time point t1<s, where P performs a check i (choosing[j]==0) and passes it Exists time point t2, t1<t2<s, where P performs a i check (number[j]!=0) and (number[i],i)<(number[j],j)
OS Winter 03
OS Winter 03
i already
Lemmata 2,3,4
Lemma 2 mutual exclusion:
Bakery Algorithm satisfies mutual exclusion property
Lemma 3 progress
Bakery Algorithm guarantees progress
Lemma 4 fairness
Bakery Algorithm guarantees lockoutfreedom
OS Winter 03
Lemma 2 Proof
Assume in contradiction that there are two
different processes that have entered critical section Then conditions of Lemma 1 are true for both processes simmetrically that is (number[i],i)<(number[j],j]) and (number[j],j)<(number[i],i) : contradiction We conclude that mutual exclusion is satisfied
OS Winter 03
Lemma 3 Proof
Suppose progress is not guaranteed Then eventually a point is reached after which
all processes are in T or R By the code, all the processes in T eventually complete D and reach T-D Then the process with the lowest (number,ID) pair is not blocked from reaching C, that is enters critical section We conclude that Bakery algorithm satisfies Progress
OS Winter 03
Lemma 4 Proof
Consider a particular process
never reaches C The process eventually completes D and reaches T-D After that any new process that enters D perceives number[i] and chooses a higher number Thus, since P does not reach C, none of these new i processes reach C either But by Lemma 3 there must be continuing progress, that is infinitely many entries to C Contradiction: P blocks the entry to C i
OS Winter 03
Pi in T and suppose it
Remark on Fairness
A process that obtained a ticket (number[k],k)
will wait at most for (n-1) turns, when other processes will enter the critical section For example if all the processes obtained their tickets at the same time they will look like (q,1),(q,2)(q,n) In which case process n will wait for processes 1 n 1 to complete the critical section
OS Winter 03
Bibliography
Nancy Ann Lynch, Distributed
Algorithms, JMC 243.9 LY53 Leslie Lamport A new solution of Dijkstras concurrent programming problem Communications of the ACM 17(8):453-455, 1974
OS Winter 03
Hardware primitives
Elementary building blocks capable of
performing certain steps atomically
Should be universal to allow for solving versatile synchronization problems
Test-and-Set (TS)
boolean test-and-set(boolean &lock) { temp=lock; lock=TRUE; return temp; } reset(boolean &lock) { lock=FALSE; }
OS Winter 03
Discussion
Satisfies Mutual Exclusion and Progress Does not satisfies Fairness Provides exclusion among unbounded
number of processes
Process IDs and number are unknown
Busy waiting
Burning CPU cycles while being blocked
OS Winter 03
Fetch-and-Add (FAA)
s: shared, a: local int FAA(int &s, int a) { temp=s; s=s+a; return temp; FAA can be used } as a ticket machine
OS Winter 03
Discussion
Satisfies all three properties Supports unbounded number of
processes Unbounded counter Busy waiting
OS Winter 03
OS Winter 03