Synchronisation Problems
Synchronisation Problems
Approach
To visualize this problem, we referred to the following image:
fork1 = Semaphore(1)
fork2 = Semaphore(1)
fork3 = Semaphore(1)
fork4 = Semaphore(1)
fork5 = Semaphore(1)
sem4 = Semaphore(4)
Visualization
Dining Savage
Context
Here is our understandings about this assignment:
• There is one shared resource, namely pot, expressed as a variable called serving.
• There will be an arbitrary number of cooks and savages.
• Cooking is expressed as adding by a certain int.
• Eating is expressed as subtracting by a certain int.
• Once pot is empty, one of the savages will signal one of the cooks for cooking.
• When cooking is done, the cook will signal to the savage.
• This is a rendezvous strategy (one wait for another to proceed).
• While this implementation is deadlock free, we believe that it’s not starvation free. The issue
lies in how the mutex is handled. Hypothetically speaking, If there are multiple vegan
threads, assuming that those are A, B and C, the following can happen:
A takes the mutex.
B and C wait.
A signals, B take the mutex.
A waits again.
B signals, A takes the mutex again.
servings = 5
mutex = Semaphore(1)
meatEmpty = Semaphore(0)
meatFull = Semaphore(0)
vegeEmpty = Semaphore(0)
vegeFull = Semaphore(0)
Visualization
4.2 Readers-Writers
The reader-writer problem is a synchronization issue in computing where processes access a
shared resource, such as a database or a file. The key players are:
• Readers: Read data without altering it.
• Writers: Modify or write new data.
The objective is to allow multiple readers to access the resource simultaneously, while writers
require exclusive access, preventing access to other writers and readers during their
operation.
Key Requirements:
• Use of Condition Variables: Employ these to manage access under specific
conditions, often paired with mutexes to ensure data safety.
• Configurable Priorities: The system can be set to prioritize readers or writers, which
affects whether writers or readers might face delays due to ongoing access by the
other group.
• Scalability: The system should support a dynamic number of threads (e.g., N=7),
ensuring robust operation regardless of the number of active readers or writers.
Variables Used:
• priorReady: Indicates waiting status for prioritized Thread type
• roomEmpty: Indicates whether the shared resource is available or occupied.
• mutexReader and mutexWriter: Provide mutual exclusion for updating the count of
active readers and writers.
• readers and writers: Counters for tracking the number of active readers and writers.
Reader's Code
If writer priority is enabled (writerPrior = True):
o The reader checks the priorReady semaphore to ensure no writer thread is waiting
for execution, then signals it back to allow others to proceed, performing the same
check.
o It then locks mutexReader to safely increment the readers counter.
o If it's the first reader (readers == 1), it wait on roomEmpty to show that critical
section room is being used by at least one Reader Thread.
o Releases mutexReader after updating.
o Then enter critical section
o It then locks mutexReader to safely decrement the readers counter.
o If it's the last reader (readers == 0), it sinal on roomEmpty to show that critical
section room is now empty and all Reader threads have been executed.
o Releases mutexReader after updating.
If writer priority is not enabled (writerPrior = False):
o The writer checks the priorReady semaphore to ensure no reader thread is waiting
for execution, then signals it back to allow others to proceed, performing the same
check.
o It then locks mutexWriter to safely increment the writers counter.
o If it's the first writer (writers == 1), it waits on roomEmpty to show that the critical
section room is being used by at least one Writer Thread.
o Releases mutexWriter after updating.
o Then enter critical section
o It then locks mutexWriter to safely decrement the writers counter.
o If it's the last writer (writers == 0), it signals on roomEmpty to show that the critical
section room is now empty and all Writer threads have been executed.
o Releases mutexWriter after updating.
If writer priority is enabled (writerPrior = True):
When a priority group (like writers or readers) is set, the system allows the non-priority group to
operate freely and in parallel, as long as there are no waiting threads from the priority group.
However, if a thread from the priority group is waiting to execute, it blocks the entry of the new
arived non-priority group. The system then waits for all active non-priority threads to finish before
allowing the waiting priority thread to proceed with its operation.
Configurable Priorities:
o Make writerPrior a configurable parameter that can be dynamically adjusted
according to the system's requirements or load conditions. This would allow
switching priorities, for instance, write operations become more critical during
certain operations.
o Introduce additional logic to handle transitions between priority modes,
doesn’t require change in code. ensuring that changing the priority setting
doesn't lead to deadlock or starvation of any group.
Supporting Multiple Threads (e.g., N=7):
o Such code allow to start any number of Reader and Writer threads without
deadlocks and starvations, hence, making system easily scalable and might be
adjust to any load.