Lecture Topic: Producer Consumer & Reader Writer Problem

[Lecture 20](https://lms.unb.ca/d2l/le/content/231539/viewContent/2623161/View)

If you were to use two semaphores and a mutex you can have multiple producers and multiple consumers.


Producer
```c
while(true) {
	// produce am item
	wait(empty)
	wait(mutex)
	buffer[index] = item
	index++
	signal(mutex)
	signal(full)
}
```

Consumer
```c
while(true) {
	// consume an item
	wait(empty)
	wait(mutex)
	item = buffer[index]
	index--
	signal(mutex)
	signal(full)
}
```

[Slides Chapter 7](https://lms.unb.ca/d2l/le/content/231539/viewContent/2622059/View)

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.

[Dining Philosopher problem](https://en.wikipedia.org/wiki/Dining_philosophers_problem)


[Slides Chapter 8](https://lms.unb.ca/d2l/le/content/231539/viewContent/2623159/View)

Deadlock Characterization:

Deadlocks can occur if four conditions are true simultaneously
- Mutual exclusion: only one process can use a resource