【问题标题】:Semaphores in Sleeping Professor Problem code tracing?Sleeping Professor 问题代码跟踪中的信号量?
【发布时间】:2019-10-31 17:55:31
【问题描述】:

我正在解决沉睡教授场景中的一些信号量问题。它使用计数信号量模型。这些是基本准则:

  1. 候诊室(临界区)有 3 把椅子。教授办公室有 1 把椅子,一次可容纳一名学生。
  2. 如果办公室里没有学生,教授就会睡着。
  3. 当学生到达时,教授会醒来。如果教授睡着了,学生就会叫醒教授。
  4. 如果学生来到等候区并且所有椅子都被占用,则学生离开。
  5. 如果教授正忙于处理其他学生,但有可用的等候空间,则该学生将在其中一个空闲椅子上等候。

现在,这是我为沉睡教授问题编写的伪代码:

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

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

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

Professor() {
  while (office hours) {
    wait (students); /* Go to sleep if no students */
    wait (mutex); /* Get access to waiting room */
    waiting = waiting - 1; /* Decrement number of waiting students */
    signal (professor); /* One professor is ready */
    signal (mutex); /* Releasing waiting room */
    ConsultWithStudent();
  }
}

Student() {
  wait (mutex); /* Enter critical section, which is the waiting room */
  if (waiting < 3) { /* If there are free chairs in the waiting room */
    waiting = waiting + 1;
    signal (students); /* Wake up professor is necessary */
    signal (mutex); /* Release access to count of waiting students */
    wait (professor); /* Wait for professor if not available */
    GetHelpFromProfessor();
  } else {
    signal (mutex); /* Waiting area is full, leave without waiting */
  }
}

我已经解决了几个代码跟踪问题,并且想知道我的信号量计数是否适合每个问题:

场景 1: 假设教授在她的办公时间到达,然后一个学生到达。假设教授目前正在咨询这个学生。信号量计数如下:

信号量 计数

学生 1

互斥锁 0

教授1

场景 2: 当教授与第一个学生交谈时,又有 4 名学生来了。显示信号量的结果计数:

信号量 计数

学生 1

互斥体 3

教授1

场景 3: 教授结束与第一个学生的谈话,那个学生离开了。教授开始与第二个学生交谈。在教授与第二个学生交谈时显示信号量的结果计数:

信号量 计数

学生 1

互斥体 2

教授1

有人可以帮我检查我的工作吗?是的,这是为了家庭作业,我在自己从一个类似的理发师问题中生成伪代码 (https://www.geeksforgeeks.org/sleeping-barber-problem-in-process-synchronization/) 后试图理解信号量。

【问题讨论】:

    标签: java c process synchronization semaphore


    【解决方案1】:

    信号量为任意数量的许可提供了一种结构。我认为您不需要混合使用第三个信号量“互斥体”——只需一个用于走廊的椅子,一个用于办公室的椅子。

    为了专注于候诊室的三把椅子,我会设置一个带有三个许可证的信号量;像这样在 Java 中:

    Semaphore chairsInWaitingRoom = new Semaphore(3);
    

    就其本身而言,在任何人陷入等待之前,允许对acquire() 进行三个不同的调用,因此该行可以运行三次而不会阻塞:

    chairsInWaitingRoom.acquire();
    

    但是,当学生出现时,您可以调用tryAcquire(),而不是调用acquire()——这将返回真/假,如果尝试获得许可成功与否。如果来电者收到true,那么他们“在办公室外找了一把椅子”。如果来电者收到false,则表示没有可用的椅子(这可能包括另一个学生刚刚将他们击败到最后一把椅子的竞争条件)。

    boolean success = chairsInWaitingRoom.tryAcquire();
    if (success) {
        // student was able to safely get a chair
    } else {
        // no more chairs were available; student leaves
    }
    

    在某个时候,办公室内的一把椅子将可用,因此学生需要首先获得那把新椅子,然后释放他们的“走廊”椅子;可能看起来像这样:

    Semaphore chairsInWaitingRoom = new Semaphore(3);
    Semaphore chairInOffice = new Semaphore(1);
    
    boolean success = chairsInWaitingRoom.tryAcquire();
    if (success) {
        // student was able to safely get a chair; now wait for the main office chair
        chairInOffice.acquire();        // first get the new office seat, block here
        chairsInWaitingRoom.release();  // then give up your hallway chair
    } else {
        // no more chairs were available; student leaves
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2016-11-04
      • 2020-12-25
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多