首页 文章

使用二进制信号量实现通用信号量

提问于
浏览
-1

使用二进制信号量实现的gen信号量:

click image please, gen semaphore implemented using binary semaphores

所以我无法理解为什么我们需要输入信号量,没有它我可以看到它是如何正常工作的 . 多个流程如何进入关键部分?在第一个进程进入后,它会等待(互斥),这意味着没有其他人可以进入,此外还有其他进程在等待信号(互斥)

一般信号量可以允许多个进程进入临界区域,但我无法看到在此代码中如何完成 .

2 回答

  • 0

    看到你的问题图像后,输入信号量的目的是只允许单个进程/线程等待锁定,如果你不使用它,其他进程将进入等待队列 .

    why we need the entry semaphore

    • 条目信号量未使用任何值初始化,如果它是全局声明的,则它将初始化为0.因此,如果条目信号量为0,则wait(条目)将仅允许单个进程进入,因为wait()功能检查条目值是否小于零,然后进程将进入等待队列 .

    How can multiple processes enter the critical section?

    • 一次只有一个过程可以在关键部分 - 否则关键部分是什么?

    • Critical Section是访问共享变量的代码段,必须作为原子操作执行 . 这意味着在一组协作过程中,在给定的时间点,只有一个过程必须执行其关键部分 . 如果任何其他进程也想要执行其关键部分,则必须等到第一个进程完成 .

    A general semaphore can allow multiple processes to enter the critical section area but I cannot see how that is done in this code.

    这是不对的,如果您允许多个进程到关键部分谁想要修改共享数据,那么您可以更改关键部分的平均值 . 您将在流程结束时收到错误的数据 .

    如果进程只读取共享数据,则可以使用常规信号量允许多个进程访问关键数据,不要修改共享数据 .

    我有非常小的代码供您展示信号量如何工作以及多个进程如何允许访问共享数据 . 你可以把它作为多个读者和作家 .

    semaphore mutex = 1;                 // Controls access to the reader count
    semaphore db = 1;                    // Controls access to the database
    int reader_count;                    // The number of reading processes accessing the data
    
    Reader()
    {
      while (TRUE) {                     // loop forever
         down(&mutex);                          // gain access to reader_count
         reader_count = reader_count + 1;       // increment the reader_count
         if (reader_count == 1)
             down(&db);                         // if this is the first process to read the database,
                                                // a down on db is executed to prevent access to the 
                                                // database by a writing process
         up(&mutex);                            // allow other processes to access reader_count
         read_db();                             // read the database
         down(&mutex);                          // gain access to reader_count
         reader_count = reader_count - 1;       // decrement reader_count
         if (reader_count == 0)
             up(&db);                           // if there are no more processes reading from the 
                                                // database, allow writing process to access the data
         up(&mutex);                            // allow other processes to access reader_countuse_data();
                                                // use the data read from the database (non-critical)
    }
    
    Writer()
    {
      while (TRUE) {                     // loop forever
         create_data();                         // create data to enter into database (non-critical)
         down(&db);                             // gain access to the database
         write_db();                            // write information to the database
         up(&db);                               // release exclusive access to the database
    }
    
  • 0

    多个流程如何进入关键部分?在第一个进程进入后,它会等待(互斥),这意味着没有其他人可以进入...一般信号量可以允许多个进程进入临界区域

    你在这里谈到一个关键部分有点不幸,因为critical section通常被理解为程序的一部分,一次不能由多个进程执行 . 你实际上的意思可能是一般(计数)信号量可以允许多个进程共享(池)资源 . 您提供的实现确实能够执行此操作,因为:在第一个进程进入并等待(互斥)后,它立即继续,因为 mutex 已初始化为 1 . 然后,如果递减的 c 不小于0,它会 signal(mutex) ,所以另一个进程可以进入 .

相关问题