📄 http:^^www.cs.wisc.edu^~bart^537^quizzes^quiz2.html
字号:
Date: Mon, 11 Nov 1996 17:25:22 GMTServer: NCSA/1.5Content-type: text/htmlLast-modified: Tue, 20 Feb 1996 13:25:27 GMTContent-length: 2251<html><head><title>CS 537 - Quiz #2</title></head><body><table border=0 width=100% align=center><tr><td width=25%><td width=50% align=center><b>UNIVERSITY OF WISCONSIN-MADISON<br>Computer Sciences Department</b><td width=25%><tr><tr><td><b>CS 537<br>Spring 1996 </b><td><td align=right><b>Bart Miller</b><tr><td><td align=center><b>Quiz #2</b><br>Wednesday, February 14<td></table><h2>Too much of too much milk</h2>Following is a solution to the<i>N</i>bottle Too Much Milk problem using semaphores.The problem is to have a refrigerator that we will try to keep fullwith<i>N</i>bottles of milk.<p>You live in a Co-op with many roommates.Each time a roommate comes home, they check the refrigerator to see ifmilk is needed.If so, they go to the store and buy a bottle.Each person only buys one bottle at a time.<p>Each roommate will be a process.The procedure<tt>MilkCheck</tt>is executed by each roommate process when arriving home.This procedure has the roommate check the refrigerator, and buymilk if necessary.<ul><li>The solution should make sure that if a roommate arrives home and milkis needed, that they buy a bottle.<li>It should also make sure that you never have more than<i>N</i>bottles of milk in the refrigerator at any one time.<li><i>Important:Try to minimize the time that any roommate process stays idle aroundthe refrigerator.In other words, don't force a roommate towait idle while another roommate is at the store.</i></ul><p>The program below is not quite correct.You are to fix this program so that is satisfies the above criteria.<p>The <b>bold face</b> text has been added to correct the problem.<p><table width=100% border=1 align=center><tr><td><b>Initialization</b><pre>semaphore mutex(<b>1</b>);int numberOfBottles = 0;int numberOfShoppers = 0;</pre><tr><td><pre>MilkCheck (){ mutex.P(); if ((numferOfBottles + numberOfShoppers) <b><</b> N) { numberOfShoppers++; <b>mutex.V();</b> BuyABottleOfMilk(); <b>mutex.P();</b> numberOfBottles++; numberOfShoppers--; } mutex.V();}</pre></table><hr><H4>Last modified:Tue Feb 13 08:36:53 CST 1996by<a href="http://www.cs.wisc.edu/~bart">bart</a></b></H4></body>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -