My task from the teacher is to create a program that solves the three problems using threads in Python: 2 files: reader.py and writer.py In the readers-writers problem there is a common resource where a group of actors called readers want to read from the resource and another group of actors called writers want to write to the resource. The basic synchronization problem that needs to be solved is: 1. Mutual exclusion of the resource, where: • only one writer may print at a time, and • no writer may write while a reader is reading. An ordinary mutex lock that is applied equally to all actors can solve this but will lead to only one actor being able to enter its critical section at a time. This is a problem as we normally want several readers to be able to read at the same time.
My task from the teacher is to create a
2 files: reader.py and writer.py
In the readers-writers problem there is a common resource where a group of actors called readers want to read from the resource and another group of actors called writers want to write to the resource.
The basic synchronization problem that needs to be solved is:
1. Mutual exclusion of the resource, where:
• only one writer may print at a time, and
• no writer may write while a reader is reading.
An ordinary mutex lock that is applied equally to all actors can solve this but will lead to only one actor being able to enter its critical section at a time. This is a problem as we normally want several readers to be able to read at the same time.
The next problem that needs to be solved is therefore the following:
2. Several readers should be able to read the resource at the same time.
In a solution where several readers can read simultaneously and lock out the writers, there is always a risk of starvation in the writers. The more readers and the more time they spend in their critical section, the more unlikely it is that writers will find an opening where all readers are outside their critical section.
The last problem that needs to be solved is therefore the following:
3. As soon as a writer wants to write, new readers must not enter their critical section.1
In other words, writers have priority over readers. As a rule, this is preferable to the opposite as a resource, such as the value of a variable, is out of date for a reader until the writer has updated it. Readers thus read an outdated value while the writer waits.
Step by step
Solved in 2 steps