THE QUEEN S UNIVERSITY OF BELFAST FSP Quick Reference Guide is attached to the end of the examination paper. 110CSC321 Level 3 EXAMINATION FOR THE DEGREE(S) OF MEng, BEng, BSc Concurrent Programming January 2003 Examiners: Professor R Pooley Professor NS Scott and the internal examiners All questions carry equal marks ANSWER 4 QUESTIONS
1. Java Monitors and FSP. (c) By considering the states that a Java thread can be in, describe with the aid of diagrams how mutual exclusion and synchronization are achieved in Java. [8 marks] What potential problem does the Java implementation of a monitor present to the programmer? How can this difficulty be avoided? [4 marks] Consider the following FSP specification. Person=( listen ->Person talk ->Person). LISTENER=(listen->LISTENER). SPEAKER =(talk->speaker). P=(a:LISTENER b:speaker {a,b::person). i. Draw the LTS graph for the composite process P. Give an example of a valid trace of P. ii. Processes LISTENER and SPEAKER are now replaced by LISTENER=(listen->LISTENER)+{talk. SPEAKER =(talk->speaker)+{listen. Draw the LTS graph for the modified composite process P. Give an example of a valid trace of P. Explain why the meaning of P has changed. [8 marks] 2 of 10
2. The Mutual Exclusion Problem. In the following attempted solution to the mutual exclusion problem the instructions 1b and 2b are atomic actions. Show that the algorithm can lead to starvation. int c=0; process P1 int L1 = 0; 1a: noncriticalsection1; do { 1b: <L1=c; c=1;> while (L1!= 0); 1c: criticalsection1; 1d: c = 0; end P1; process P2 int L2 = 0; 2a: noncriticalsection2; do { 2b: <L2=c; c=1;> while (L2!= 0); 2c: criticalsection2; 2d: c = 0; end P2; [4 marks] Consider the following attempted solution to the mutual exclusion problem Show that the algorithm does not satisfy the mutual exclusion property. int c1=1; int c2=1; process P1 1a: noncriticalsection1; 1b: c1=0; while (c2!=1) { 1c: c1=1; 1d: c1=0; 1e: criticalsection1; 1f: c1=1; end P1; process P2 2a: noncriticalsection2; 2b: c2=0; while (c1!=1) { 2c: c2=0; 2d: c2=1; 2e: criticalsection2; 2f: c2=1; end P2; (c) (d) [4 marks] Construct a Java solution to the mutual exclusion problem for 3 processes, p[0],p[1] and p[2], subject to the following constraints. i. The solution must be bare machine algorithm that does not use hardware assisted instructions. ii. It is guaranteed that no process will halt in its non-critical section i.e. starvation in the absence of contention is not an issue. iii. Process p[2] is first to enter its critical section. iv. When process p[i] leaves its critical section the next process to enter its critical section is p[(i+1)%3], where i=0,1,2. Prove that the program in (c) does not deadlock. 3 of 10
3. Semaphores. The Dining Philosophers problem can be specified as follows. "Five philosophers sit around a circular table. Each philosopher spends his life alternately thinking and eating. In the centre of the table is a large bowl of spaghetti. A philosopher needs two forks to eat a helping of spaghetti. One fork is placed between each pair of philosophers and they agree that each will only use the fork to their immediate right and left." i. Construct a semaphore solution to the Dining Philosophers problem that is correct in all respects except for the prevention of deadlock. ii. What are the four necessary and sufficient conditions for deadlock to occur? Show how each condition occurs in your semaphore solution. Suggest a strategy that will prevent deadlock. [12 marks] One proposed solution to the Dining Philosophers problem permits only four philosophers to sit down at the table at the same time. Two implementations are shown below. Assume in each a blocked-set semaphore implementation. i. Give an argument to show that in this implementation it is impossible to starve on the semaphore placesleft. Semaphore placesleft = new Semaphore(4); process Phil[i=0..4] P(placesLeft) eat think V(placesLeft) end Phil; [4 marks] ii. Show that in this implementation it is possible to starve on the semaphore placesleft. Semaphore placesleft = new Semaphore(4); Semaphore exit = new Semaphore(0); process Phil[i=0..4] P(placesLeft) eat think V(exit) end Phil; process Butler P(exit) V(placesLeft) end Butler; [4 marks] 4 of 10
4. Monitors. Hotel Ashoka has only one sauna for hotel guests. Any number of men can use the sauna at the same time and any number of women can use the sauna at the same time. However, men and women are not permitted to use it together. Consider the following Java monitor that is designed to solve the above problem. The class data nmen and nwomen record the number of men and the number of women in the sauna respectively. Complete the four monitor methods. Do not introduce any additional class data. class Sauna { private int nmen =0; private int nwomen =0; synchronized public void menenter() throws InterruptedException { synchronized public void menexit () { synchronized public void womenenter () throws InterruptedException { synchronized public void womenexit () { (c) Describe how your monitor works and explain why your solution is unfair. The class data in above is extended to include the following, private int waitingmen = 0; private int waitingwomen =0; private boolean mensturn=false; where waitingmen records the number of men who are waiting, waitingwomen records the number of women who are waiting and mensturn is a flag used to ensure fairness. Devise a fair solution that avoids deadlock. Explain how the solution works. Give the corresponding Java code for the menenter() and menexit() methods only. [8 marks] 5 of 10
5. Modelling Processes Using FSP. Consider the following problem. "A canyon cuts through the territory of a colony of baboons. The baboons use a rope stretching across the canyon to cross from one side to the other. The rope is strong enough to permit any number of baboons to cross in the same direction at the same time. However, the rope is too thin for the baboons to cross the canyon in both directions at the same time. " The activities of the baboons can be modelled using the FSP specification given below. Prefixes lr and rl refer to baboons moving left-right and right-left across the rope respectively. const N =.. range T = 0..N range ID = 1..N // baboon identities BABOON = (enter->exit->baboon). ENTER = C[1], C[i:ID] = ([i].enter-> C[i%N+1]). EXIT = C[1], C[i:ID] = ([i].exit-> C[i%N+1]). FIFO = ([ID]:BABOON ENTER EXIT). BABOONS = (lr:fifo rl:fifo). ROPE =. BaboonsCrossingCanyon = (BABOONS ROPE ONEWAY). property ONEWAY =. (c) For N = 2 draw the LTSA graph for the composite process FIFO. What effect do the processes ENTER and EXIT have on the actions of the baboons? Complete the FSP process ROPE so that baboons can move concurrently on the rope only if they are moving in the same direction. Explain how your process satisfies this requirement. Note: The issue of starvation should be ignored. Describe a safety property to check that left-right moving baboons do not collide with right-left moving baboons. Implement the check as a FSP safety property ONEWAY. If the ROPE constraint is removed, give an example of a trace that violates the safety property and explain how it would be detected by the LTSA tool as a safety violation. [8 marks] 6 of 10
6. FSP Models and their Java Implementation. A, S and J work in a bar. A washes the dirty glasses one at a time and places each on a workspace. The workspace can hold a maximum of 20 glasses. S and J remove the glasses from the workspace one at a time and dry them. [You can ignore what happens to a glass after it is dried.] Unfortunately, there is only one drying cloth so S and J have to take turns at using it. However, as J is able to dry faster he dries two glasses at each turn while S only dries one. S takes the first turn at drying. Model the concurrent activity in the bar using FSP. Your model should include the processes A,S J and WORKSPACE and use the alphabet {wash, sdry, jdry. [10 marks] Identify the passive process in your model and implement it as a Java monitor. [10 marks] 7 of 10
FSP Quick Reference 1. Processes A process is defined by one or more local processes separated by commas. The definition is terminated by a full stop. STOP and ERROR are primitive local processes. Example Process = (a -> Local), Local = (b -> STOP). Action Prefix -> Choice Guarded Action when Alphabet Extension + Table: 1 Process operators If x is an action and P a process then (x -> P) describes a process that initially engages in the action x and then behaves exactly as described by P. If x and y are actions then (x->p y->q) describes a process which initially engages in either of the actions x or y. After the first action has occurred, the subsequent behavior is described by P if the first action was x and Q if the first action was y. The choice (when B x -> P y -> Q) means that when the guard B is true then the actions x and y are both eligible to be chosen, otherwise if B is false then the action x cannot be chosen. The alphabet of a process is the set of actions in which it can engage. P + S extends the alphabet of the process P with the actions in the set S. 8 of 10
2. Composite Processes A composite process is the parallel composition of one or more processes. The definition of a composite process is preceded by. Example Composite = (P Q). Parallel Composition Replicator forall Process Labeling : Process Sharing :: Priority High << Priority Low >> Table 2: Composite Process Operators If P and Q are processes then (P Q) represents the concurrent execution of P and Q. forall [i:1..n] P(i) is the parallel composition (P(1) P(N)) a:p prefixes each label in the alphabet of P with a. {a1,..,ax::p replaces every label n in the alphabet of P with the labels a1.n,,ax.n. Further, every transition (n->q) in the definition of P is replaced with the transitions ({a1.n,,ax.n->q). C =(P Q)<<{a1,,an specifies a composition in which the actions a1,,an have higher priority than any other action in the alphabet of P Q including the silent action tau. In any choice in this system which has one or more of the actions a1,,an labelling a transition, the transitions labelled with lower priority actions are discarded. C=(P Q)>>{a1,,an specifies a composition in which the actions a1,,an have lower priority than any other action in the alphabet of P Q including the silent action tau. In any choice in this system which has one or more transitions not labeled by a1,,an, the transitions labeled by a1,,an are discarded. 9 of 10
3. Common Operators The operators in Table 3 may be used in the definition of both processes and composite processes. Conditional if then else Re-labeling / Hiding \ Interface @ Table 3: Common Process Operators The process if B then P else Q behaves as the process P if the condition B is true otherwise it behaves as Q. If the else Q is omitted and B is false, then the process behaves as STOP. Re-labelling is applied to a process to change the names of action labels. The general form of re-labelling is: /{newlabel_1/oldlabel_1, newlabel_n/oldlabel_n. When applied to a process P, the hiding operator \{a1..ax removes the action names a1..ax from the alphabet of P and makes these concealed actions "silent". These silent actions are labelled tau. Silent actions in different processes are not shared. When applied to a process P, the interface operator @{a1..ax hides all actions in the alphabet of P not labeled in the set a1..ax. 4. Properties Safety property Progress progress Table 4: Safety and Progress Properties A safety property P defines a deterministic process that asserts that any trace including actions in the alphabet of P, is accepted by P. progress P = {a1,a2..an defines a progress property P which asserts that in an infinite execution of a target system, at least one of the actions a1,a2..an will be executed infinitely often. 10 of 10