Process Synchronization
Process Synchronization
• Process Synchronization is the task of coordinating the execution of processes in a way that
no two processes can have access to the same shared data and resources.
• It is specially needed in a multi-process system when multiple processes are running
together, and more than one processes try to gain access to the same shared resource or data
at the same time.
• For Example, process A changing the data in a memory location while another process B is
trying to read the data from the same memory location. There is a high probability that data
read by the second process will be erroneous.
• Entry Section: It is part of the process which decides the entry of a particular process.
• Critical Section: This part allows one process to enter and modify the shared variable.
• Exit Section: Exit section allows the other process that are waiting in the Entry Section, to
enter into the Critical Sections. It also checks that a process that finished its execution
should be removed through this Section.
• Remainder Section: All other parts of the Code, which is not in Critical, Entry, and Exit
Section, are known as the Remainder Section.
• Process Synchronization problems occur when two processes running concurrently share the same data or same
variable. The value of that variable may not be updated correctly before its being used by a second process. Such
a condition is known as Race Around Condition. There are a software as well as hardware solutions to this
problem.
• //Shared variable lock initialized to false
• boolean lock;
• while(1){
• while (TestAndSet(lock));
• critical section
• lock = false;
• remainder section
• }
Nahida Nazir 11/19/2024
PRINTER SPOOLER PROBLEM
• Consider a situation where there are multiple processes and only one
printer for printing purposes as the printer is a slower device as compared
to CPU and memory so the spooler comes into the picture. The spooler is
a program that maintains a directory containing a list of files that the
printer has to print. Spooler hands over the file to the printer one by one.
• Every process that wants to keep its file in a spooler needs to execute this
program called Spooler Program:
• Mutex: A mutex is a lock that provides mutual exclusion. When the mutex
is used, then only one thread can work with the entire buffer. If the mutex
is used in the printer spooler then only one program can print its file at a
time.
• In 1965, Dijkstra proposed a new and very significant technique for managing concurrent
processes by using the value of a simple integer variable to synchronize the progress of
interacting processes. This integer variable is called semaphore
• It is a synchronization tool that works on two atomic operations wait() and signal()
• Its an integer variable used in mutual exclusive manner by various concurrent cooperative
processes in order to achieve synchronization.
Counting semaphore (-infinity to +infinity)
Binary semaphore (0, 1)
• In the Semaphore, only one process is allowed to enter into the critical section. In this, the
principle of mutual exclusion is to be followed strictly. And the semaphore mechanism is a
more efficient mechanism than other methods which we use in process synchronization.
• It is machine-independent because it is implemented in the microkernel’s machine-
independent code.
• With the help of the semaphore, the resources are managed flexibly.
• In semaphore, there is a busy waiting, so there is no wastage of resources and process time.
• In the semaphore, more threads are permitted to enter into the critical section