SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Semelhante a 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
 

Semelhante a 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
 

Último

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
 
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
 
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
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
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
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
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
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(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
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Último (20)

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, ...
 
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...
 
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...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
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
 
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)
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
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
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
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 in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(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...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(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
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

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.