Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #805
| Newsgroups | comp.programming.threads |
|---|---|
| From | "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> |
| Subject | Re: Bakery algorithm - multithreading |
| References | <c8a777ad-e492-4d99-bff7-63f2d8415600@v24g2000yqk.googlegroups.com> |
| Message-ID | <slrnjfkbe6.doj.aggedor@earl-grey.cloud9.net> (permalink) |
| Date | 2011-12-27 14:42 -0600 |
On 2011-12-23, persres <persres@gmail.com> wrote: > Hi, > I saw this code from the book (http://www.faculty.idc.ac.il/gadi/ > book.htm) - > > choosing[i] = true > number[i] = 1 + max(number[1], number[2],...., number[n]) > choosing[i] = false > for j = 1 to n do > wait_until(choosing[j] = false) > wait_until(number[j] = 0 or (number[j], j ) >= (number[i], > i) ) ------> Step 6 > od > critical section > number[i] = 0 > > > The code for max is - > l1 = 0 > for l2 = 1 to n do > l3 = number[l2] > if l1 < l3 then l1 = l3 fi > od > > > A simple algorithm to synchronise n processes. In the above code i is > for process i. The number and choosing are shared registers. > > I don't understand why we need the array choosing. After all, the > condition in step 6 should ensure that process i and process j can > never get into the critical section at the same time. > > Can some one see through this and say if the array choosing can be > removed. If the array choosing or the wait_until(choosing[j] = false) is removed, two processes may be in the critical section at the same time. The following sequence of events is possible. - process 2 does max(number[1:n]) and gets 0 - process 1 does max(number[1:n]) and gets 0 - process 2 does number[2] = 1 + 0 (0 gotten slightly earlier for max(number[1:n]) - process 2 enters critical section, the wait_until is true for all j ((number[1] = 0) and (number[2],2) >= (number[2],2)) - process 1 does number[1] = 1 + 0 (0 gotten slightly earlier for max(number[1:n]) - process 1 enters critical section, the wait_until is true for all j ( (number[1],1) >= (number[1],1)) and (number[2],2) >= (number[1],1)) The array choosing is used so that while process i is determine the value to store in number[i] no other process uses number[i] to determine whether it may enter the critical section.
Back to comp.programming.threads | Previous | Next — Previous in thread | Find similar | Unroll thread
Bakery algorithm - multithreading persres <persres@gmail.com> - 2011-12-23 14:30 -0800 Re: Bakery algorithm - multithreading "A. Bolmarcich" <aggedor@earl-grey.cloud9.net> - 2011-12-27 14:42 -0600
csiph-web