Syncronisation

























Introduction

Synchronisation problem

On the basis of synchronization, processes divided into two types:

Independent Process : if a process cannot affect or be affected by the other processes executing in the system then the process is said to be independent
Cooperative Process : a process is said to be cooperating if it can affect or be affected by the other processes executing in the system. So , any process which shares its data with other processes is a cooperating process.

Process synchronization problem arises in the case of Cooperative process also because resources are shared in Cooperative processes.

Race Condition

A race condition is a situation that may occur inside a critical section. This happens when the result of multiple thread execution in critical section differs according to the order in which the threads execute.Race conditions in critical sections can be avoided if the critical section is treated as an atomic instruction. Also, proper thread synchronization using locks or atomic variables can prevent race conditions.

Critical Section

The critical section in a code segment where the shared variables can be accessed. Atomic action is required in a critical section i.e. only one process can execute in its critical section at a time. All the other processes have to wait to execute in their critical sections.

Semaphores

A semaphore is a signalling mechanism and a thread that is waiting on a semaphore can be signalled by another thread. This is different than a mutex as the mutex can be signalled only by the thread that called the wait function.A semaphore uses two atomic operations, wait and signal for process synchronization. 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.

Barrier Semaphore

In parallel computing, a barrier is a type of synchronization method. A barrier for a group of threads or processes in the source code means any thread/process must stop at this point and cannot proceed until all other threads/processes reach this barrier.The basic barrier has mainly two variables, one of which records the pass/stop state of the barrier, the other of which keeps the total number of threads that have entered in the barrier. The barrier state was initialized to be "stop" by the first threads coming into the barrier. Whenever a thread enters, based on the number of threads already in the barrier, only if it is the last one, the thread sets the barrier state to be "pass" so that all the threads can get out of the barrier. On the other hand, when the incoming thread is not the last one, it is trapped in the barrier and keeps testing if the barrier state has changed from "stop" to "pass", and it gets out only when the barrier state changes to "pass".

Reader Writer

This is an example of common concurrency problem. Here many threads try to access the same shared resource simultaneously. Threads may read/write (or append). Threads reading the shared resource can do it simultaneously, i.e. two or more readers are allowed to access the shared resource. If a thread is writing into the resource, no other thread can access the resource.

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.