Critical Section Problem.pptx

183 views 11 slides Jul 23, 2023
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Good for critical section


Slide Content

Operating System Concepts Instructor Name: Muhammad Asif 1

Quick Recap of Previous Lecture CPU Scheduling Scheduling Criteria Scheduling Algorithms FCFS (First Come First Serve) Example SJF (Shortest Job First) Example Priority Scheduling Round Robin Scheduling Example Multi Level Queue Scheduling Multi Level Feedback Queue 2

Today’s Outlines Critical Section Problem Condition for the Solution to the Critical Section Problem Race Condition Peterson’s Solution 3

Critical Section A Critical Section is a code segment that accesses shared variables and has to be executed as an atomic action. It means that in a group of cooperating processes: at a given point of time, only one process must be executing its critical section. If any other process also wants to execute its critical section, it must wait until the first one finishes . Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes 4

Critical Section Problem n processes all competing to use some shared data Each process has a code segment, called critical section, in which the shared data is accessed. Problem: E nsure that when one process is executing in its critical section, no other process is allowed to execute in its critical section. 5

Conditions for Solution of Critical Section Problem Mutual Exclusion . If process Pi is executing in its critical section, then no other processes can be executing in their critical sections . Progress . If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. Bounded Waiting . A bound must exist on the number of times that other processes are allowed to enter their critical sections A fter a process has made a request to enter its Critical section and before that request is granted . 6

Race Condition The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. To prevent race conditions, concurrent processes must be synchronized. 7

Peterson’s Solution Two process solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: int turn ; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process P i is ready! 8

P eterson’s Algorithm while (true) { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } 9

Peterson’s Algorithm Explanation: Process P0: while (true) { flag[0] = TRUE; turn = 1; while ( turn == 1 &&flag[1]== T); CRITICAL SECTION flag[0] = FALSE ; } // Turn = 10 Process P1: while (true) { flag[1] = TRUE; turn = 0; while ( turn == 0 &&flag[0]== T); CRITICAL SECTION flag[1] = FALSE; } 0 1 // Flag =

Any Questions? 11
Tags