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
intreadCount;// init to 0; number of readers currently accessing resource
intreadCount;// 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
SemaphoreserviceQueue;// FAIRNESS: preserves ordering of requests (signaling must be FIFO)
SemaphoreserviceQueue;// FAIRNESS: preserves ordering of requests (signaling must be FIFO)
voidwriter()
voidwriter()
{
{
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()
voidreader()
voidreader()
{
{
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