0

我正在研究在昏昏欲睡的理发师问题中使用信号量值。我在想象这样一个场景,其中一位顾客已经和理发师在一起,然后其他 4 位顾客进入了理发店。候车室可容纳三个座位。我想知道理发师已经被占用的这种情况下的信号量值。

我知道,当理发师开店时顾客第一次进来时,我最终得到了这些信号量值:

barber = 0
customer = 0
mutex = 1

这是我解决这个问题的伪代码:

/* Counting semaphores - the integer value represents the initial count for the semaphores */

Semaphore customer = 0; /* Number of customer waiting for service */
Semaphore barber = 0; /* Number of barber waiting for students */
Semaphore mutex = 1; /* Mutual exclusion when accessing the waiting room */

int waiting = 0; /* Students waiting for turn with professor */

Barber() {
  while (TRUE) {
    wait (customer); /* Go to sleep if no customers */
    wait (mutex); /* Get access to waiting room */
    waiting--; /* Decrement number of waiting customers */
    signal (barber); /* Barber is ready */
    signal (mutex); /* Releasing waiting room */
    #GiveHaircut;
  }
}

Customer() {
  wait (mutex); /* Enter critical section, which is the waiting room */
  if (waiting < 3) { /* If there are free chairs in the waiting room */
    waiting++;
    signal (customer); /* Wake up barber if necessary */
    signal (mutex); /* Release access to count of waiting customers */
    wait (barber); /* Wait for barber if not available */
    #GetHaircut;
  } else {
    signal (mutex); /* Waiting area is full, leave without waiting */
  }
}

当我试图追踪这段代码并且理发师已经被占用时,我不断得到barber = -1.

我不确定这是否可以作为一个值,我只是感到非常困惑,如果有人可以帮助我跟踪伪代码以在这种情况下找到信号量值,我将不胜感激?我只看到过客户进来的例子,但没有看到其他在线场景。谢谢你。

4

1 回答 1

0

我不确定你的场景中是否有一个或多个理发师,所以我会给你两个案例的简短伪代码草稿。在这两种情况下,我们都假设您有一个 3 座等候室 - 这意味着您有一个最大 val 为 3 的信号量,当客户进来时,他们等待一个空闲位置,一旦轮到他们理发,他们就会发出信号现在有一个免费座位。对于席位“资源”,我们将有一个称为席位可用的信号量。Barber 唯一“担心”的是他有客户,因此我们将为他引入一个名为customersAvailable 的信号量。

因此,当您有一个理发师时,所有客户都在使用与您的单个理发师相同的“资源”——因此您需要一个互斥信号量,我们将其称为 barberMutex。

一个理发师场景

Semaphore barberMutex = 1
Semaphore customersAvailable = 0
Semaphore seatsAvailable = 3

Barber:
wait(customersAvailable) // wait for customers to show up
# doHaircut...
signal(barberMutex) // after one customer has been served, barber is free

Customer:
wait(seatsAvailable) // wait for an available seat
signal(customersAvailable) // once you've sat signal to you barber that there's customers waiting
wait(barberMutex) // wait if the barber is not free
signal(seatsAvailable) // once it's your turn, you go to barber and there is an open seat
# getHaircut

多个理发师场景 对于这个场景,我们有多个理发师,所以我们不会有一个互斥体,而是一个 val 为 n 的信号量,其中 n 是在该商店工作的理发师数量(假设我们的示例为 3,并将该信号量称为 barbersAvailable)。

Semaphore barbersAvailable = 3
Semaphore customersAvailable = 0
Semaphore seatsAvailable = 3

Barber:
wait(customersAvailable)
#doHaircut
*signal(barbersAvailable)*

Customer:
wait(seatsAvailable) // wait for an available seat
signal(customersAvailable) // once you've sat signal to you barber that there's customers waiting
*wait(barberAvailable)* // wait for one of the barbers to be free
signal(seatsAvailable) // once it's your turn, you go to barber and there is an open seat
# getHaircut

我总是假设wait busy 等待资源,并且在获得它后会降低该信号量的 val,同时信号也会增加信号量的 val。

于 2019-11-01T04:52:20.370 回答