SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
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
 
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
 
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
 
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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
(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
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
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
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
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
 

Kürzlich hochgeladen (20)

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
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
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
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...
 
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🔝
 
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...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
(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...
 
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
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
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
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
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
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

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.