读者和写者(Courtois 1971)论文翻译

《现代操作系统》(第四版)96页读者写者问题。以下是书上提到的由Courtois(库尔图瓦)等人给出一种写者优先的解法的论文。引用部分为原文,下面是对应的译文

 

 

 

      Concurrent Control with “Readers” and “Writers”

 

 

正文

The problem of the mutual exclusion of several independent processes from simultaneous access to a“critical section” is discussed for the case where there are two distinct classes of processes known as “readers” and “writers.” The “readers” may share the section with each other, but the “writers” must have exclusive access. Two solutions are presented: one for the case where we wish minimum delay for the readers; the other for the case where we wish writing to take place as early as possible.

 针对存在两种不同类型的进程:“读者”和“写者”,讨论了几个独立进程同时访问“关键部分”的互斥问题。“读者”可以彼此共享该部分,但“写者”必须具有独占访问权。本文提出了两种解决方案:一种是希望读者能在最短时间内收到回复;另一种是写入操作尽早发生。

Dijkstra, Knuth, and de Bruijn have discussed the problem of guaranteeing exclusive access to a shared resource in a system of cooperating sequential processes. The problem they deal with has been shown to have a relatively simple solution using the “P” and “V” operations of Dijkstra. We discuss two related problems of practical significance in which we recognize two classes of processes wishing to use the resource. The processes of the first class, named writers, must have exclusive access as in the original problem, but processes of the second class, the readers, may share the resource with an unlimited number of other readers.

Dijkstra(迪杰斯特拉)、Knuth(克努特)和De Bruijn(德布鲁因)讨论了在一个协同顺序过程的系统中保证对共享资源的独占访问问题。他们所处理的问题已经证明,使用Dijkstra的“p”和“v”操作有一个相对简单的解决方案。我们讨论了具有现实意义的两个问题,我们在其中识别出两类希望使用资源的进程。第一类的进程,命名为writers(写者),必须具有独占访问权,但第二类的进程,reader(读者),可以与无限数量readers共享资源。

Problem 1 

We demand of our solution that no reader be kept waiting unless a writer has already obtained permission to use the resource; i.e. no reader should wait simply because a writer is waiting for other readers to finish. In this case the solution presented is quite simple, but our experience has shown that it is not easily arrived at. Numerous solutions, which have quite unreasonable complexity, have been proposed. The following solution resulted from several cycles among the authors in which each simplified a solution shown him by another. We present it in hope that others may be spared the effort of solving again this rather common problem. See Figure 1

我们的解决方案要求,除非writer已经获得使用资源的许可,否则不应让reader一直等待;也就是说,任何reader都不应仅仅因为writer在等待其他reader完成而等待。在这种情况下,所提出的解决方案相当简单,但我们的经验表明,这是不容易实现的。许多解决方案,具有相当不合理的复杂性。下面的解决方案来自于作者之间的几次循环,每次都更加简化。我们提出它是为了希望其他人可以省去再次解决这个相当常见的问题的努力。参见图1:

Please notice that w functions as a mutual exclusion semaphore for the writer but is only used by the first reader to enter the critical section and the last reader to leave it. It is ignored by readers who enter or leave while other readers are present. mutex ensures that only one reader will enter or leave at a time thereby eliminating the possibility of ambiguity about which process is responsible for adjusting w. w Will be positive if and only if there are no readers and no writers present in the critical section.

请注意,w对于writer来说是一个互斥信号量,但只是当第一个reader进入关键部分,最后一个reader离开关键部分时才被用到。当其他readers在场的时候,w被进入或离开的reader所忽略。互斥确保一次只有一个reader进入或离开,从而消除了由哪个进程负责调整w的可能性。 当且仅当关键部分中没有readers和writers在场时,w是正数。

Problem 2

Here we retain the requirement that Writers must have exclusive access while readers may share, but we add the requirement that once a writer is ready to write, he performs his “write” as soon as possible. A solution to this problem cannot be a solution to Problem I because to meet this requirement a reader Who arrives after a writer has announced that he is ready to write must wait even if the writer is also waiting. For the first problem it was possible that a writer could wait indefinitely while a stream of readers arrived. In this problem we give priority to writers and allow readers to wait indefinitely While a stream of Writers is Working. On general principles we require that the solution give priority to writers Without making any assumptions about priority being built into the V routine. In other words, where several processes are waiting at a semaphore, we cannot predict which one will be allowed to proceed as the result of a V operation.

在这里,我们保留了writers必须具有独占访问权限,而readers可以共享的要求,但我们添加了一个要求,即:一旦writer准备好写入,他将尽快执行他的write。解决这个问题的办法不能解决问题一,因为为了满足这个要求,一个reader在一个writer宣布他已经准备好写入后到达,他必须等待,即使writer也在等待。对于第一个问题,一个writer可能会无限期地等待一批reader到达。在第二个问题上,我们优先考虑writer,并允许reader无限期地等待一批writer的工作。原则上说,我们会要求解决方案给予writer优先级,而无需对V例程中内置的优先级做任何假设。换句话说,当多个进程在等待一个信号量时,我们无法预测V操作会允许哪个进程继续进行。

We propose the solution shown in Figure 2.

我们在图二中给出了解决方案。

The reader should first note that the use of  mutex 1 and w corresponds exactly to the use of mutex and w in the solution to Problem 1.  The semaphore r is used to protect the act of entering the critical section in the same way that w is used to protect the shared resource in Problem 1. The first Writer to pass P(r) Will block readers from entering the section which manipulates mutex 1 and wmutex 2 is used here for writers just as mutex is used for readers in Problem 1.  mutex 3 is necessary because of our absolute insistence on priority for Writers. Without mutex 3 we have the possibility that a writer and one or more readers will be simultaneously waiting for a V(r) to be done by a reader. In that event we could not guarantee priority to the writer.mutex 3 guarantees a reader exclusive access to the block of code from “P(r)” to “V(r)” inclusive. As a result there will be at most one process ever Waiting at r, and the result of a V is clear.

 读者应首先注意,mutex 1和w的使用方法与问题一的解决方案中mutex和w的完全一致。信号量r用于保护进入关键部分的行为,与w用于保护问题一中的共享资源的方式相同。第一个传递p(r)的writer将阻止reader进入对mutex 1和w操作的部分。mutex 2的部分在这里用于writer,就像mutex用于问题一中的reader一样。mutex 3是必要的,因为我们绝对坚持writer的优先权。没有mutex 3,我们有可能一个writer和一个或多个readers同时等待v(r)被一个reader完成。在这种情况下,我们不能保证writer的优先级。mutex 3保证reader独占访问从P(r)到V(r)的代码块。因此,在r处最多会有一个进程等待,V的结果就清楚了。

Final Remarks

The reader will note that the above solutions do not guarantee a FIFO discipline for the writers. To provide such a guarantee we must either assume further properties of the V operation or make use of an array of n semaphores where n is the number of writers.

Acknowledgment. We are grateful to A.N. Habermann of Carnegie-Mellon University for having shown us an error in an earlier version of this report.

读者会注意到,上述解决方案并不保证writer遵守FIFO规则(先入先出规则)。为了提供这样的保证,我们必须假定v操作的其他属性,或者使用n信号量数组,其中n是reader数。

我们感谢卡内基梅隆大学的A.N.哈伯曼在本报告的早期版本中向我们展示了一个错误。

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值