Units 3 & 4 Flashcards
(41 cards)
Shared resources
processes that are working together often need to access the same data or hardware resource
Producer-consumer model
2 or more processes working with a shared resource
producer - writes data to the shared resource
consumer - reads data from the shared resource
Condition synchronisation
mechanism to ensure that a process is blocked if some condition is not met
Mutual exclusion
control access to shared data
only one process at a time had exclusive access to a shared resource
What are race conditions?
when the final outcome of a process is dependent on the timing or sequence of other processes
Atomic action
either happens completely or it does not happen at all
fine-grained or course-grained
Critical region
a section of code in which a shared resource is being accessed
must be executed under mutual exclusion
Entry protocol
code that must be executed before entering a critical region
prevents a process entering the critical region if another process is already in a critical region that accesses the same data resource
Exit protocol
code that a process must execute immediately on completion of its critical region
signals the resource is now available to other processes
Semaphore
data structure for solving synchronisation problems
like a permit - only one process at a time can hold the permit + a process must get hold of the permit before it can enter the critical region
Binary semaphore
for mutual exclusive access to a single shared resource
several processes sharing a resource will share one semaphore
initial value of 1
Counting semaphore
to protect access to multiple instances of shared data
initial value set to the number of resource instances available
Blocking semaphore
to synchronise two processes
initialised to 0
Monitors
data structures to encapsulate the shared data
shared data is accessible only through special monitor operations which are atomic
facilitate mutual exclusion
support condition synchronisation
Monitor construct
well-defined interface
only operations defined in the interface can be used to access the monitor’s data
a monitor operation is equivalent to a critical region with entry + exit protocols
In what ways can semaphores be used?
ensuring mutual exclusion from critical code
controlling access to shared resources
synchronising cooperating processes
How can the incorrect use of semaphores lead to problems?
by forgetting to use semWait it is possible for a resource be become unprotected
by forgetting semSignal it is possible for a resource to become locked indefinitely
Condition variables
special variables associated with the monitor that enable the monitor to handle condition synchronisation
have a queue - if a condition is not satisfied a process can be made to wait in a queue for that specific condition
Thread safety
a class is said to be thread safe if it can be executed concurrently by several threads in a safe manner - without data getting into an inconsistent state
Multiple readers, single writer policy
readers - threads that read the contents of a file
writers - threads that write to a file
need to keep track of how many threads are currently reading or writing - 2 variables: int activeReaders (>= 0) int activieWriters (1 or 0)
reader thread needs to check the value of activeWriters before it begins to read
a writer thread should check the values of both (one writer only)
Writers-preferred policy
shared resource is given to a waiting writer (if there is one)
only if there is no writers waiting will it be given to readers
all waiting readers will be allowed access at the same time
Readers-preferred policy
the shared resource is given to all waiting readers (if there are any) + is given to a writer only if no readers are waiting
Alternating reader-writers policy
shared resource is given to a waiting writer when readers have finished + to readers when writers have finished
Take a number policy
shared resource is given to threads in order of arrival