未验证 提交 4ee1497a 编写于 作者: B Bandi Yugandhar 提交者: GitHub

Update 计算机操作系统.md

上级 4fa8a6b8
...@@ -464,10 +464,10 @@ The first case may result Writer to starve. This case favous Writers i.e no writ ...@@ -464,10 +464,10 @@ The first case may result Writer to starve. This case favous Writers i.e no writ
```c ```c
int readcount, writecount; //(initial value = 0) int readcount, writecount; //(initial value = 0)
semaphore rmutex, wmutex, readTry, resource; //(initial value = 1) semaphore rmutex, wmutex, readLock, resource; //(initial value = 1)
//READER //READER
reader() { void reader() {
<ENTRY Section> <ENTRY Section>
down(&readLock); // reader is trying to enter down(&readLock); // reader is trying to enter
down(&rmutex); // lock to increase readcount down(&rmutex); // lock to increase readcount
...@@ -482,7 +482,7 @@ reader() { ...@@ -482,7 +482,7 @@ reader() {
<EXIT Section> <EXIT Section>
down(&rmutex); //reserve exit section - avoids race condition with readers 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 if (readcount == 0) //checks if you are last reader leaving
up(&resource); //if last, you must release the locked resource up(&resource); //if last, you must release the locked resource
up(&rmutex); //release exit section for other readers up(&rmutex); //release exit section for other readers
...@@ -490,8 +490,8 @@ reader() { ...@@ -490,8 +490,8 @@ reader() {
//WRITER //WRITER
writer() { void writer() {
<ENTRY Section> <ENTRY Section>
down(&wmutex); //reserve entry section for writers - avoids race conditions down(&wmutex); //reserve entry section for writers - avoids race conditions
writecount++; //report yourself as a writer entering writecount++; //report yourself as a writer entering
if (writecount == 1) //checks if you're first writer if (writecount == 1) //checks if you're first writer
...@@ -510,12 +510,14 @@ writer() { ...@@ -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(&readLock); //if you're last writer, you must unlock the readers. Allows them to try enter CS for reading
up(&wmutex); //release exit section up(&wmutex); //release exit section
} }
```
We can observe that every reader is forced to acquire ReadTry lock. On the otherhand, writers doesnt 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. 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 int readCount; // init to 0; number of readers currently accessing resource
// all semaphores initialised to 1 // all semaphores initialised to 1
...@@ -524,7 +526,7 @@ Semaphore readCountAccess; // for syncing changes to shared variable readCo ...@@ -524,7 +526,7 @@ Semaphore readCountAccess; // for syncing changes to shared variable readCo
Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO) Semaphore serviceQueue; // FAIRNESS: preserves ordering of requests (signaling must be FIFO)
void writer() void writer()
{ {
down(&serviceQueue); // wait in line to be servicexs down(&serviceQueue); // wait in line to be servicexs
// <ENTER> // <ENTER>
down(&resourceAccess); // request exclusive access to resource down(&resourceAccess); // request exclusive access to resource
...@@ -542,7 +544,7 @@ void writer() ...@@ -542,7 +544,7 @@ void writer()
void reader() void reader()
{ {
down(&serviceQueue); // wait in line to be serviced down(&serviceQueue); // wait in line to be serviced
down(&readCountAccess); // request exclusive access to readCount down(&readCountAccess); // request exclusive access to readCount
// <ENTER> // <ENTER>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册