1.2 KiB
1.2 KiB
Lecture Topic: Producer Consumer & Reader Writer Problem
If you were to use two semaphores and a mutex you can have multiple producers and multiple consumers.
Producer
while(true) {
// produce am item
wait(empty)
wait(mutex)
buffer[index] = item
index++
signal(mutex)
signal(full)
}
Consumer
while(true) {
// consume an item
wait(empty)
wait(mutex)
item = buffer[index]
index--
signal(mutex)
signal(full)
}
In the reader writer problem, you can encounter the "First reader-writer problem", in which the writer process never writes. A "second reader-writer problem" in which when a writer is ready to write, no reader is available. These problems can sometimes be solved by kernel reader writer locks.
Deadlock Characterization:
Deadlocks can occur if four conditions are true simultaneously
- Mutual exclusion: only one process can use a resource