Advanced Operating Systems Assignment no_02



Ms140400009
Advanced Operating Systems
Assignment no_02
Question 2:
(a)    Briefly explain why AcquireReadLock() and AcquireWriteLock() functions perform P and V operations on the mutex semaphore in the above code?15
Solution:
Mutex semaphore in the above code is a binary semaphore having value 0 or 1. When its value will be 1 then reader or writer will be able to check the condition variables. If its value is 0 then reader or writer will not be able to proceed further. Basically it is used to ensure mutual exclusion. Any reader or writer wants to acquire lock he will first perform P operation on mutex semaphore which will decrease its value to 0. After this operation it will check condition variables no one else will be able to check the condition variables. As value of mutex semaphore is 0 which ensures mutual exclusion. After completing the job reader or writer will perform V operation on mutex semaphore which will increase its value to 1.
(b)   Briefly explain whether readers or writers could be starved due to this implementation?10
Solution:
It can be noted that there is possibility of starvation for writers and readers as well. So there are two possibilities:
In a reader wants to access database then it only checks if there is no active reader. In this way if readers keeps coming on before the release of previous reader then readers  will always hold the database. In this way writers will be starved because for writers to acquire  lock it is condition that there should be no active reader or writer.
When a reader or writer releases a lock, priority is always given to waiting writers. In this way if there are a lot waiting writers then they will always get the chance to access the database and readers will be starved.
(c)    Suggest a mechanism through which a no starvation policy could be implemented. In other words, suggest in words, how would you modify the code such that a starvation-free implementation results.15
Solution:
In the modified code given below there will be only one reader or writer accessing the database at any time. The main point is when a reader will release the lock it will give priority to waiting writer and when a writer will release the lock it will give priority to waiting reader to access database. In this way there will be no starvation. My modified code is given below:
Class ReaderWriterLock {

Semaphore mutex = 1; // declaration of a semaphore

OkToRead = 0; // a flag to check it is OK to read the database

OkToWrite = 0; // a flag to check it it is OK to write the database



int ACTIVEREADERS=0;// number of readers holding the read lock and accessing the database

WAITINGREADERS=0; // number of readers waiting to acquire read lock

ACTIVEWRITERS=0; // number of writers that have acquired write lock

 // (practically this will always be 1)



WAITINGWRITERS=0; // number of writers that are waiting for write lock



voidAcquireReadLock()

{

P(mutex); // remember that P operation on a semaphore means decrementing its value

if ((ACTIVEWRITERS + ACTIVEREADERS== 0)

{

V(OkToRead); // remember that V operation on a semaphore means incrementing its value
ACTIVEREADERS++;

}

else

{

 WAITINGREADERS++;

}

V(mutex);

P(OkToRead);

}

voidReleaseReadLock()

{

P(mutex);

 ACTIVEREADERS--;

if (WAITINGWRITERS > 0)

 {

V(OkToWrite);

 ACTIVEWRITERS++;

 WAITINGWRITERS--;

 }

V(mutex);

}

voidAcquireWriteLock()

{

P(mutex);

if (ACTIVEWRITERS + ACTIVEREADERS == 0)

 {

V(OkToWrite);

 ACTIVEWRITERS++;

 }

else

 {

 WAITINGWRITERS++;

 }

V(mutex);

P(OkToWrite);

}





voidReleaseWriteLock()

{

P(mutex);

 ACTIVEWRITERS--;

if (WAITINREADERS > 0)

 {

V(OkToRead);

 ACTIVEREADERS++;

 WAITINNREADERS--;

 }

V(mutex);

}

} // end of class


Question 2
Review the Readers/Writers problem discussed in lecture 12, write the code for Reader() and Writer() functions, when readers are given priority over writers, keeping the problem constraints in mind.
Solution:
Modified code for the above question is given below:
State variables:
 # of active readers -- AR = 0
 # of active writers -- AW = 0
 # of waiting readers -- WR = 0
 # of waiting writers -- WW = 0
 Condition okToRead = NIL
 Condition okToWrite = NIL
 Lock lock = FREE
Code:
Reader() {
lock.Acquire();
while (AW > 0) { // check if safe to read
// if any writers, wait
WR++;
okToRead.Wait(&lock);
WR--;
}
AR++;
lock.Release();
Access DB
lock.Acquire();
AR--;
If (AR == 0 && WR= 0)//if no other readers still
okToWrite->Broadcast(&lock);
lock.Release();
}
Writer() { // symmetrical
lock.Acquire();
while ((AW + AR) > 0) { // check if safe to write
// if any readers or writers, wait
WW++;
okToWrite->Wait(&lock);
WW--;
}
AW++;
lock.Release();
Access DB
// check out
lock.Acquire();
AW--;
if (WR> 0) // give priority to readers
okToRead->Signal(&lock);
else if (WW> 0)
okToWrite->Broadcast(&lock);
lock.Release();
}