{"id":183,"date":"2013-04-17T12:15:15","date_gmt":"2013-04-17T04:15:15","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=183"},"modified":"2013-04-17T12:15:15","modified_gmt":"2013-04-17T04:15:15","slug":"sleeping-barber-problem","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=183","title":{"rendered":"Sleeping barber problem \uff08\u7761\u89c9\u7684\u7406\u53d1\u5e08\u95ee\u9898\uff09"},"content":{"rendered":"<p>\u4eca\u5929\u505a\u64cd\u4f5c\u7cfb\u7edf\u4f5c\u4e1a\u78b0\u5230\u7684\u95ee\u9898\uff0c\u4e00\u65f6\u60f3\u4e0d\u51fa\u600e\u4e48\u505a\uff0c\u540e\u6765Google\u4e86\u4e00\u4e0b\u53d1\u73b0\u662f\u4e2a\u7ecf\u5178\u95ee\u9898\u3002<br \/>\n=========================================================================<br \/>\n\u4ee5\u4e0b\u82f1\u6587\u5185\u5bb9\u8f6c\u8f7d\u81ea\uff1a<a title=\"http:\/\/www.answers.com\/topic\/sleeping-barber-problem\" href=\"http:\/\/www.answers.com\/topic\/sleeping-barber-problem\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.answers.com\/topic\/sleeping-barber-problem<\/a><br \/>\n\u672c\u4eba\u7684\u7406\u89e3\u7ee7\u7eed\u5f80\u4e0b\u770b\u3002<br \/>\nIn\u00a0<a href=\"http:\/\/www.answers.com\/topic\/computer-science\">computer science<\/a>, the\u00a0<b>sleeping barber problem<\/b>\u00a0is a classic\u00a0<a href=\"http:\/\/www.answers.com\/topic\/inter-process-communication\">inter-process communication<\/a>\u00a0and\u00a0<a href=\"http:\/\/www.answers.com\/topic\/synchronization\">synchronization<\/a>\u00a0problem between multiple\u00a0<a href=\"http:\/\/www.answers.com\/topic\/operating-system\">operating system<\/a>\u00a0<a href=\"http:\/\/www.answers.com\/topic\/process-computing\">processes<\/a>. The problem is analogous to that of keeping a barber working when there are customers, resting when there are none and doing so in an orderly manner.<br \/>\nThe analogy is based upon a hypothetical barber shop with one barber. The barber has one barber chair and a waiting room with a number of chairs in it. When the barber finishes cutting a customer&#8217;s hair, he dismisses the customer and then goes to the waiting room to see if there are other customers waiting. If there are, he brings one of them back to the chair and cuts his hair. If there are no other customers waiting, he returns to his chair and sleeps in it.<br \/>\nEach customer, when he arrives, looks to see what the barber is doing. If the barber is sleeping, then the customer wakes him up and sits in the chair. If the barber is cutting hair, then the customer goes to the waiting room. If there is a free chair in the waiting room, the customer sits in it and waits his turn. If there is no free chair, then the customer leaves. Based on a na\u00efve analysis, the above description should ensure that the shop functions correctly, with the barber cutting the hair of anyone who arrives until there are no more customers, and then sleeping until the next customer arrives. In practice, there are a number of problems that can occur that are illustrative of general scheduling problems.<br \/>\nThe problems are all related to the fact that the actions by both the barber and the customer (checking the waiting room, entering the shop, taking a waiting room chair, etc.) all take an unknown amount of time. For example, a customer may arrive and observe that the barber is cutting hair, so he goes to the waiting room. While he is on his way, the barber finishes the haircut he is doing and goes to check the waiting room. Since there is no one there (the customer not having arrived yet), he goes back to his chair and sleeps. The barber is now waiting for a customer and the customer is waiting for the barber. In another example, two customers may arrive at the same time when there happens to be a single seat in the waiting room. They observe that the barber is cutting hair, go to the waiting room, and both attempt to occupy the single chair.<br \/>\nThe Sleeping Barber Problem is often attributed to\u00a0<a href=\"http:\/\/www.answers.com\/topic\/edsger-w-dijkstra\">Edsger Dijkstra<\/a>\u00a0(1965), one of the pioneers in computer science.<br \/>\nMany possible solutions are available. <span style=\"color: #ff6600;\"><strong>The key element of each is a\u00a0<a href=\"http:\/\/www.answers.com\/topic\/mutex\"><span style=\"color: #ff6600;\">mutex<\/span><\/a>, which ensures that only one of the participants can change state at once.<\/strong><\/span> The barber must acquire this mutex exclusion before checking for customers and release it when he begins either to sleep or cut hair. A customer must acquire it before entering the shop and release it once he is sitting in either a waiting room chair or the barber chair. This eliminates both of the problems mentioned in the previous section. A number of\u00a0<a href=\"http:\/\/www.answers.com\/topic\/semaphore\">semaphores<\/a>\u00a0are also required to indicate the state of the system. For example, one might store the number of people in the waiting room.<br \/>\nA\u00a0<i>multiple sleeping barbers problem<\/i>\u00a0has the additional complexity of coordinating several barbers among the waiting customers.<\/p>\n<h2>Implementation<\/h2>\n<ul>\n<li>The following\u00a0<a href=\"http:\/\/www.answers.com\/topic\/pseudocode\">pseudocode<\/a>\u00a0guarantees synchronization between barber and customer and is deadlock free, <span style=\"color: #ff6600;\">but may lead to\u00a0<a href=\"http:\/\/www.answers.com\/topic\/resource-starvation\"><span style=\"color: #ff6600;\">starvation<\/span><\/a>\u00a0of a customer<\/span>. The functions wait() and signal() are functions provided by the\u00a0<a href=\"http:\/\/www.answers.com\/topic\/semaphore\">semaphores<\/a>.<\/li>\n<\/ul>\n<div dir=\"ltr\">\n<div>\n<pre># The first two are mutexes (only 0 or 1 possible)\nSemaphore barberReady = 0\nSemaphore accessWRSeats = 1     # if 1, the # of seats in the waiting room can be incremented or decremented\nSemaphore custReady = 0         # the number of customers currently in the waiting room, ready to be served\nint numberOfFreeWRSeats = N     # total number of seats in the waiting room\ndef Barber():\n  while true:                   # Run in an infinite loop.\n    wait(custReady)             # Try to acquire a customer - if none is available, go to sleep.\n    wait(accessWRSeats)         # Awake - try to get access to modify # of available seats, otherwise sleep.\n    numberOfFreeWRSeats += 1    # One waiting room chair becomes free.\n    signal(barberReady)         # I am ready to cut.\n    signal(accessWRSeats)       # Don't need the lock on the chairs anymore.\n    # (Cut hair here.)\ndef Customer():\n  while true:                   # Run in an infinite loop to simulate multiple customers.\n    wait(accessWRSeats)         # Try to get access to the waiting room chairs.\n    if numberOfFreeWRSeats &gt; 0: # If there are any free seats:\n      numberOfFreeWRSeats -= 1  #   sit down in a chair\n      signal(custReady)         #   notify the barber, who's waiting until there is a customer\n      signal(accessWRSeats)     #   don't need to lock the chairs anymore\n      wait(barberReady)         #   wait until the barber is ready\n      # (Have hair cut here.)\n    else:                       # otherwise, there are no free seats; tough luck --\n      signal(accessWRSeats)     #   but don't forget to release the lock on the seats!\n      # (Leave without a haircut.)<\/pre>\n<\/div>\n<\/div>\n<h2>============================<\/h2>\n<p>\u672c\u4eba\u89c2\u70b9\uff1a<br \/>\n\u5176\u4e2d\u9700\u8981\u5171\u4eab\u7684\u6570\u636e\u5e94\u8be5\u53ea\u6709\u53ef\u7528\u7684\u5ea7\u6905\u6570\u76ee(numberOfFreeWRSeats)\uff0c\u4f46\u662f\u9700\u8981\u6539\u52a8\u8fd9\u4e2a\u5ea7\u6905\u6570\u76ee\u7684\u8fc7\u7a0b\u6bd4\u8f83\u7e41\u6742&#8230;\u539f\u672c\u4ee5\u4e3aN\u662f\u7528\u4f5c\u4fe1\u53f7\u91cf\u91cc\u9762\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u6765\u770b\u662f\u628a\u4ed6\u4f5c\u4e3a\u989d\u5916\u7684\u53d8\u91cf\u3002<br \/>\n\u4e3a\u4ec0\u4e48N\u4e0d\u80fd\u505a\u4fe1\u53f7\u91cf\uff1f\u540e\u6765\u60f3\u4e86\u4e00\u4e0b..\u8fd9\u4e2a\u4e1c\u897f\u662f\u5171\u4eab\u7684\u8d44\u6e90&#8230;\u660e\u663e\u4e0d\u80fd\u62ff\u6765\u505a\u4fe1\u53f7\u91cf\u7684\u5427\u3002<br \/>\n\u6587\u4e2d3\u4e2a\u4fe1\u53f7\u91cf<\/p>\n<pre>Semaphore barberReady = 0\nSemaphore accessWRSeats = 1     # if 1, the # of seats in the waiting room can be incremented or decremented\nSemaphore custReady = 0         # the number of customers currently in the waiting room, ready to be served<\/pre>\n<p>\u5176\u4e2dbarberReady\u662f\u7406\u53d1\u5e08\u5c31\u7eea\u7684\u4fe1\u53f7\u91cf\uff0cbarber\u8bb2\u5ba2\u4eba\u8bf7\u5165\u7406\u53d1\u5ba4\u540e\uff0csignal\u4e00\u4e0b\uff0c\u7136\u540e\u793a\u610f\u5ba2\u6237\u53ef\u4ee5\u5f00\u59cb\u7406\u53d1\u4e86\uff0c\u5ba2\u6237\u90a3\u8fb9wait\u8fd9\u4e2a\u4fe1\u53f7\u91cf\uff0c\u7b49\u5230\u4e86\u5c31\u5f00\u59cb\u7406\u53d1\u8fc7\u7a0b\u3002\u8fd9\u4e2a\u662f\u4e00\u4e2a\u540c\u6b65\u7684\u5173\u7cfb\uff0c\u56e0\u4e3a\u80af\u5b9a\u662f\u8981\u7406\u53d1\u5e08\u5148\u51c6\u5907\u597d\uff0c\u7136\u540e\u5ba2\u4eba\u624d\u53ef\u4ee5\u5f00\u59cb\u7406\u53d1\uff0c\u8fd9\u4e24\u4e2a\u52a8\u4f5c\u6709\u987a\u5e8f\u8981\u6c42\uff0c\u6240\u4ee5\u4fe1\u53f7\u91cf\u521d\u503c\u8bbe\u7f6e\u62100.<br \/>\n\u7b2c\u4e8c\u4e2aaccessWRSeats\u662f\u7528\u6765\u9501\u5ea7\u6905\u6570\u76ee\u6539\u53d8\u7684\uff0c\u9501\u4f4f\uff08=0\uff09\u7684\u65f6\u5019\uff0c\u8bf4\u660ewaitingroom\u5185\u6709\u4eba\u5458\u53d8\u52a8\u4e2d\uff0c\u8981\u963b\u585e\u5176\u4ed6\u4eba\u5bf9\u4eba\u5458\u53d8\u52a8\u7684\u5c1d\u8bd5\u3002\u663e\u7136\u8fd9\u662f\u4e00\u4e2a\u4e92\u65a5\u91cf\uff08mutex\uff09\uff0c\u5f53\u5ba2\u4eba\u8fdb\u5165\u7406\u53d1\u5ba4\u51c6\u5907\u7406\u53d1\u65f6\u3001\u6216\u8005\u7406\u53d1\u5e97\u5916\u6709\u4eba\u8fdb\u5165\u65f6\uff0c\u90fd\u8981\u6539\u53d8numberOfFreeWRSeats\u8fd9\u4e2a\u91cf\uff0c\u8fd9\u79cd\u64cd\u4f5c\u663e\u7136\u662f\u72ec\u5360\u7684\uff0c\u540c\u65f6\u53ea\u80fd\u6709\u4e00\u65b9\u6765\u4fee\u6539\u8fd9\u4e2a\u503c\uff0c\u6240\u4ee5\u5f15\u5165\u6b64\u4fe1\u53f7\u91cf\u534f\u8c03\u3002<br \/>\n\u7b2c\u4e09\u4e2a\u561b..\u5ba2\u4eba\u5c31\u7eea\u7684\u4fe1\u53f7\u91cf\uff0c\u4e3b\u8981\u539f\u56e0\u662fbarber\u7a7a\u95f2\u4e0b\u6765\u7684\u65f6\u5019\u9700\u8981\u7761\u5927\u89c9\u3002\u8fd9\u4e2a\u4e0e\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u91cf\u4e00\u6837\uff0c\u662f\u4e00\u4e2a\u540c\u6b65\u7684\u95ee\u9898\u3002\u5373\u5ba2\u4eba\u5148\u8981\u51c6\u5907\u597d\u4e86\uff0c\u624d\u80fd\u628a\u7406\u53d1\u5e08\u5524\u9192\u505a\u4e8b\u60c5\uff0c\u4e0d\u7136\u5c31\u522b\u5435\u5230\u4ed6\u3002<br \/>\n\u4e09\u4e2a\u4fe1\u53f7\u91cf\u7406\u89e3\u4e86\uff0c\u540e\u9762\u7684\u8fc7\u7a0b\u4e5f\u5c31\u4e00\u76ee\u4e86\u7136\u4e86\u3002\u4e0d\u77e5\u9053\u6211\u8bf4\u7684\u5bf9\u4e0d\u5bf9&#8230;<br \/>\n\u8fd9\u4e2a\u95ee\u9898\u5e94\u8be5\u6709\u53e6\u4e00(\u6216\u591a)\u79cd\u89e3\u6cd5\uff0c\u53e6\u4e00\u79cd\u89e3\u6cd5\u662f\u6307\u4f1a\u5bfc\u81f4barber\u7684starvation\u7684\u89e3\u6cd5\uff0c\u6216\u8005\u662f\u6743\u8861\u4e24\u8005\u6298\u4e2d\u7684\u529e\u6cd5\uff0c\u8fd9\u4e2a\u6ca1\u6709\u591a\u60f3\u8fc7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u505a\u64cd\u4f5c\u7cfb\u7edf\u4f5c\u4e1a\u78b0\u5230\u7684\u95ee\u9898\uff0c\u4e00\u65f6\u60f3\u4e0d\u51fa\u600e\u4e48\u505a\uff0c\u540e\u6765Google\u4e86\u4e00\u4e0b\u53d1\u73b0\u662f\u4e2a\u7ecf\u5178\u95ee\u9898\u3002 ========================================================================= \u4ee5\u4e0b\u82f1\u6587\u5185\u5bb9\u8f6c\u8f7d\u81ea\uff1ahttp:\/\/www.answers.com\/topic\/sleeping-barber-problem \u672c\u4eba\u7684\u7406\u89e3\u7ee7\u7eed\u5f80\u4e0b\u770b\u3002 In\u00a0computer science, the\u00a0sleeping barber problem\u00a0is a classic\u00a0inter-process communication\u00a0and\u00a0synchronization\u00a0problem between multiple\u00a0operating system\u00a0processes. The problem is analogous to that of keeping a barber working when there are customers, resting when there are none and doing so in an orderly manner. The analogy is based upon a hypothetical barber shop with one barber. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-study"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=183"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/183\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}