Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Multi-threading, concurrency, and semaphores. The following process definitions

ID: 3822463 • Letter: M

Question

Multi-threading, concurrency, and semaphores.

The following process definitions were intended to solve the reader-writer problem. they depend on two binary semaphores and a counter variable, initialized as follows:

int readcount;

semaphore mutex, wrt;

mutex=1, wrt=1, readcount=1;

Each writer process (i=1,2,3...) is coded as follows:

Writer:

begin ...

wait(wrt);

...

writing is performed

...

signal(wrt)

...

end.

Note: wait is Dijkstra's P operator and signal is Dijkstra's V operator. The dots(...) denote code that may differ from writer to writer.

Each reader process (j=1,2,3...) is coded as follows:

Reader:

begin ...

wait(mutex);

readcount++;

if(readcount==1) wait(wrt);

signal(mutex);

...

reading is performed

...

wait(mutex);

readcount--;

if(readcount==0) signal(wrt);

signal(mutex);

...

end.

Note: wait is Dijkstra's P operator and signal is Dijkstra's V operator. The dots(...) denote code that may differ from reader to reader.

Based on the above code, answer the fallowing ten questions. Explaim your answers. If a situation is possible, you need to give an example of how it is possible(e.g. when a particular semaphore/counter has a certain value and Reader2 is waiting on Reader1...). If a situation is not possible , you need to explain why(e.g. Reader1 is blocked by the wait in its code while Writer3 has not yet called signal in its code).

g) What happens if Readers13 calls wait(wrt) instead of wait(mutex) at the start of its code?

h) What happens if Writer7 forgets to call signal(wrt) after it completes writing to the database?

i) What is the situation when wrt=0, mutex=1, and readcount=3. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?

j) What is the situation when wrt=1, mutex=0, and readcount=0. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?

Explanation / Answer

The following process definitions were intended to solve the reader-writer problem. they depend on two binary semaphores and a counter variable, initialized as follows:

int readcount;

semaphore mutex, wrt;

mutex=1, wrt=1, readcount=1;

Each writer process (i=1,2,3...) is coded as follows:

Writer:

begin ...

wait(wrt);

...

writing is performed

...

signal(wrt)

...

end.

Note: wait is Dijkstra's P operator and signal is Dijkstra's V operator. The dots(...) denote code that may differ from writer to writer.

Each reader process (j=1,2,3...) is coded as follows:

Reader:

begin ...

wait(mutex);

readcount++;                         /*Critical Section 1*/

if(readcount==1) wait(wrt);

signal(mutex);

...

reading is performed

...

wait(mutex);

readcount--;                           /*Critical Section 2*/

if(readcount==0) signal(wrt);

signal(mutex);

...

This is multiple readers -writers problem. basically, there are two locks in the system one is wrt which is to be acquired whenever data needs to be written or to be read. Each writer process has to acquire it individually while for readers, only 1st reader need to acquire the lock. In other words, if a lock a currently held by a reader, other readers need not compete for wrt lock.

readcount indicates no of readers in the system.

if(readcount==1) wait(wrt); indicates that only 1st reader needs to acquire wrt lock.

however, readcount is a variable shared amongst reader processes. to synchronize reader process to access and update readcount mutex lock/semaphore is used.

g) What happens if Readers13 calls wait(wrt) instead of wait(mutex) at the start of its code?

Reader 13 would never be able to enter its critical section 1 until all currently active readers have exited their critical section 2.after that it will compete with other readers and writers to acquire wrt lock.

once it acquires wrt lock,it will become 1st reader and again would for wrt lock, hence will stuck forever. System will enter deadlock.

h) What happens if Writer7 forgets to call signal(wrt) after it completes writing to the database?

No other reader or writer would be able to read and write respectively, if writer 7 forgets to release the lock.

i) What is the situation when wrt=0, mutex=1, and readcount=3. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?

There are 3 readers in system as readcount = 3. none of the readers are executing in their critical sections as mutex = 1.

it is not possible to determine no of writers in system.

j) What is the situation when wrt=1, mutex=0, and readcount=0. In other words, exactly how many Readers and how many Writers are waiting or accessing the database?

There is exactly one reader in the system who has just entered its critical section 1 (as mutex = 0) and is yet to update readcount.

Please let me know in case of any doubts.

Thanks