The Little Book of Semaphores 信号量小书 第四章 经典同步问题 4.2 读者-写者问题

第四章 经典同步问题

 

4.2 读者 - 写者问题

下一个经典问题,称为Reader-Writer问题(读者-作家问题/读者-写者问题),适用于并发线程读取和修改数据结构,数据库或文件系统的任何情况。 在写入或修改数据结构时,通常需要禁止其他线程读取,以防止读者线程中断正在进行的修改并读取不一致或无效的数据。

与生产者 - 消费者问题一样,解决方案是不对称的。 读者和写者在进入关键部分之前执行不同的代码。 同步约束是:

  1. 任何数量的读者可以同时进入关键部分。
  2. 写者必须拥有对关键部分的独占访问权。

换句话说,当任何其他线程(读者或者写者)在那里时,写者不能进入临界区;而当写者在那里时,其他线程都不可以进入。

这里的排除模式可能被称为分类互斥(categorical mutual exclusion)。 临界区中的线程不一定排除其他线程,但是在临界区中存在一个类别会排除其他类别。

思考:使用信号量来强制执行这些约束,同时允许读者和写者访问数据结构,并避免死锁的可能性。

 

4.2.1 读者 - 写者问题提示

这是一组足以解决问题的变量。

readers计数器记录房间里有多少读者。 互斥锁mutex保护共享的计数器。

如果临界区中没有线程(读者或编写者),则roomEmpty为1,否则为0。 这演示了我用于表示条件的信号量的命名约定。 在这种惯例中,“等待(wait)”通常意味着“等待条件为真”,“信号(signal)”意味着“信号条件为真”。

 

4.2.2 读者 - 写者问题方案

写者的代码很简单。 如果临界区为空,则写者可以进入,但进入后具有排除所有其他线程的效果:

当写者退出时,可以确定房间现在是空的吗? 是的,因为它知道在那里没有其他线程可以进入。

读者代码类似于我们在上一节中看到的屏障代码。 我们会跟踪房间内的读者人数,以便我们可以给第一个到达的和最后一个离开的线程一个特殊的任务。

到达的第一个读者必须等待roomEmpty。 如果房间是空的,那么读者继续执行,同时禁止写者。 后续读者仍然可以进入,因为他们都不会试图在roomEmpty上等待。

如果已经有一个写者在房间里,这时有读者到了,它要等待房间变为空。 由于它拥有互斥锁,因此任何后续读者都会在互斥锁上排队。

临界区之后的代码类似。 最后一个离开房间的读者把灯都关掉了-- 也就是说,它标志着roomEmpty,可能让等待的写者进入。

同样,为了证明此代码是正确的,断言和演示关于程序必须如何表现的许多声明是有用的。 你能说服自己以下是真的吗?

  • 只有一个读者可以排队等待roomEmpty,但有几个写者可能排队等候。
  • 当读者发出信号roomEmpty时,房间必须为空。

与此读者代码类似的模式很常见:进入某个部分的第一个线程锁定信号量(或队列),最后一个线程解锁它。 事实上,它是如此常见,我们应该给它一个名称,并将其包装在一个对象中。

模式的名称是Lightswitch,类似于进入房间的第一个人打开灯(锁定互斥锁)的模式,最后一个人将其关闭(解锁互斥锁)。 这是Lightswitch的类定义:

lock接受一个参数,一个它将检查并可能保持的信号量。 如果信号量被锁定,则调用线程在信号量上阻塞,所有后续线程在self.mutex上阻塞。 当信号量被解锁时,第一个等待线程再次锁定它并且所有等待的线程继续。

如果信号量的初始状态是解锁的,则第一个线程将其锁定并且所有后续线程可以继续。

直到每个调用锁的线程都调用unlock时,unlock才会生效。 当最后一个线程调用unlock时,它会解锁信号量。

使用这些函数,我们可以更简单地重写读者代码:

readLightswitch是一个共享的Lightswitch对象,它的初始计数值为零。

作者的代码没有变化。

还可以将对roomEmpty的引用存储为Lightswitch的属性,而不是将其作为参数传递给锁定和解锁。 这种替代方案不太容易出错,但我认为如果锁定和解锁的每次调用都指定了它运行的信号量,它就会提高可读性。

 

4.2.3 饿死

在之前的解决方案中,是否存在死锁的危险?为了发生死锁,线程必须能够在保持另一个信号量的同时等待信号量,从而阻止自身收到信号。

在这个例子中,死锁是不可能的,但是有一个相关的问题几乎同样糟糕:作者可能会饿死。

如果写者到达的时候,临界区中有读者,那么当读者来来往往时,它可能会在队列中等待。只要新读者在最后一位读者离开之前到达,房间里总会有至少一位读者。

这种情况不是僵局,因为一些线程正在取得进展,但这并不是完全可取的。只要系统上的负载很低,这样的程序就可以工作,因为这样写者就有很多机会。但随着负载的增加,系统的行为会迅速恶化(至少从写者的角度来看)。

思考:扩展此解决方案,以便在写者到达时,现有读者可以完成,但不能再进入其他读者。

 

4.2.4 不会饿死的读者 - 写者问题提示

这是一个提示。 您可以为读者添加一个旋转门,并允许写者锁定它。 写者必须通过相同的旋转门,但是当他们在旋转门内时,他们应该检查roomEmpty信号量。 如果写者卡在旋转门中,它会强迫读者在旋转门处排队。 然后,当最后一个读者离开临界区时,我们保证接下来至少有一个写者可以进入(在任何排队的读者进入之前)。

readSwitch记录房间里有多少读者; 当第一个读者进入时会锁定roomEmpty;并在最后一个读者退出时将其解锁。

turnstile对于读者来说是一个旋转门,对于写者来说是一个互斥体。

 

4.2.5 不会饿死的读者 - 写者问题方案

这是写者的代码:

如果写者到达时,房间里已有读者,它将阻塞在第2行,这意味着旋转门将被锁定。 这将阻止新的读者进入,因为已经有写者在排队了。 这是读者代码:

当最后一个读者离开时,它发出roomEmpty信号,解锁等待的写者。写者立即进入它的临界区,因为没有一个等待的读者可以通过旋转门。

当写者退出时,它会发出turnstile信号,它可以解锁等待的线程,这可能是读者或写者。 因此,这个解决方案保证至少有一个写者可以继续,但是当有写者在排队时,读者仍然有可能进入。

根据应用程序,为写者提供更高的优先级可能是个好主意。 例如,如果写者要对数据结构进行更新,而这种更新又是时序要求很严格的,则最好在写者有机会继续之前,将能查看旧数据的读者的数量控制到最小。

但是,一般情况下,由调度程序而不是程序员来选择要解除阻塞的等待线程。 某些调度程序使用先进先出队列,这意味着线程按其排队的相同顺序解除阻塞。 其他调度程序随机选择,或根据基于等待线程的属性的优先级方案进行选择。

如果您的编程环境可以使某些线程优先于其他线程,那么这是解决此问题的简单方法。 如果没有,你将不得不寻找另一种方式。

思考:为读者编写一个优先考虑写者的问题的解决方案。 也就是说,一旦写者到来,在所有写者离开系统之前,不允许读者进入。

 

4.2.6 写者优先的读者 - 写者问题提示

像往常一样,提示是解决方案中使用的变量形式。

 

4.2.7 写者优先的读者 - 写者问题方案

这是读者的代码:

如果一个读者在临界区,它拥有noWriters,但它不包含noReaders。 因此,如果写者到达它可以锁定noReaders,这将导致后续读者排队。

当最后一个读者退出时,它会发出noWriters信号,允许任何排队的写者继续运行。

写者的代码:

当写者在临界区时,它同时拥有noReaders和noWriters。 这具有(相对明显的)效果,即确保在临界区没有读者并且也没有其他写者。 此外,writeSwitch具有允许多个写者在noWriters上排队的(不太明显的)效果,但是当它们存在时保持noReaders被锁定。 因此,许多写者可以通过临界区而无需发出noReaders信号。 只有当最后一位写者退出时,读者才能进入。

当然,这种解决方案的一个缺点是,现在读者可能会饿死(或至少面临长时间的延迟)。对于某些应用程序来说,最好的办法是获得具有可预测周转时间的往期的数据。

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
例子讲述Semaphore的应用。 1 Introduction 1 1.1 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Execution model . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 Serialization with messages . . . . . . . . . . . . . . . . . . . . . 3 1.4 Non-determinism . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 Shared variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5.1 Concurrent writes . . . . . . . . . . . . . . . . . . . . . . 4 1.5.2 Concurrent updates . . . . . . . . . . . . . . . . . . . . . 5 1.5.3 Mutual exclusion with messages . . . . . . . . . . . . . . 6 2 Semaphores 7 2.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.3 Why semaphores? . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 Basic synchronization patterns 11 3.1 Signaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2 Rendezvous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.2.1 Rendezvous hint . . . . . . . . . . . . . . . . . . . . . . . 13 3.2.2 Rendezvous solution . . . . . . . . . . . . . . . . . . . . . 15 3.2.3 Deadlock #1 . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.3 Mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.3.1 Mutual exclusion hint . . . . . . . . . . . . . . . . . . . . 17 3.3.2 Mutual exclusion solution . . . . . . . . . . . . . . . . . . 19 3.4 Multiplex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.4.1 Multiplex solution . . . . . . . . . . . . . . . . . . . . . . 21 3.5 Barrier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.5.1 Barrier hint . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.5.2 Barrier non-solution . . . . . . . . . . . . . . . . . . . . . 25 3.5.3 Deadlock #2 . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.5.4 Barrier solution . . . . . . . . . . . . . . . . . . . . . . . . 29 3.5.5 Deadlock #3 . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.6 Reusable barrier . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.6.1 Reusable barrier non-solution #1 . . . . . . . . . . . . . . 33 3.6.2 Reusable barrier problem #1 . . . . . . . . . . . . . . . . 35 3.6.3 Reusable barrier non-solution #2 . . . . . . . . . . . . . . 37 3.6.4 Reusable barrier hint . . . . . . . . . . . . . . . . . . . . . 39 3.6.5 Reusable barrier solution . . . . . . . . . . . . . . . . . . 41 3.6.6 Preloaded turnstile . . . . . . . . . . . . . . . . . . . . . . 43 3.6.7 Barrier objects . . . . . . . . . . . . . . . . . . . . . . . . 44 3.7 Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.7.1 Queue hint . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.7.2 Queue solution . . . . . . . . . . . . . . . . . . . . . . . . 49 3.7.3 Exclusive queue hint . . . . . . . . . . . . . . . . . . . . . 51 3.7.4 Exclusive queue solution . . . . . . . . . . . . . . . . . . . 53 3.8 Fifo queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.8.1 Fifo queue hint . . . . . . . . . . . . . . . . . . . . . . . . 57 3.8.2 Fifo queue solution . . . . . . . . . . . . . . . . . . . . . . 59 4 Classical synchronization problems 61 4.1 Producer-consumer problem . . . . . . . . . . . . . . . . . . . . . 61 4.1.1 Producer-consumer hint . . . . . . . . . . . . . . . . . . . 63 4.1.2 Producer-consumer solution . . . . . . . . . . . . . . . . . 65 4.1.3 Deadlock #4 . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.1.4 Producer-consumer with a finite buffer . . . . . . . . . . . 67 4.1.5 Finite buffer producer-consumer hint . . . . . . . . . . . . 69 4.1.6 Finite buffer producer-consumer solution . . . . . . . . . 71 4.2 Readers-writers problem . . . . . . . . . . . . . . . . . . . . . . . 71 4.2.1 Readers-writers hint . . . . . . . . . . . . . . . . . . . . . 73 4.2.2 Readers-writers solution . . . . . . . . . . . . . . . . . . . 75 4.2.3 Starvation . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.2.4 No-starve readers-writers hint . . . . . . . . . . . . . . . . 79 4.2.5 No-starve readers-writers solution . . . . . . . . . . . . . 81 4.2.6 Writer-priority readers-writers hint . . . . . . . . . . . . . 83 4.2.7 Writer-priority readers-writers solution . . . . . . . . . . . 85 4.3 No-starve mutex . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4.3.1 No-starve mutex hint . . . . . . . . . . . . . . . . . . . . 89 4.3.2 No-starve mutex solution . . . . . . . . . . . . . . . . . . 91 4.4 Dining philosophers . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.4.1 Deadlock #5 . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.4.2 Dining philosophers hint #1 . . . . . . . . . . . . . . . . . 97 4.4.3 Dining philosophers solution #1 . . . . . . . . . . . . . . 99 4.4.4 Dining philosopher¡¯s solution #2 . . . . . . . . . . . . . . 101 4.4.5 Tanenbaum¡¯s solution . . . . . . . . . . . . . . . . . . . . 103 4.4.6 Starving Tanenbaums . . . . . . . . . . . . . . . . . . . . 105 4.5 Cigarette smokers problem . . . . . . . . . . . . . . . . . . . . . . 107 4.5.1 Deadlock #6 . . . . . . . . . . . . . . . . . . . . . . . . . 111 4.5.2 Smokers problem hint . . . . . . . . . . . . . . . . . . . . 113 CONTENTS vii 4.5.3 Smoker problem solution . . . . . . . . . . . . . . . . . . 115 4.5.4 Generalized Smokers Problem . . . . . . . . . . . . . . . . 115 4.5.5 Generalized Smokers Problem Hint . . . . . . . . . . . . . 117 4.5.6 Generalized Smokers Problem Solution . . . . . . . . . . . 119 5 Less classical synchronization problems 121 5.1 The dining savages problem . . . . . . . . . . . . . . . . . . . . . 121 5.1.1 Dining Savages hint . . . . . . . . . . . . . . . . . . . . . 123 5.1.2 Dining Savages solution . . . . . . . . . . . . . . . . . . . 125 5.2 The barbershop problem . . . . . . . . . . . . . . . . . . . . . . . 127 5.2.1 Barbershop hint . . . . . . . . . . . . . . . . . . . . . . . 129 5.2.2 Barbershop solution . . . . . . . . . . . . . . . . . . . . . 131 5.3 Hilzer¡¯s Barbershop problem . . . . . . . . . . . . . . . . . . . . . 133 5.3.1 Hilzer¡¯s barbershop hint . . . . . . . . . . . . . . . . . . . 134 5.3.2 Hilzer¡¯s barbershop solution . . . . . . . . . . . . . . . . . 135 5.4 The Santa Claus problem . . . . . . . . . . . . . . . . . . . . . . 137 5.4.1 Santa problem hint . . . . . . . . . . . . . . . . . . . . . . 139 5.4.2 Santa problem solution . . . . . . . . . . . . . . . . . . . 141 5.5 Building H2O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 5.5.1 H2O hint . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 5.5.2 H2O solution . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.6 River crossing problem . . . . . . . . . . . . . . . . . . . . . . . . 148 5.6.1 River crossing hint . . . . . . . . . . . . . . . . . . . . . . 149 5.6.2 River crossing solution . . . . . . . . . . . . . . . . . . . . 151 5.7 The roller coaster problem . . . . . . . . . . . . . . . . . . . . . . 153 5.7.1 Roller Coaster hint . . . . . . . . . . . . . . . . . . . . . . 155 5.7.2 Roller Coaster solution . . . . . . . . . . . . . . . . . . . . 157 5.7.3 Multi-car Roller Coaster problem . . . . . . . . . . . . . . 159 5.7.4 Multi-car Roller Coaster hint . . . . . . . . . . . . . . . . 161 5.7.5 Multi-car Roller Coaster solution . . . . . . . . . . . . . . 163 6 Not-so-classical problems 165 6.1 The search-insert-delete problem . . . . . . . . . . . . . . . . . . 165 6.1.1 Search-Insert-Delete hint . . . . . . . . . . . . . . . . . . 167 6.1.2 Search-Insert-Delete solution . . . . . . . . . . . . . . . . 169 6.2 The unisex bathroom problem . . . . . . . . . . . . . . . . . . . . 170 6.2.1 Unisex bathroom hint . . . . . . . . . . . . . . . . . . . . 171 6.2.2 Unisex bathroom solution . . . . . . . . . . . . . . . . . . 173 6.2.3 No-starve unisex bathroom problem . . . . . . . . . . . . 175 6.2.4 No-starve unisex bathroom solution . . . . . . . . . . . . 177 6.3 Baboon crossing problem . . . . . . . . . . . . . . . . . . . . . . 177 6.4 The Modus Hall Problem . . . . . . . . . . . . . . . . . . . . . . 178 6.4.1 Modus Hall problem hint . . . . . . . . . . . . . . . . . . 179 6.4.2 Modus Hall problem solution . . . . . . . . . . . . . . . . 181 viii CONTENTS 7 Not remotely classical problems 183 7.1 The sushi bar problem . . . . . . . . . . . . . . . . . . . . . . . . 183 7.1.1 Sushi bar hint . . . . . . . . . . . . . . . . . . . . . . . . . 185 7.1.2 Sushi bar non-solution . . . . . . . . . . . . . . . . . . . . 187 7.1.3 Sushi bar non-solution . . . . . . . . . . . . . . . . . . . . 189 7.1.4 Sushi bar solution #1 . . . . . . . . . . . . . . . . . . . . 191 7.1.5 Sushi bar solution #2 . . . . . . . . . . . . . . . . . . . . 193 7.2 The child care problem . . . . . . . . . . . . . . . . . . . . . . . . 194 7.2.1 Child care hint . . . . . . . . . . . . . . . . . . . . . . . . 195 7.2.2 Child care non-solution . . . . . . . . . . . . . . . . . . . 197 7.2.3 Child care solution . . . . . . . . . . . . . . . . . . . . . . 199 7.2.4 Extended child care problem . . . . . . . . . . . . . . . . 199 7.2.5 Extended child care hint . . . . . . . . . . . . . . . . . . . 201 7.2.6 Extended child care solution . . . . . . . . . . . . . . . . 203 7.3 The room party problem . . . . . . . . . . . . . . . . . . . . . . . 205 7.3.1 Room party hint . . . . . . . . . . . . . . . . . . . . . . . 207 7.3.2 Room party solution . . . . . . . . . . . . . . . . . . . . . 209 7.4 The Senate Bus problem . . . . . . . . . . . . . . . . . . . . . . . 211 7.4.1 Bus problem hint . . . . . . . . . . . . . . . . . . . . . . . 213 7.4.2 Bus problem solution #1 . . . . . . . . . . . . . . . . . . 215 7.4.3 Bus problem solution #2 . . . . . . . . . . . . . . . . . . 217 7.5 The Faneuil Hall problem . . . . . . . . . . . . . . . . . . . . . . 219 7.5.1 Faneuil Hall Problem Hint . . . . . . . . . . . . . . . . . . 221 7.5.2 Faneuil Hall problem solution . . . . . . . . . . . . . . . . 223 7.5.3 Extended Faneuil Hall Problem Hint . . . . . . . . . . . . 225 7.5.4 Extended Faneuil Hall problem solution . . . . . . . . . . 227 7.6 Dining Hall problem . . . . . . . . . . . . . . . . . . . . . . . . . 229 7.6.1 Dining Hall problem hint . . . . . . . . . . . . . . . . . . 231 7.6.2 Dining Hall problem solution . . . . . . . . . . . . . . . . 233 7.6.3 Extended Dining Hall problem . . . . . . . . . . . . . . . 234 7.6.4 Extended Dining Hall problem hint . . . . . . . . . . . . . 235 7.6.5 Extended Dining Hall problem solution . . . . . . . . . . 237 8 Synchronization in Python 239 8.1 Mutex checker problem . . . . . . . . . . . . . . . . . . . . . . . 240 8.1.1 Mutex checker hint . . . . . . . . . . . . . . . . . . . . . . 243 8.1.2 Mutex checker solution . . . . . . . . . . . . . . . . . . . 245 8.2 The coke machine problem . . . . . . . . . . . . . . . . . . . . . . 247 8.2.1 Coke machine hint . . . . . . . . . . . . . . . . . . . . . . 249 8.2.2 Coke machine solution . . . . . . . . . . . . . . . . . . . . 251 9 Synchronization in C 253 9.1 Mutual exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9.1.1 Parent code . . . . . . . . . . . . . . . . . . . . . . . . . . 254 9.1.2 Child code . . . . . . . . . . . . . . . . . . . . . . . . . . 254 9.1.3 Synchronization errors . . . . . . . . . . . . . . . . . . . . 255 CONTENTS ix 9.1.4 Mutual exclusion hint . . . . . . . . . . . . . . . . . . . . 257 9.1.5 Mutual exclusion solution . . . . . . . . . . . . . . . . . . 259 9.2 Make your own semaphores . . . . . . . . . . . . . . . . . . . . . 261 9.2.1 Semaphore implementation hint . . . . . . . . . . . . . . 263 9.2.2 Semaphore implementation . . . . . . . . . . . . . . . . . 265 9.2.3 Semaphore implementation detail . . . . . . . . . . . . . . 267 A Cleaning up Python threads 271 A.1 Semaphore methods . . . . . . . . . . . . . . . . . . . . . . . . . 271 A.2 Creating threads . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 A.3 Handling keyboard interrupts . . . . . . . . . . . . . . . . . . . . 272 B Cleaning up POSIX threads 275 B.1 Compiling Pthread code . . . . . . . . . . . . . . . . . . . . . . . 275 B.2 Creating threads . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 B.3 Joining threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 B.4 Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值