“Critical Section Problem Using Peterson’s
Solution”
By,
SHREYAS V E - 4VV21CS147
PRAMUKH B V - 4VV21CS190
SHREYA CHAKRAVARTHY S - 4VV21CS145
SHANE MATHEW - 4VV21CS139
Subject: Operating Systems
Subject Code: 21CS52
Faculty in-charge: Dr. Madhusudhan H S
Algorithm:
ANALYSIS:
• Peterson's solution is restricted to two.
• The processes are numbered P0 and P1 or Pi
and Pj where j = 1-i
• Peterson's solution requires the two processes
to share two data items:
int turn;
boolean flag[2];
• The variable turn indicates whose turn it is to
enter its critical section.
• The flag array is used to indicate if a process is
ready to enter its critical section.
• To enter the critical section,
• process Pi first sets flag [i] = true
• turn = j
• If both processes try to enter at the same time,
turn will be set to both i and j at roughly the
same time.
To prove that solution is correct, then we need to show that
1. Mutual exclusion is preserved
• Only one process can enter the critical section.
• If Pi and Pj are the two processes that enter the critical section, the turn will be given to any one among i or j
but not both.
2. Progress requirement is satisfied
• Assume that the process is waiting continuously to enter the critical section i.e, Pi is in while loop and
flag[j]=true and turn==j
• However the process Pj in the critical section, flag[j] will become false and now Pi can enter into the critical
section to progress.
3. Bounded-waiting requirement is met
• Since each process gives the turn to another process bounded waiting is assured and each process will wait
at most one entry by another process.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> // for sleep()
#define TRUE 1
#define FALSE 0
int flag[2] = { FALSE, FALSE }; // Flags to indicate if process is ready to enter
critical section
int turn = 0; // Variable to indicate whose turn it is

void* process(void* arg) {
int process_id = *((int*)arg);
int other = 1 - process_id;
for (int i = 0; i < 5; i++) {
flag[process_id] = TRUE;
turn = other;
while (flag[other] == TRUE && turn == other) {
// Process waits because it's not its turn or the other process is ready
}
// Process enters critical section
printf("Process %d is in the critical section.n", process_id);
sleep(1); // Simulating critical section work
// Process exits critical section
flag[process_id] = FALSE;
}
pthread_exit(NULL);
}
int main() {
pthread_t threads[2];
int ids[2] = { 0, 1 };
int i;
// Creating threads
for (i = 0; i < 2; i++) {
if (pthread_create(&threads[i], NULL, process, &ids[i]) != 0) {
perror("pthread_create");
return -1;
}
}
// Joining threads
for (i = 0; i < 2; i++) {
if (pthread_join(threads[i], NULL) != 0) {
perror("pthread_join");
return -1;
}
}
return 0;
}
Operatioooooooooooooooooooooooooooooooooooooooooooooo
THANK YOU

More Related Content

PPT
Peterson Critical Section Problem Solution
PPTX
14- Process Synchronization.pptx
PPT
Process Synchronization
PPTX
operating system notes about deadlock 3.pptx
PPTX
Chapter6 Synchronization in Operating systems.pptx
PPT
Process synchronization(deepa)
PDF
6 Synchronisation
PDF
OperatingSystem-Unit2_Process Management
Peterson Critical Section Problem Solution
14- Process Synchronization.pptx
Process Synchronization
operating system notes about deadlock 3.pptx
Chapter6 Synchronization in Operating systems.pptx
Process synchronization(deepa)
6 Synchronisation
OperatingSystem-Unit2_Process Management

Similar to Operatioooooooooooooooooooooooooooooooooooooooooooooo (20)

PPT
Operating System
PPTX
Critical Section Problem - Ramakrishna Reddy Bijjam
PPTX
Operating system critical section
PDF
CH05.pdf
PDF
Process Synchronization
PPT
PPT
PPT
Lecture16-17.ppt
PPTX
Mutual Exclusion using Peterson's Algorithm
PPTX
process synchronization topic of operating system
PDF
Peterson’s Solution.pdf by Mustehsan Mehmood
PPTX
Synchronization Peterson’s Solution.pptx
PPTX
Cs problem [repaired]
PPT
OS Process Synchronization, semaphore and Monitors
PPTX
Operating system 23 process synchronization
PPT
Section06-Syncopkojiojoijnnjkhuubgfffppt
PPTX
Lecture 8.pptx Operating system lecture
PPTX
Describing the Peterson solution sw.pptx
Operating System
Critical Section Problem - Ramakrishna Reddy Bijjam
Operating system critical section
CH05.pdf
Process Synchronization
Lecture16-17.ppt
Mutual Exclusion using Peterson's Algorithm
process synchronization topic of operating system
Peterson’s Solution.pdf by Mustehsan Mehmood
Synchronization Peterson’s Solution.pptx
Cs problem [repaired]
OS Process Synchronization, semaphore and Monitors
Operating system 23 process synchronization
Section06-Syncopkojiojoijnnjkhuubgfffppt
Lecture 8.pptx Operating system lecture
Describing the Peterson solution sw.pptx
Ad

Recently uploaded (20)

PDF
Design Guidelines and solutions for Plastics parts
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
PDF
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Soil Improvement Techniques Note - Rabbi
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
Design Guidelines and solutions for Plastics parts
Information Storage and Retrieval Techniques Unit III
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
737-MAX_SRG.pdf student reference guides
Fundamentals of safety and accident prevention -final (1).pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
Visual Aids for Exploratory Data Analysis.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
August 2025 - Top 10 Read Articles in Network Security & Its Applications
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Soil Improvement Techniques Note - Rabbi
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Amdahl’s law is explained in the above power point presentations
Abrasive, erosive and cavitation wear.pdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
III.4.1.2_The_Space_Environment.p pdffdf
Ad

Operatioooooooooooooooooooooooooooooooooooooooooooooo

  • 1. “Critical Section Problem Using Peterson’s Solution” By, SHREYAS V E - 4VV21CS147 PRAMUKH B V - 4VV21CS190 SHREYA CHAKRAVARTHY S - 4VV21CS145 SHANE MATHEW - 4VV21CS139 Subject: Operating Systems Subject Code: 21CS52 Faculty in-charge: Dr. Madhusudhan H S
  • 2. Algorithm: ANALYSIS: • Peterson's solution is restricted to two. • The processes are numbered P0 and P1 or Pi and Pj where j = 1-i • Peterson's solution requires the two processes to share two data items: int turn; boolean flag[2]; • The variable turn indicates whose turn it is to enter its critical section. • The flag array is used to indicate if a process is ready to enter its critical section. • To enter the critical section, • process Pi first sets flag [i] = true • turn = j • If both processes try to enter at the same time, turn will be set to both i and j at roughly the same time.
  • 3. To prove that solution is correct, then we need to show that 1. Mutual exclusion is preserved • Only one process can enter the critical section. • If Pi and Pj are the two processes that enter the critical section, the turn will be given to any one among i or j but not both. 2. Progress requirement is satisfied • Assume that the process is waiting continuously to enter the critical section i.e, Pi is in while loop and flag[j]=true and turn==j • However the process Pj in the critical section, flag[j] will become false and now Pi can enter into the critical section to progress. 3. Bounded-waiting requirement is met • Since each process gives the turn to another process bounded waiting is assured and each process will wait at most one entry by another process.
  • 4. #include <stdio.h> #include <pthread.h> #include <unistd.h> // for sleep() #define TRUE 1 #define FALSE 0 int flag[2] = { FALSE, FALSE }; // Flags to indicate if process is ready to enter critical section int turn = 0; // Variable to indicate whose turn it is 
  • 5. void* process(void* arg) { int process_id = *((int*)arg); int other = 1 - process_id; for (int i = 0; i < 5; i++) { flag[process_id] = TRUE; turn = other; while (flag[other] == TRUE && turn == other) { // Process waits because it's not its turn or the other process is ready } // Process enters critical section printf("Process %d is in the critical section.n", process_id); sleep(1); // Simulating critical section work // Process exits critical section flag[process_id] = FALSE; } pthread_exit(NULL); }
  • 6. int main() { pthread_t threads[2]; int ids[2] = { 0, 1 }; int i; // Creating threads for (i = 0; i < 2; i++) { if (pthread_create(&threads[i], NULL, process, &ids[i]) != 0) { perror("pthread_create"); return -1; } } // Joining threads for (i = 0; i < 2; i++) { if (pthread_join(threads[i], NULL) != 0) { perror("pthread_join"); return -1; } } return 0; }