Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CyC2018 committed Sep 6, 2018
1 parent cb1f030 commit 3c08c15
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions notes/计算机操作系统.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,12 @@ void writer() {
}
}
```
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).


以下内容由 [@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
```source-c
int readcount, writecount; //(initial value = 0)
semaphore rmutex, wmutex, readLock, resource; //(initial value = 1)
Expand All @@ -482,13 +482,12 @@ void reader() {
<EXIT Section>
down(&rmutex); //reserve exit section - avoids race condition with readers
readcount--; //indicate you're leaving
readcount--; //indicate you're leaving
if (readcount == 0) //checks if you are last reader leaving
up(&resource); //if last, you must release the locked resource
up(&rmutex); //release exit section for other readers
}

//WRITER
void writer() {
<ENTRY Section>
Expand All @@ -514,10 +513,9 @@ 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.


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.

```c
```source-c
int readCount; // init to 0; number of readers currently accessing resource
// all semaphores initialised to 1
Expand All @@ -532,17 +530,16 @@ void writer()
down(&resourceAccess); // request exclusive access to resource
// </ENTER>
up(&serviceQueue); // let next in line be serviced
// <WRITE>
writeResource(); // writing is performed
// </WRITE>
// <EXIT>
up(&resourceAccess); // release resource access for next reader/writer
// </EXIT>
}

void reader()
{
down(&serviceQueue); // wait in line to be serviced
Expand All @@ -554,11 +551,11 @@ void reader()
// </ENTER>
up(&serviceQueue); // let next in line be serviced
up(&readCountAccess); // release access to readCount
// <READ>
readResource(); // reading is performed
// </READ>
down(&readCountAccess); // request exclusive access to readCount
// <EXIT>
readCount--; // update count of active readers
Expand All @@ -568,10 +565,9 @@ void reader()
up(&readCountAccess); // release access to readCount
}


```


### 2. 哲学家进餐问题

<div align="center"> <img src="../pics//a9077f06-7584-4f2b-8c20-3a8e46928820.jpg"/> </div><br>
Expand Down

0 comments on commit 3c08c15

Please sign in to comment.