mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-26 12:53:43 +00:00
Update 计算机操作系统 - 进程管理.md
读者-写者问题中的三段英文说明改为中文;代码块语言 source-c 改为 c。
This commit is contained in:
@@ -425,7 +425,7 @@ void writer() {
|
||||
|
||||
以下内容由 [@Bandi Yugandhar](https://github.com/yugandharbandi) 提供。
|
||||
|
||||
The first case may result Writer to starve. This case favous Writers i.e no writer, once added to the queue, shall be kept waiting longer than absolutely necessary(only when there are readers that entered the queue before the writer).
|
||||
上面的实现可能导致写者饥饿。下面的实现是写者优先的:写者进入队列之后,只需要等待在它之前进入的读者,不会被之后到达的读者一直阻塞。
|
||||
|
||||
```c
|
||||
int readcount, writecount; //(initial value = 0)
|
||||
@@ -476,11 +476,11 @@ void writer() {
|
||||
}
|
||||
```
|
||||
|
||||
We can observe that every reader is forced to acquire ReadLock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadLock, it will be released only when there is no writer left in the queue.
|
||||
每个读者都必须先获取 readLock,而只有第一个写者需要获取 readLock,并且直到队列中没有写者时才会释放,因此这种实现可能导致读者饥饿。
|
||||
|
||||
From the both cases we observed that either reader or writer has to starve. Below solutionadds 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.
|
||||
以上两种实现总有一方可能饥饿。下面的实现使用一个先进先出的 serviceQueue 让读者和写者按到达顺序排队,获取共享数据的锁总能在有限时间内完成,因此不会出现饥饿。
|
||||
|
||||
```source-c
|
||||
```c
|
||||
int readCount; // init to 0; number of readers currently accessing resource
|
||||
|
||||
// all semaphores initialised to 1
|
||||
|
||||
Reference in New Issue
Block a user