Process Synchronization: Sourav Banerjee Kgec Kalyani
Process Synchronization: Sourav Banerjee Kgec Kalyani
Sourav Banerjee
KGEC
Kalyani
Contents
• Mutual exclusion
Mutual exclusion
• Consider a mail server that a processes e-mail for an organization. Suppose
we want the system to continuously monitor the total number of e-mails
that have been sent since the day began. Assume that the receipt of an e-
mail is handled by one of several threads. Each time one of these threads
receive an e-mail from a user, the thread increments a processwide shared
variable, mailcount, by 1. consider what happens if two threads attempt to
increment mailcount simultaneously. First, assume that each thread runs
the assembly-language code-
LOAD mailcount
ADD 1
STORE mailcount
Continuation….
• Assume that the LOAD instruction copies mailcount from memory to a
register, the ADD instruction adds the immediate constant 1 from memory
to the value in the register, and the STORE instruction copies the value in
the register to memory. Suppose mailcount is currently 21687. now
suppose the first thread executes the LOAD and ADD instructions, thus
leaving 21688 in the register ( but not yet updating the value in mailcount
memory, which is still 21687). Then, due to a quantum expiration, the first
thread loses the processor and the system context switches to the second
thread. The second thread now executes all three instructions, thus setting
mailcount to 21688. this thread loses processor, and the system context
switches back to the first thread, which then continues by executing the
STORE instruction-also placing 21688 into mailcount. Due to the
uncontrolled access to the shared variable mailcount, the system has
essentially lost track of one of the e-mails-mailcount should be 21689.
Continuation….
• In the case of an e-mail management application such an error may seem
minor. A similar error occurring in a mission-critical application such as
air traffic control could cost lives.
• The cause of this incorrect result in the writing of the shared variable
mailcount. Clearly, many concurrent threads may read data
simultaneously without this difficulty. But when one thread reads data that
another thread is writing , or when one thread writes data that another
thread is also writing, indeterminate results can occur.
• We can solve this problem by granting each thread exclusive access to
mailcount. While one thread increments the shared variable, all other
threads desiring to do so will be made to wait. When the executing thread
finishes accessing the shared variable, the system will allow one of the
waiting processes to proceed. This is called serializing access to shared
variable.
Continuation….
• In this manner , threads will not be able to access the shared data
simultaneously. As each thread proceeds to update the shared variable, all
others are excluded from doing so simultaneously. This is called mutual
exclusion.
Producer/Consumer relationship
• In a producer/consumer relationship, the producer portion of the
application generates data and stores it in a shared object, and the
consumer portion reads data from the shared object.
• One example, is print spooling. A word processor spools data into a buffer
( typically a file) and that data is subsequently consumed by the printer as
it prints the document.
• Consider a multithreaded producer/consumer relationship implemented in
java in which a producer thread generates data and places it into a buffer
capable of holding a single value and a consumer thread reads data from
the buffer. If the producer waiting to put the next data into the buffer
determines that the consumer has not yet read the previous data from the
buffer, the producer should call wait so the consumer gets a chance to read
unconsumed data before it is overwritten.