2025年readers-writers problems读者-作者问题

readers-writers problems读者-作者问题在计算机科学中 第一和第二读写器问题是并发中常见计算问题的示例 这两个问题处理的情况是许多线程必须同时访问同一个共享内存 一些读和一些写 自然约束是没有进程可以访问共享进行读或写 而另一个进程正在写入 它 特别是 允许两个或多个读者同时访问共享

大家好,我是讯享网,很高兴认识大家。

在计算机科学中,第一和第二读写器问题是并发中常见计算问题的示例。 这两个问题处理的情况是许多线程必须同时访问同一个共享内存,一些读和一些写,自然约束是没有进程可以访问共享进行读或写,而另一个进程正在写入 它。 (特别是,允许两个或多个读者同时访问共享。)读者-作者锁是一种解决一个或多个读者-作者问题的数据结构。

In computer science, the first and second readers-writers problems are examples of a common computing problem in concurrency. The two problems deal with situations in which many threadsmust access the same shared memory at one time, some reading and some writing, with the natural constraint that no process may access the share for reading or writing while another process is in the act of writing to it. (In particular, it is allowed for two or more readers to access the share at the same time.) A readers-writer lock is a data structure that solves one or more of the readers-writers problems.

  [隐藏]

     1 第一读者-作者问题
     2 第二个读写器问题
     3 第三个读写器问题
     4 另见
     5 参考文献
     6 外部链接



Contents

 [hide] 

  • 1 The first readers-writers problem
  • 2 The second readers-writers problem
  • 3 The third readers-writers problem
  • 4 See also
  • 5 References
  • 6 External links

假设我们有一个具有上面详述的约束的共享内存区域。 可以在互斥互斥体后面保护共享数据,在这种情况下,没有两个线程可以同时访问数据。 但是,此解决方案不是**的,因为读取器 R1 可能拥有锁,然后另一个读取器 R2 请求访问。 R2 等到 R1 完成后再开始自己的读取操作是愚蠢的; 相反,R2 应该立即启动。 这就是第一个读者-作者问题的动机,其中添加了一个约束,即如果当前已打开共享以供阅读,则不应让任何读者等待。 这也称为读者偏好,其解决方案如下:

The first readers-writers problem[edit]

Suppose we have a shared memory area with the constraints detailed above. It is possible to protect the shared data behind a mutual exclusion mutex, in which case no two threads can access the data at the same time. However, this solution is suboptimal, because it is possible that a reader R1 might have the lock, and then another reader R2 requests access. It would be foolish for R2 to wait until R1was done before starting its own read operation; instead, R2 should start right away. This is the motivation for the first readers-writers problem, in which the constraint is added that no reader shall be kept waiting if the share is currently opened for reading. This is also called readers-preference, with its solution below:

semaphore wrt=1, mutex=1; readcount=0; writer() { wait(wrt); // Writing is done signal(wrt); } reader() { wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); // Do the Reading // (Critical Section Area) wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex); } 

讯享网

假设我们有一个受互斥锁保护的共享内存区域,如上所述。 这个解决方案是次优的,因为读取器 R1 可能拥有锁,写入器 W 正在等待锁,然后读取器 R2 请求访问。 R2 在 W 之前立即跳入是愚蠢的; 如果这种情况发生得足够频繁,W 就会饿死。 相反,W 应该尽快开始。 这是第二个读者-作者问题的动机,其中添加了约束,即一旦添加到队列中的作者,等待的时间不应超过绝对必要的时间。 这里,P() 用于等待,V() 用于信号。 这也称为作家偏好。

下面介绍了作者偏好场景的解决方案:


讯享网

The second readers-writers problem[edit]

Suppose we have a shared memory area protected by a mutex, as above. This solution is suboptimal, because it is possible that a reader R1 might have the lock, a writer W be waiting for the lock, and then a reader R2 requests access. It would be foolish for R2 to jump in immediately, ahead of W; if that happened often enough, W would starve. Instead, W should start as soon as possible. This is the motivation for the second readers-writers problem, in which the constraint is added that no writer, once added to the queue, shall be kept waiting longer than absolutely necessary. Here, P() is for wait and V() is for signal. This is also called writers-preference.

A solution to the writers-preference scenario is presented below:[1]

讯享网int readcount, writecount; (initial value = 0) semaphore mutex_1, mutex_2, mutex_3, w, r ; (initial value = 1) READER P(mutex_3); P(r); P(mutex_1); readcount := readcount + 1; if readcount = 1 then P(w); V(mutex_1); V(r); V(mutex_3); reading is performed P(mutex_1); readcount := readcount - 1; if readcount = 0 then V(w); V(mutex_1); WRITER P(mutex_2); writecount := writecount + 1; if writecount = 1 then P(r); V(mutex_2); P(w); writing is performed V(w); P(mutex_2); writecount := writecount - 1; if writecount = 0 then V(r); V(mutex_2); 

事实上,这两个问题陈述所暗示的解决方案都会导致饥饿——第一个读者-作者问题可能会使队列中的作者饿死,而第二个读者-作者问题可能会使读者饿死。 因此,有时会提出第三读写器问题,增加了不允许线程饿死的约束; 也就是说,获取共享数据锁的操作总是会在有限的时间内终止。

对读者和作者都公平的解决方案可能如下:

The third readers-writers problem[edit]

In fact, the solutions implied by both problem statements result in starvation — the first readers-writers problem may starve writers in the queue, and the second readers-writers problem may starve readers. Therefore, the third readers-writers problem is sometimes proposed, which adds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time.

A solution with fairness for both readers and writers might be as follows:

 semaphores: no_waiting, no_accessing, counter_mutex ( initial value is 1 ) shared variables: nreaders ( initial value is 0 ) local variables: prev, current WRITER: P( no_waiting ); P( no_accessing ); V( no_waiting ); ... write ... V( no_accessing ); READER: P( no_waiting ); P( counter_mutex ); prev := nreaders; nreaders := nreaders + 1; V( counter_mutex ); if prev = 0 then P( no_accessing ); V( no_waiting ); ... read ... P( counter_mutex ); nreaders := nreaders - 1; current := nreaders; V( counter_mutex ); if current = 0 then V( no_accessing ); 

请注意,受 counter_mutex 保护的部分可以由合适的 fetch-and-addatomic 指令替换,从而在阅读器代码中节省两个潜在的上下文切换。

还要注意,当且仅当信号量在阻塞和释放线程时保持先进先出顺序时,此解决方案才能满足“不允许线程饿死”的条件。 否则,例如,一个被阻塞的写入器可能会无限期地保持阻塞状态,而其他写入器在它可以之前递减信号量的循环。

Note that sections protected by counter_mutex could be replaced by a suitable fetch-and-addatomic instruction, saving two potential context switches in reader's code.

Note also that this solution can only satisfy the condition that "no thread shall be allowed to starve" if and only if semaphores preserve first-in first-out ordering when blocking and releasing threads. Otherwise, a blocked writer, for example, may remain blocked indefinitely with a cycle of other writers decrementing the semaphore before it can.



See also[edit]

  • Producers-consumers problem
  • Dining philosophers problem
  • Cigarette smokers problem
  • Sleeping barber problem
  • Read/write lock pattern



References[edit]

[2] [3] [4]

  1. Jump up ^ Communications of the ACM :Concurrent Control with "Readers" and "Writers" P.J. Courtois,* F. H, 1971 [1]
  2. Jump up ^ Morris JM (1979) A starvation-free solution to the mutual exclusion problem. Inf Process Lett 8:76–80
  3. Jump up ^ Fair Solution to the Reader-Writer-Problem with Semaphores only. H. Ballhausen, 2003 [2]
  4. Jump up ^ Faster Fair Solution for the Reader-Writer Problem. V.Popov, O.Mazonka 2013 [3]
小讯
上一篇 2025-01-14 15:09
下一篇 2025-03-03 20:44

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/100804.html