下载类网站模板,珠海网站设计报价,网页开发入门,网站建设的基础资料看到你的问题图像后#xff0c;输入信号量的目的是只允许单个进程/线程等待锁定#xff0c;如果你不使用它#xff0c;其他进程将进入等待队列 .why we need the entry semaphore条目信号量未使用任何值初始化#xff0c;如果它是全局声明的#xff0c;则它将初始化为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 countsemaphore db 1; // Controls access to the databaseint reader_count; // The number of reading processes accessing the dataReader(){while (TRUE) { // loop foreverdown(mutex); // gain access to reader_countreader_count reader_count 1; // increment the reader_countif (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 processup(mutex); // allow other processes to access reader_countread_db(); // read the databasedown(mutex); // gain access to reader_countreader_count reader_count - 1; // decrement reader_countif (reader_count 0)up(db); // if there are no more processes reading from the// database, allow writing process to access the dataup(mutex); // allow other processes to access reader_countuse_data();// use the data read from the database (non-critical)}Writer(){while (TRUE) { // loop forevercreate_data(); // create data to enter into database (non-critical)down(db); // gain access to the databasewrite_db(); // write information to the databaseup(db); // release exclusive access to the database}