Answer this operating system question about semaphores and p
Answer this operating system question about semaphores and processes
In a system, there are multiple reader processes which read from a shared file and multiple writer processes which update a shared file. The following variables arc shared among all processes: int readCount; FILE *sharedFile: semaphore mutex, writeBlock; Reader and writer processes are given in the following C++-like pseudo programs: Reader Process while(TRUE) P{mutex); 3 readCount++ if (readCount==l) P(writeBlock); V(mutex); Read(sharedFile);//Critical section P(mutex); readCount--; if (readCount ==0) V{writeBlock); V(mutex);} Writer Process while(TRUE) {; P(writeBlock); Update(sharedFile)://Critical section V{writeBlock);} How should the shared variable readCount, and the semaphores mutex and writeBlock be initialized so that the system would work properly? Suppose that when a writer is updating, three new readers have arrived and they are the only reader processes at that time. Show where the readers are blocked. Suppose that some readers arc reading, explain what happens to a newly arrived writer. Are writers subject to starvation in this solution? Justify your answer.Solution
Semaphore:
A semaphore is a generalized mutex.
It lie on single buffer, we can split the 4kb buffer into 1kb buffers.
A semaphore can be associated with the foue buffers. The consumer and producer can work on different buffers at the same time.
Processes:
Process is nothing but computing, a process is an instance of a computer program that is being executed.
It contains the program code and its current activity. Depending on the operating system, a process may be made up of multiple threads of execution that execute instructions concurrently.
Reader-Writers processes:
readres-writers problems are examples of a common computing problme in concurrency. There are at least three variations of the problems, which deal with situations in which many threads try to access the same shared resource at one time. Some threads may read and some may write with the constraint that no process may access the share for either reading or writing. While another process is in writing of it.
A reader-writer lock is a data structure that sloves one or more of the reader-witers problems.
Reader PRECEDENCE:
reader() {
while(TRUE) {
<other computing>;
P(mutex);
readCount++;
if(readCount == 1)
P(writeBlock);
V(mutex);
/* Critical section */
access(resource);
P(mutex);
readCount--;
if(readCount == 0)
V(writeBlock);
V(mutex);
}
}
resourceType *resource;
int readCount = 0;
semaphore mutex = 1;
semaphore writeBlock = 1;
fork(reader, 0);
fork(writer, 0);
writer() {
while(TRUE) {
<other computing>;
P(writeBlock);
/* Critical section */
access(resource);
V(writeBlock);
}
First reader competes with writers
Last reader signals writers.
Any writer must wait for all the readers.
Readers can strave writers.
Updates can be delayed forever.
Writer Precedence:
reader() {
while(TRUE) {
<other computing>;
P(writePending);
P(readBlock);
P(mutex1);
readCount++;
if(readCount == 1)
P(writeBlock);
V(mutex1);
V(readBlock);
V(writePending);
access(resource);
P(mutex1);
readCount--;
if(readCount == 0)
V(writeBlock);
V(mutex1);
}
}
int readCount = 0, writeCount = 0;
semaphore mutex = 1, mutex2 = 1;
semaphore readBlock = 1, writeBlock = 1, writePending = 1;
fork(reader, 0);
fork(writer, 0);
writer() {
while(TRUE) {
<other computing>;
P(mutex2);
writeCount++;
if(writeCount == 1)
P(readBlock);
V(mutex2);
P(writeBlock);
access(resource);
V(writeBlock);
P(mutex2)
writeCount--;
if(writeCount == 0)
V(readBlock);
V(mutex2);
}



