SlideShare a Scribd company logo
1 of 2
Download to read offline
The following is alleged to be a solution to the critical section problem. Argue for its correctness
or show a case in which it fails.
shared int turn; /* shared variables to synchronize operation */
boolean flag[2]; /* shared variables to synchronize operation */
proc (int i) {
while (TRUE) {
;
flag[i] = TRUE;/* Attempt to enter the critical section */
turn = (i+1)mod 2;
while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ;
/*Now authorized to enter the critical section */
;
/* Exiting the critical section */
flag[i] = FALSE; // Indicate no desire to enter my c.s.
}
}
turn = 0;
flag[0] = flag[1] = FALSE;
fork (proc, 1, 0); /* Start a process to run proc(0) */
fork (proc, 1, 1); /* Start a process to run proc(1) */
Solution
proc (int i) {
while (TRUE) {
;
flag[i] = TRUE;/* Attempt to enter the critical section */
turn = (i+1)mod 2;
while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ;
/*Now authorized to enter the critical section */
;
/* Exiting the critical section */
flag[i] = FALSE; // Indicate no desire to enter my c.s.
}
}
turn = 0;
flag[0] = flag[1] = FALSE;
fork (proc, 1, 0); /* Start a process to run proc(0) */
fork (proc, 1, 1);

More Related Content

More from gopalk44

My protease appears to affect bacteria, but not my own cells. What t.pdf
My protease appears to affect bacteria, but not my own cells. What t.pdfMy protease appears to affect bacteria, but not my own cells. What t.pdf
My protease appears to affect bacteria, but not my own cells. What t.pdf
gopalk44
 
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdfKnow and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
gopalk44
 
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdfImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
gopalk44
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
gopalk44
 
how are the technological approaches of cloning and IPSCs similar to.pdf
how are the technological approaches of cloning and IPSCs similar to.pdfhow are the technological approaches of cloning and IPSCs similar to.pdf
how are the technological approaches of cloning and IPSCs similar to.pdf
gopalk44
 
Discuss the various types of business crimes, a business liability f.pdf
Discuss the various types of business crimes, a business liability f.pdfDiscuss the various types of business crimes, a business liability f.pdf
Discuss the various types of business crimes, a business liability f.pdf
gopalk44
 
Describe the different types of ribs. Which ribs are considered “fal.pdf
Describe the different types of ribs. Which ribs are considered “fal.pdfDescribe the different types of ribs. Which ribs are considered “fal.pdf
Describe the different types of ribs. Which ribs are considered “fal.pdf
gopalk44
 
Circle the best answer to the following questions. From the Bohr mod.pdf
Circle the best answer to the following questions.  From the Bohr mod.pdfCircle the best answer to the following questions.  From the Bohr mod.pdf
Circle the best answer to the following questions. From the Bohr mod.pdf
gopalk44
 
Below are two possible tree topologies. How many clades are different.pdf
Below are two possible tree topologies. How many clades are different.pdfBelow are two possible tree topologies. How many clades are different.pdf
Below are two possible tree topologies. How many clades are different.pdf
gopalk44
 

More from gopalk44 (20)

My protease appears to affect bacteria, but not my own cells. What t.pdf
My protease appears to affect bacteria, but not my own cells. What t.pdfMy protease appears to affect bacteria, but not my own cells. What t.pdf
My protease appears to affect bacteria, but not my own cells. What t.pdf
 
Name the phases in interphase, and describe what happens during those.pdf
Name the phases in interphase, and describe what happens during those.pdfName the phases in interphase, and describe what happens during those.pdf
Name the phases in interphase, and describe what happens during those.pdf
 
Look for at least four definitions of accounting (from four differen.pdf
Look for at least four definitions of accounting (from four differen.pdfLook for at least four definitions of accounting (from four differen.pdf
Look for at least four definitions of accounting (from four differen.pdf
 
Match the word or phrase with the best description of it. ;An express.pdf
Match the word or phrase with the best description of it. ;An express.pdfMatch the word or phrase with the best description of it. ;An express.pdf
Match the word or phrase with the best description of it. ;An express.pdf
 
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
JAVAThe mean of a list of numbers is its arithmetic average. The .pdfJAVAThe mean of a list of numbers is its arithmetic average. The .pdf
JAVAThe mean of a list of numbers is its arithmetic average. The .pdf
 
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdfKnow and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
Know and understand the contributions of Hooke, van Leeuwenhoek, Cohn.pdf
 
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdfImplementDijkstra’s algorithm using the graph class you implemente.pdf
ImplementDijkstra’s algorithm using the graph class you implemente.pdf
 
Illegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdfIllegal numbers.a. Complete the method find which accepts a collec.pdf
Illegal numbers.a. Complete the method find which accepts a collec.pdf
 
how many chromosomes will be found in each cell at the end of anapha.pdf
how many chromosomes will be found in each cell at the end of anapha.pdfhow many chromosomes will be found in each cell at the end of anapha.pdf
how many chromosomes will be found in each cell at the end of anapha.pdf
 
how are the technological approaches of cloning and IPSCs similar to.pdf
how are the technological approaches of cloning and IPSCs similar to.pdfhow are the technological approaches of cloning and IPSCs similar to.pdf
how are the technological approaches of cloning and IPSCs similar to.pdf
 
HELP! Thought I can figure this one out but got it wrong. I have one.pdf
HELP! Thought I can figure this one out but got it wrong. I have one.pdfHELP! Thought I can figure this one out but got it wrong. I have one.pdf
HELP! Thought I can figure this one out but got it wrong. I have one.pdf
 
From a recent statistical analysis for the last five years, on an av.pdf
From a recent statistical analysis for the last five years, on an av.pdfFrom a recent statistical analysis for the last five years, on an av.pdf
From a recent statistical analysis for the last five years, on an av.pdf
 
Find asymptotic upperlower bounds. Find asymptotic upperlower boun.pdf
Find asymptotic upperlower bounds. Find asymptotic upperlower boun.pdfFind asymptotic upperlower bounds. Find asymptotic upperlower boun.pdf
Find asymptotic upperlower bounds. Find asymptotic upperlower boun.pdf
 
Explain how a wireless device can help an organization perform busin.pdf
Explain how a wireless device can help an organization perform busin.pdfExplain how a wireless device can help an organization perform busin.pdf
Explain how a wireless device can help an organization perform busin.pdf
 
Exam scores were normal in BIO 200. Jasons exam score was one stan.pdf
Exam scores were normal in BIO 200. Jasons exam score was one stan.pdfExam scores were normal in BIO 200. Jasons exam score was one stan.pdf
Exam scores were normal in BIO 200. Jasons exam score was one stan.pdf
 
Discuss the various types of business crimes, a business liability f.pdf
Discuss the various types of business crimes, a business liability f.pdfDiscuss the various types of business crimes, a business liability f.pdf
Discuss the various types of business crimes, a business liability f.pdf
 
Describe the different types of ribs. Which ribs are considered “fal.pdf
Describe the different types of ribs. Which ribs are considered “fal.pdfDescribe the different types of ribs. Which ribs are considered “fal.pdf
Describe the different types of ribs. Which ribs are considered “fal.pdf
 
Circle the best answer to the following questions. From the Bohr mod.pdf
Circle the best answer to the following questions.  From the Bohr mod.pdfCircle the best answer to the following questions.  From the Bohr mod.pdf
Circle the best answer to the following questions. From the Bohr mod.pdf
 
C++ ProgramN-number queue rotations.[16] Write methods void Que.pdf
C++ ProgramN-number queue rotations.[16] Write methods void Que.pdfC++ ProgramN-number queue rotations.[16] Write methods void Que.pdf
C++ ProgramN-number queue rotations.[16] Write methods void Que.pdf
 
Below are two possible tree topologies. How many clades are different.pdf
Below are two possible tree topologies. How many clades are different.pdfBelow are two possible tree topologies. How many clades are different.pdf
Below are two possible tree topologies. How many clades are different.pdf
 

Recently uploaded

Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 

The following is alleged to be a solution to the critical section pr.pdf

  • 1. The following is alleged to be a solution to the critical section problem. Argue for its correctness or show a case in which it fails. shared int turn; /* shared variables to synchronize operation */ boolean flag[2]; /* shared variables to synchronize operation */ proc (int i) { while (TRUE) { ; flag[i] = TRUE;/* Attempt to enter the critical section */ turn = (i+1)mod 2; while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ; /*Now authorized to enter the critical section */ ; /* Exiting the critical section */ flag[i] = FALSE; // Indicate no desire to enter my c.s. } } turn = 0; flag[0] = flag[1] = FALSE; fork (proc, 1, 0); /* Start a process to run proc(0) */ fork (proc, 1, 1); /* Start a process to run proc(1) */ Solution proc (int i) { while (TRUE) { ; flag[i] = TRUE;/* Attempt to enter the critical section */ turn = (i+1)mod 2; while ((flag[(i+1) mod 2]) && (turn == (i+1) mod 2)) ; /*Now authorized to enter the critical section */ ; /* Exiting the critical section */ flag[i] = FALSE; // Indicate no desire to enter my c.s. }
  • 2. } turn = 0; flag[0] = flag[1] = FALSE; fork (proc, 1, 0); /* Start a process to run proc(0) */ fork (proc, 1, 1);