SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
(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
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(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
 
(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
 
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
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
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
 

Dernier (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
(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...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(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...
 
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
 
(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...
 
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
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
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...
 

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.