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).
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)
int readcount, writecount; //(initial value = 0)
semaphore rmutex, wmutex, readLock, resource; //(initial value = 1)
semaphore rmutex, wmutex, readLock, resource; //(initial value = 1)
...
@@ -482,13 +482,12 @@ void reader() {
...
@@ -482,13 +482,12 @@ void 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
}
}
//WRITER
//WRITER
void writer() {
void writer() {
<ENTRY Section>
<ENTRY Section>
...
@@ -514,10 +513,9 @@ void writer() {
...
@@ -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.
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
```source-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
...
@@ -532,17 +530,16 @@ void writer()
...
@@ -532,17 +530,16 @@ void writer()
down(&resourceAccess); // request exclusive access to resource
down(&resourceAccess); // request exclusive access to resource
// </ENTER>
// </ENTER>
up(&serviceQueue); // let next in line be serviced
up(&serviceQueue); // let next in line be serviced
// <WRITE>
// <WRITE>
writeResource(); // writing is performed
writeResource(); // writing is performed
// </WRITE>
// </WRITE>
// <EXIT>
// <EXIT>
up(&resourceAccess); // release resource access for next reader/writer
up(&resourceAccess); // release resource access for next reader/writer
// </EXIT>
// </EXIT>
}
}
void reader()
void reader()
{
{
down(&serviceQueue); // wait in line to be serviced
down(&serviceQueue); // wait in line to be serviced
...
@@ -554,11 +551,11 @@ void reader()
...
@@ -554,11 +551,11 @@ void reader()
// </ENTER>
// </ENTER>
up(&serviceQueue); // let next in line be serviced
up(&serviceQueue); // let next in line be serviced
up(&readCountAccess); // release access to readCount
up(&readCountAccess); // release access to readCount
// <READ>
// <READ>
readResource(); // reading is performed
readResource(); // reading is performed
// </READ>
// </READ>
down(&readCountAccess); // request exclusive access to readCount
down(&readCountAccess); // request exclusive access to readCount
// <EXIT>
// <EXIT>
readCount--; // update count of active readers
readCount--; // update count of active readers
...
@@ -568,10 +565,9 @@ void reader()
...
@@ -568,10 +565,9 @@ void reader()
up(&readCountAccess); // release access to readCount
up(&readCountAccess); // release access to readCount