Critical Section Problem in OS - 3 conditions that are used in it

By:   Last Updated: in: ,

Operating System: Q1.What is Critical Section Problem in OS? Q2.What are 3 conditions that are used in critical section problem in os? (Operating System all notes)


Q1. What is Critical Section Problem in OS?

Ans. Critical Section Problem in OS: Given two more processes (or threads) that share a resource (e.g. variable or device), we must often synchronize their activity.
Must satisfy to one degree or another the concepts of mutual exclusion, progress, and bounded waiting.

Example: Consider only two processes critical section (<cs>): instructions that access the shared resource.
We must establish mutual exclusion: no two processes can be in their <cs> at the same time.

process producer {
while (true) {
while (count == BUFFER_SIZE);
buffer[in] = item;
in = (in +1) % BUFFER_SIZE;
process consumer {
while (true) {
while (count == 0);
item = buffer[out];
out = (out - 1) % BUFFER_SIZE;
Assume count = 5 and both producer and consumer execute the statements ++count and --count.
Results? count could be set to 4, 5, or 6 (but only 5 is correct).
reg_1 = count
reg_1 = reg_1 +1
count = reg_1
reg_2 = count
reg_2 = reg_2 - 1
count = reg_2
Principle of atomicity
t_0: producer executes reg_1 = count           [reg_1 = 5]
t_1: producer executes reg_1 = reg_1 + 1     [reg_1 = 6]
t_2: consumer executes rรจg_2 = count          [reg_2 = 5]
t_3: consumer executes reg_2 = reg_2 - 1    [reg_2 = 4]
t_4: producer executes count = reg_1           [count = 6]
t_5: consumer executes count = reg_2          [count = 4]

Race Condition: A situation where multiple processes access and manipulate the same data concurrently and the outcome of the execution depends on the order in which the instructions execute.

The operating system kernel code itself can have race conditions. Preemptive vs. non-preemptive kernels. This is called the critical section problem in os.

Q2. What are three conditions, that are used in critical section problem?

Ans. Following are the three conditions, that are used in this problem:
(a) Mutual Exclusion: Only one process may execute its critical section at once.

(b) Progress: If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes not executing in their remainder sections can participate in the decision on which process will enter its critical section next and this decision cannot be postponed indefinitely.

(c) Bounded Waiting: This is a limit on the number of times other processes are allowed to enter their critical section after a process has made a request to enter its critical section and before that request is granted.

External Links:-

1. Critical Section Problem in OS - (click here)

No comments:
Write comment