Process Synchronization Theory


Synchronization problem

On the basis of synchronization, processes are categorized as one of the following two types:
Independent Process : Execution of one process does not affects the execution of other processes.
Cooperative Process : Execution of one process affects the execution of other processes.
Process synchronization problem arises in the case of Cooperative process also because resources are shared in Cooperative processes.


Race Condition

Several processes access and process the manipulations over the same data concurrently, then the outcome depends on the particular order in which the access takes place.


Semaphores

Semaphore is simply a variable. This variable is used to solve the critical section problem and to achieve process synchronization in the multiprocessing environment. The two most common kinds of semaphores are counting semaphores and binary semaphores. Counting semaphore can take non-negative integer values and Binary semaphore can take the value 0 & 1. only.


Mutex

When you have a multi-threaded application, the different threads sometimes share a common resource, such as a variable or similar. This shared source often cannot be accessed at the same time, so a construct is needed to ensure that only one thread is using that resource at a time. The concept is called "mutual exclusion" (short Mutex), and is a way to ensure that only one thread is allowed inside that area, using that resource etc.
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program


Producer Consumer

It is also known as the bounded buffer problem. The problem consists of two processes, the producer and consumer, sharing a common buffer (fixed size). The producer's job is to generate data and store it in the common buffer, one at a time. The consumer keeps consuming data until the common buffer empties. The producer should't be able to produce more data items when the buffer is full, and the consumer shouldn't be able to consumer the data items when the buffer is empty.


Dining Philosopher

The problem consists of five philosophers sitting around a circular table. The table has five forks. At any instance, a philosopher is eating or thinking. When a philosopher wants to eat, he has to use two forks, one from his left and one from his right. When a philosopher starts thinking, he keeps the two forks on the table. So, philosopher requires the two forks(shared resource) to start eating.


Readers-Writers Problem

The readers-writers problem relates to an object such as a file that is shared between multiple processes. Some of these processes are readers i.e. they only want to read the data from the object and some of the processes are writers i.e. they want to write into the object. The readers-writers problem is used to manage synchronization so that there are no problems with the object data. For example - If two readers access the object at the same time there is no problem. However if two writers or a reader and writer access the object at the same time, there may be problems.

Sleeping Barber

This is an example of synchronization problem. The objective is to keeping a barber working when there are customers, resting when there are none in an orderly manner. The barber has one barber chair and a waiting room with a number of chairs in it. When the barber finishes cutting a customer's hair, he dismisses the customer and then goes to the waiting room to see if there are other customers waiting. If there are, he brings one of them back to the chair and cuts his hair. If there are no other customers waiting, he returns to his chair and sleeps in it. Each customer, when he arrives, looks to see what the barber is doing. If the barber is sleeping, then the customer wakes him up and sits in the chair. If the barber is cutting hair, then the customer goes to the waiting room. If there is a free chair in the waiting room, the customer sits in it and waits his turn. If there is no free chair, then the customer leaves.