SlideShare a Scribd company logo
1 of 8
“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;
}
THANK YOU

More Related Content

Similar to Operatioooooooooooooooooooooooooooooooooooooooooooooo

Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationShipra Swati
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
Peterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodPeterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodWarisBaig
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronizationVaibhav Khanna
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
15- Bakery-Algorithm.pptx
15- Bakery-Algorithm.pptx15- Bakery-Algorithm.pptx
15- Bakery-Algorithm.pptxmobeenahmed49
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
5Process Synchronization.pptx gfgfhgjg jh
5Process Synchronization.pptx gfgfhgjg jh5Process Synchronization.pptx gfgfhgjg jh
5Process Synchronization.pptx gfgfhgjg jhudaybiswas9
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptxAbdullahBhatti53
 

Similar to Operatioooooooooooooooooooooooooooooooooooooooooooooo (20)

CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Ch7
Ch7Ch7
Ch7
 
OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
Peterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodPeterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan Mehmood
 
Ch6
Ch6Ch6
Ch6
 
Cs problem [repaired]
Cs problem [repaired]Cs problem [repaired]
Cs problem [repaired]
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
15- Bakery-Algorithm.pptx
15- Bakery-Algorithm.pptx15- Bakery-Algorithm.pptx
15- Bakery-Algorithm.pptx
 
Os3
Os3Os3
Os3
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
VHDL Behavioral Description
VHDL Behavioral DescriptionVHDL Behavioral Description
VHDL Behavioral Description
 
5Process Synchronization.pptx gfgfhgjg jh
5Process Synchronization.pptx gfgfhgjg jh5Process Synchronization.pptx gfgfhgjg jh
5Process Synchronization.pptx gfgfhgjg jh
 
os4-2_cop.ppt
os4-2_cop.pptos4-2_cop.ppt
os4-2_cop.ppt
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 

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; }
  • 7.