int readers_count = 0;
semaphore mutex = 1; // binary semaphore
semaphore db = 1; // binary semaphore
void reader() {
while(TRUE) {
x. down(mutex); //or wait(mutex) or P(mutex)
readers_count = readers_count + 1;
y. if(readers_count == 1) down(db);
up(mutex); // or signal(mutex) or // V(mutex)
< access DB >
down(mutex);
readers_count = readers_count - 1;
q. up(mutex); //or signal(mutex) or // V(mutex) //Here buffer is empty and ready to go with next instruction, but still reader can stay inside, so writer cannot execute, only reader can go.
p. if(readers_count == 0) up(db);//reader can reside inside the program, still showing reader=0, which is never possible. Even here writer is getting signal to execute. But not getting V(mutex). Means all writer getting simulteneous signal.
}
}
void writer() {
while(TRUE) {
z: down(db);
< access DB >
up(db);
}
}
1)Reader is empty or not, mutex is giving signal to next instruction.
2) Writer is getting signal, even if there are any reader is present in the process.
3) Writer is not getting mutex signal.
All these three conditions are causing deadlock