Update 计算机操作系统.md
This commit is contained in:
parent
4fa8a6b8d6
commit
4ee1497ade
|
@ -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
|
||||
|
@ -490,8 +490,8 @@ reader() {
|
|||
|
||||
|
||||
//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
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user