Skip to content

Commit

Permalink
Update 计算机操作系统.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yugandharbandi authored Sep 5, 2018
1 parent 4fa8a6b commit 4ee1497
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions notes/计算机操作系统.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,10 @@ The first case may result Writer to starve. This case favous Writers i.e no writ

```c
int readcount, writecount; //(initial value = 0)
semaphore rmutex, wmutex, readTry, resource; //(initial value = 1)
semaphore rmutex, wmutex, readLock, resource; //(initial value = 1)

//READER
reader() {
void reader() {
<ENTRY Section>
down(&readLock); // reader is trying to enter
down(&rmutex); // lock to increase readcount
Expand All @@ -482,16 +482,16 @@ 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
writer() {
<ENTRY Section>
void writer() {
<ENTRY Section>
down(&wmutex); //reserve entry section for writers - avoids race conditions
writecount++; //report yourself as a writer entering
if (writecount == 1) //checks if you're first writer
Expand All @@ -510,12 +510,14 @@ writer() {
up(&readLock); //if you're last writer, you must unlock the readers. Allows them to try enter CS for reading
up(&wmutex); //release exit section
}
```

We can observe that every reader is forced to acquire ReadTry lock. On the otherhand, writers doesn’t need to lock individually. Once the first writer locks the ReadTry lock, it will be released only when there is writer left in the queue.
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
int readCount; // init to 0; number of readers currently accessing resource

// all semaphores initialised to 1
Expand All @@ -524,7 +526,7 @@ Semaphore readCountAccess; // for syncing changes to shared variable readCo
Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO)

void writer()
{
{
down(&serviceQueue); // wait in line to be servicexs
// <ENTER>
down(&resourceAccess); // request exclusive access to resource
Expand All @@ -542,7 +544,7 @@ void writer()


void reader()
{
{
down(&serviceQueue); // wait in line to be serviced
down(&readCountAccess); // request exclusive access to readCount
// <ENTER>
Expand Down

0 comments on commit 4ee1497

Please sign in to comment.