COMP 3500 Lecture 18: 18-Midterm 1 Review-Exercise Handout Key

65 views2 pages

Document Summary

Exercise 1: two processes cooperate in computing the sum of the first n odd integers. Add appropriate semaphore declarations and signal and wait statements to these programs so that the proper value of sqr (i. e. , 25) will be printed out. Solution: semaphore sqr_add = 0, n_minus = 1; loopp: loopq: wait(sqr_add); if (n==0) goto endp. N = n-1; signal(n_minus); goto loopp; endp: print(sqr); Sqr = sqr + 2n + 1; signal(sqr_add); goto loopq; Another solution! loopp: loopq: if (n==0) goto endp wait(sqr_add); N = n-1; signal(n_minus); goto loopp; endp: wait(sqr_add); print(sqr); There are three smokers and one cigarette dealer. A smoker must make a cigarette before he or she can smoke it. Making a cigarette requires (1) tobacco, (2) paper, and (3) matches. Each smoker has one of the three items. 1 smoker has tobacco, another has paper, and a third one has matches. The cigarette dealer has an infinite supply of tobacco, paper, and matches.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers