90 likes | 345 Views
CIS 720. Passing the Baton Technique. Cigarette Smokers Problem. Three smoker processes, one agent Each smoker needs 3 ingredients: tobacco, paper, match 1st smoker has tobacco, 2 nd smoker has paper 3 rd smoker has match
E N D
CIS 720 Passing the Baton Technique
Cigarette Smokers Problem • Three smoker processes, one agent • Each smoker needs 3 ingredients: tobacco, paper, match • 1st smoker has tobacco, • 2nd smoker has paper • 3rd smoker has match • Agent randomly puts two ingredients on the table and the smoker that does not have these two ingredients picks them up and smokes. Agents waits until the smoker has finished smoking.
Solution using semaphores • Smoker1: do true P(paper); P(match); smoke; V(done) od • Smoker2: • do true P(paper); P(tobacco); smoke; V(done) od • Smoker3: • do true P(match); P(tobacco); smoke; V(done) od • Agent: • do true if true V(paper); V(tobacco); • [] true V(paper); V(match); • [] true V(match); V(tobacco); • fi; • P(done) • od
inm = number of matches deposited • outm = number of matches consumed • int = number of tobacco deposited • outt = number of tobacco consumed • inp = number of paper deposited • outp = number of paper consumed
Solution using semaphores • Smoker1: do true < outp = outp + 1; outm = outm + 1 > smoke done = true od • Agent: do true < await (done) if true inp = inp + 1; inm = inm + 1 [] true inp = inp + 1; int = int + 1 [] true int = int + 1; inm = inm + 1 fi >
Solution using semaphores • Smoker1: do true < await (inp > outp /\ inm > outm) outp = outp + 1; outm = outm + 1 > smoke <done = true> od • Agent: do true < await( done) done = false; if true inp = inp + 1; inm = inm + 1 [] true inp = inp + 1; int= int+ 1 [] true int= int+ 1; inm = inm + 1 fi >
Solution using semaphores • Smoker1: do true P(e); if (inp == outp \/ inm == outm) d1++; V(e); P(s1) fi outp = outp + 1; outm = outm + 1; SIGNAL smoke P(e); done = true; SIGNAL od
Solution using semaphores • Agent: do true P(e); if !done da++; V(e); P(d) fi; done = false; if true inp = inp + 1; inm = inm + 1 [] true inp = inp + 1; inm = inm + 1 [] true inp = inp + 1; inm = inm + 1 fi od
SIGNAL: If (inp > outp /\ inm > outm /\ d1 > 0 d1--; V(s1) [] (inp > outp /\ int > outt /\ d2 > 0 d2--; V(s2) [] (inm > outm /\ int > outt /\ d3 > 0 d3--; V(s3) [] (done /\ da > 0 da--; V(d) [] else V(e) fi