Docsity

Process Synchronization, Study notes of Operating Systems

Toggle sidebar
bg1
An operating system is a software that manages all applications on a device
and basically helps in the smooth functioning of our computer. Because of
this reason, the operating system has to perform many tasks, and
sometimes simultaneously.
This isn't usually a problem unless these simultaneously occurring processes
use a common resource.
Inconsistency of data can occur when various processes share a common
resource in a system which is why there is a need for process
synchronization in the operating system.
Process Synchronization
It is the task phenomenon of coordinating the execution of processes in such
a way that no two processes can have access to the same shared data and
resources.
It is a procedure that is involved to preserve the appropriate order of
execution of cooperative processes.
In order to synchronize the processes, there are various
synchronization mechanisms.
Process Synchronization is mainly 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 any data
at the same time.
Producer-Consumer problem is a classical synchronization problem in the
operating system. With the presence of more than one process and limited
resources in the system the synchronization problem arises. If one resource
is shared between more than one process at the same time then it can lead
to data inconsistency.
What is Producer Consumer Problem?
Before knowing what Producer-Consumer Problem is we have to know what
are Producer and Consumer.
In operating System&Producer is a process which is able to
produce data/item.
Consumer is a&Process that is able to consume the data/item
produced by the Producer.
Both Producer and Consumer share a common memory buffer. This
buffer is a space of a certain size in the memory of the system which is
Producer-Consumer problem aka Bounded-Buffer Problem
pf3
pf4
pf5

Partial preview of the text

Download Process Synchronization and more Study notes Operating Systems in PDF only on Docsity!

An operating system is a software that manages all applications on a device and basically helps in the smooth functioning of our computer. Because of this reason, the operating system has to perform many tasks, and sometimes simultaneously. This isn't usually a problem unless these simultaneously occurring processes use a common resource. Inconsistency of data can occur when various processes share a common resource in a system which is why there is a need for process synchronization in the operating system.

Process Synchronization

It is the task phenomenon of coordinating the execution of processes in such a way that no two processes can have access to the same shared data and resources.  It is a procedure that is involved to preserve the appropriate order of execution of cooperative processes.  In order to synchronize the processes, there are various synchronization mechanisms.  Process Synchronization is mainly 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 any data at the same time. Producer-Consumer problem is a classical synchronization problem in the operating system. With the presence of more than one process and limited resources in the system the synchronization problem arises. If one resource is shared between more than one process at the same time then it can lead to data inconsistency. What is Producer Consumer Problem? Before knowing what Producer-Consumer Problem is we have to know what are Producer and Consumer.  In operating System Producer is a process which is able to produce data/item.  Consumer is a Process that is able to consume the data/item produced by the Producer.  Both Producer and Consumer share a common memory buffer. This buffer is a space of a certain size in the memory of the system which is

Producer-Consumer problem aka Bounded-Buffer Problem

used for storage. The producer produces the data into the buffer and the consumer consumes the data from the buffer. So, what are the Producer-Consumer Problems?

  1. Producer Process should not produce any data when the shared buffer is full.
  2. Consumer Process should not consume any data when the shared buffer is empty.
  3. The access to the shared buffer should be mutually exclusive i.e at a time only one process should be able to access the shared buffer and make changes to it.’ What is race condition (refer lecture slide). In the critical section , a race condition occurs when the end result of multiple thread (process) executions varies depending on the sequence in which the threads execute. But how to avoid this race condition? There is a simple solution: by treating the critical section as a section that can be accessed by only a single process at a time. This kind of section is called an atomic section. What is a Critical Section Problem? A part of code that can only be accessed by a single process at any moment is known as a critical section. The producer-consumer problem is a specific example of a more general situation known as the critical section problem. The general idea is that in a number of cooperating processes, each has a critical section of code, with the following conditions and terminologies: o Only one process in the group can be allowed to execute in their critical section at any one time. If one process is already executing their critical section and another process wishes to do so, then the second process must be made to wait until the first process has completed their critical section work. o The code preceding the critical section, and which controls access to the critical section, is termed the entry section. It acts like a carefully controlled locking door.

 A boolean Flag[]: A boolean array Flag represents which process is which process wants to enter into the critical solution.  int Turn: A integer variable Turn indicates the process number which is ready to enter into the critical section. Let us look at some disadvantages of Peterson's solution are:  The Peterson's solution involves Busy waiting.  The solution is also limited to only 2 processes.

Analysis of Peterson Solution

1. voidEntry_Section (int process)

3. 1. int other;

4. 2. other = 1 -process;

5. 3. interested[process] = TRUE;

6. 4. turn = process;

7. 5. while (interested [other] =True && TURN=proce

ss);

10. Critical Section

12. voidExit_Section (int process)

14. 6. interested [process] = FALSE;

This is a two-process solution. Let us consider two cooperative processes P and P2. The entry section and exit section are shown below. Initially, the value of interested variables and turn variable is 0. Initially process P1 arrives and wants to enter into the critical section. It sets its interested variable to True (instruction line 3) and also sets turn to 1 (line number 4). Since the condition given in line number 5 is completely satisfied by P1 therefore it will enter in the critical section.

  1. P1 → 1 2 3 4 5 CS Meanwhile, Process P1 got preempted and process P2 got scheduled. P2 also wants to enter in the critical section and executes instructions 1, 2, 3 and 4 of entry section. On instruction 5, it got stuck since it doesn't satisfy the condition (value of other interested variable is still true). Therefore it gets into the busy waiting.
  2. P2 → 1 2 3 4 5 P1 again got scheduled and finish the critical section by executing the instruction no. 6 (setting interested variable to false). Now if P2 checks then it are going to satisfy the condition since other process's interested variable becomes false. P2 will also get enter the critical section.
  3. P1 → 6
  4. P2 → 5 CS Any of the process may enter in the critical section for multiple numbers of times. Hence the procedure occurs in the cyclic order.

Bakery Process

The Bakery algorithm is one of the simplest known solutions to the mutual exclusion problem for the general case of N process. Bakery Algorithm is a critical section solution for N processes. The algorithm preserves the first come first serve property. Before entering its critical section, the process receives a number. Holder of the smallest number enters the critical section. If processes Pi and Pj receive the same number

A semaphore is a signaling mechanism, and a process can signal a process that is waiting on a semaphore. This differs from a mutex in that the mutex can only be notified by the process that sets the shared lock. Semaphores make use of the wait() and signal() functions (two atomic operations) for synchronization among the processes. There are two kinds of semaphores:

  1. Binary Semaphores
  2. Counting Semaphores