Table of Contents
ToggleIntroduction
Process synchronization is the sharing of resources among processes in a manner that the concurrent accessibility to these shared resources is managed thus reducing the possibility of having data inconsistency. To ensure consistency in data, it is necessary to have methods to ensure that the execution is synchronized of processes that are co-operating.
In this post we will try to uncover one of the important concepts in process synchronization i.e critical section in OS.
Critical Section In OS
Critical section in OS is code segment that has access to shared variables such as common variables and files, and perform write operations on them and these write operations must be executed in an atomic manner for data consistency. This means that, when there are multiple co-operating processes, at any given moment there should be only one process that must be running the critical portion. If other process want to run the critical section, they must wait until the previous one has exited the critical section.
If write operations are not performed in atomic manner and it is not guaranteed that only one process will be allowed to enter in critical section at a time, any process can be interrupted mid-execution as processes run concurrently. As critical section in OS contains shared resources, partial execution of processes can lead to data inconsistencies.
Race Conditions
Race condition is a situation when the output depends on relative timing or interleaving or order of multiple threads or processes being executed. One or more possible outcomes may be possible depending on which process is executed first which is undesirable, resulting in a data inconsistencies.
Therefore, we need a synchronization protocol that allows processes to cooperate while manipulating shared resources, which essentially is the critical section problem.
Components Of Critical Section In OS
Components of critical section in OS includes
- Entry Section: The part of process which decides the entry of a particular process. In this part the code request entry to the critical section.
- Critical Section: This section contains shared resources and it ensures only one process enters and modifies the shared resources.
- Exit Section: This section handles entry of the other process that are waiting in the Entry Section, to Critical Sections. It also handles removal of a process that finished its execution
- Remainder Section: All other parts of the Code, which is not in Critical, Entry, and Exit Section, are known as the Remainder Section.
Requirements For Solution Of Critical Section In OS
A solution for the critical section in OS must meet the following three requirements
- Mutual exclusion
- Progress
- Bound waiting
Mutual exclusion: In a group of concurrent processes, mutual exclusion principle states that there should only be one process that should be considered to be in critical section at any given point of time i.e no two processes can exist in the critical section at any given point of time.
Progress: Formal definition of Progress says that:
If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder section can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefinitely.
To simplify above statement, only processes that can participate in the decision-making of who can enter the critical section are those that are about to enter the critical section or are executing some code before entering the critical section and processes that are in their reminder section cannot participate in this decision-making process.Progress helps to ensure that one process is executing the critical section at any given point and selection of which process will enter the critical section that should be in limited time and cannot be postponed indefinitely as it can lead to deadlock.
Bounded waiting: Once the process has made an attempt to gain access to its critical section There is a limit to how many other processes are able to access their section before the request is granted. When the limit time is reached, the system has to give the process permission to access the critical area.
Solutions To Critical Section Problem
- Peterson’s Algorithm: Peterson’s Algorithm uses two shared variables to solve critical section problem
- turn – An integer variable that determines whose turn is to enter the critical section.
- boolean flag[i] – A boolean array indicating whether process wants to enter critical section or not.
- test_and_set: It uses a test and set instruction to provide the synchronization among the processes executing concurrently. The shared variable is lock the algorithm always returns whatever value is sent to it and sets lock to true.
- compare_and_swap: Similar to test_and_set algorithm but instead of directly setting lock to true, in this algorithm key is set to true and then swapped with lock. So, when a process is in the critical section, no other process gets to enter it as the value of lock is true.
- Mutex locks and Semaphores: These are locks that we set before using a shared resource and release after using it. When the lock is set, no other thread can access the locked region of code.Mutex use acquire() and release functions where as semaphores use wait() and signal() functions. To know difference between semaphores and mutex see here.
Conclusion
For proper synchronization of multiple process/threads one must ensure mutual exclusion, bounded waiting and progress.In this post we discussed critical section in OS, its components and how can we avoid problem of critical section in OS.
Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!