SlideShare ist ein Scribd-Unternehmen logo
1 von 19
 A cooperating process is one that can affect or be
affected by other processes executing in the
system.
 Concurrent access to shared data may result in
data inconsistency.
 Previously we studied producer-consumer
problem to understand cooperating processes.
 Our solution allowed at most BUFFER.SIZE - 1
items in the buffer at the same time (refer to old
slides).
 To remedy this deficiency we can add an integer
variable counter, initialized to 0.
 Counter is incremented every time we add a new
item to the buffer and is decremented every time
we remove one item from the buffer.
 producer-consumer problem
Shared Data:
 #define BUFFER_SIZE 10
 int buffer [BUFFER_SIZE] ;
 int in = 0 ,
 int out = 0 ;
 int count=0;
while (true)
/* produce an item and put in nextProduced
while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
while (true)
{
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
/*consume the item in nextConsumed
}
 Although both producer and consumer routines are
correct separately, they may not function correctly
when executed concurrently.
 For example the variable counter is currently 5 and
that the producer and consumer processes execute
the statements "counter++" and "counter—"
concurrently.
 Following the execution of these two statements, the
value of the variable counter may be 4, 5, or 6!
.(correct result, though, is counter == 5)
 In machine language count++ could be implemented as
MOV R1, COUNTER (R1=5)
 INC R1 (R1=6)
 count-- could be implemented as
MOV R2, COUNTER (R2=5)
 DEC R2 (R2=4)
 Consider this execution interleaving with “count = 5” initially:
producer execute MOV COUNTER, R1{count = 6 }
consumer execute MOV COUNTER, R2{count = 4}
What will be the value of Count=?
 Notice that we have incorrect state "count == 4",
indicating that four buffers are full, when, in fact, five
buffers are full.
 This happened because we allowed both processes to
manipulate the variable count concurrently.
 When several processes access and manipulate the
same data concurrently and the outcome of the
execution depends on the particular order in which
the access takes place, is called a race condition.
 To guard against the race condition, we need to
ensure that only one process at a time can be
manipulating the variable counter, hence the need for
processes synchronization.
 In concurrent programming, a critical section is a
piece of code that accesses a shared resource (data
structure or device) that must not be concurrently
accessed by more than one thread of execution.
 The critical-section problem is to design a protocol
that the processes can use to cooperate.
 Each process must request permission to enter its
critical section. The section of code implementing this
request is the entry section. The critical section may
be followed by an exit section. The remaining code is
the remainder section.
A solution to the critical-section problem must satisfy the
following three requirements:
Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections.
Progress - If no process is executing in its critical section
and there exist some processes that wish to enter their
critical section, then the selection of the processes that
will enter the critical section next cannot be postponed
indefinitely.
Bounded Waiting - There exists a bound, or limit, on the
number of times that other processes are allowed to
enter their critical sections after a process has made a
request to enter its critical section and before that
request is granted.
 A software-based solution to the critical-section problem
is known as Peterson's solution.
 It is restricted to two processes that alternate
execution between their CS and remainder sections.
 Peterson's solution may not work correctly on modern
architectures because of machine-language instructions
(like example of variable count in producer-consumer in
previous slides).
 We present the solution because it provides a good
algorithmic description of solving the critical-section
problem and addresses the requirements of mutual
exclusion, progress, and bounded waiting requirements.
 Processes are numbered P0 and P1.
 Peterson's solution requires two data items to be
shared between the two processes:
◦ int turn;
◦ Boolean flag[2]
 The variable turn indicates whose turn it is to enter
the critical section.
 The flag array is used to indicate if a process is
ready to enter the critical section. flag[i] = true
implies that process Pi is ready!
 To enter the critical section, process Pi first sets
flag[i] to be true and then sets turn to the value j,
thereby asserting that if the other process wishes
to enter the critical section, it can do so.
 If both processes try to enter at the same time,
turn will be set to both i and j at roughly the same
time.
 Only one of these assignments will last; the other
will occur but will be overwritten immediately.
do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
} while (TRUE);
do {
flag[0] = TRUE;
turn = 1;
while ( flag[1] && turn == 1);
CRITICAL SECTION
flag[0] = FALSE;
REMAINDER SECTION
} while (TRUE);
do {
flag[1] = TRUE;
turn = 0;
while ( flag[0] && turn == 0);
CRITICAL SECTION
flag[1] = FALSE;
REMAINDER SECTION
} while (TRUE);
 We now prove that this solution is correct. We need
to show that:
 1. Mutual exclusion is preserved?
 2. The progress requirement is satisfied?
 3. The bounded-waiting requirement is met?

Weitere ähnliche Inhalte

Ähnlich wie Lecture16-17.ppt

Process synchronization
Process synchronizationProcess synchronization
Process synchronizationAbeera Naeem
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronizationVaibhav Khanna
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfGeekyHassan
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxAmanuelmergia
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfBhanuCharan9
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.MOHIT DADU
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordinationsiva krishna
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 

Ähnlich wie Lecture16-17.ppt (20)

OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdf
 
09 sinkronisasi proses
09 sinkronisasi proses09 sinkronisasi proses
09 sinkronisasi proses
 
Ch7
Ch7Ch7
Ch7
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdf
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Shared Memory
Shared MemoryShared Memory
Shared Memory
 
Distributed Coordination
Distributed CoordinationDistributed Coordination
Distributed Coordination
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 

Kürzlich hochgeladen

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...liera silvan
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
EmpTech Lesson 18 - ICT Project for Website Traffic Statistics and Performanc...
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

Lecture16-17.ppt

  • 1.
  • 2.  A cooperating process is one that can affect or be affected by other processes executing in the system.  Concurrent access to shared data may result in data inconsistency.
  • 3.  Previously we studied producer-consumer problem to understand cooperating processes.  Our solution allowed at most BUFFER.SIZE - 1 items in the buffer at the same time (refer to old slides).  To remedy this deficiency we can add an integer variable counter, initialized to 0.  Counter is incremented every time we add a new item to the buffer and is decremented every time we remove one item from the buffer.
  • 4.  producer-consumer problem Shared Data:  #define BUFFER_SIZE 10  int buffer [BUFFER_SIZE] ;  int in = 0 ,  int out = 0 ;  int count=0;
  • 5. while (true) /* produce an item and put in nextProduced while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
  • 6. while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /*consume the item in nextConsumed }
  • 7.  Although both producer and consumer routines are correct separately, they may not function correctly when executed concurrently.  For example the variable counter is currently 5 and that the producer and consumer processes execute the statements "counter++" and "counter—" concurrently.  Following the execution of these two statements, the value of the variable counter may be 4, 5, or 6! .(correct result, though, is counter == 5)
  • 8.  In machine language count++ could be implemented as MOV R1, COUNTER (R1=5)  INC R1 (R1=6)  count-- could be implemented as MOV R2, COUNTER (R2=5)  DEC R2 (R2=4)  Consider this execution interleaving with “count = 5” initially: producer execute MOV COUNTER, R1{count = 6 } consumer execute MOV COUNTER, R2{count = 4} What will be the value of Count=?
  • 9.  Notice that we have incorrect state "count == 4", indicating that four buffers are full, when, in fact, five buffers are full.  This happened because we allowed both processes to manipulate the variable count concurrently.  When several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition.  To guard against the race condition, we need to ensure that only one process at a time can be manipulating the variable counter, hence the need for processes synchronization.
  • 10.  In concurrent programming, a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution.  The critical-section problem is to design a protocol that the processes can use to cooperate.  Each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section may be followed by an exit section. The remaining code is the remainder section.
  • 11.
  • 12. A solution to the critical-section problem must satisfy the following three requirements: Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.
  • 13. Bounded Waiting - There exists a bound, or limit, on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.
  • 14.  A software-based solution to the critical-section problem is known as Peterson's solution.  It is restricted to two processes that alternate execution between their CS and remainder sections.  Peterson's solution may not work correctly on modern architectures because of machine-language instructions (like example of variable count in producer-consumer in previous slides).  We present the solution because it provides a good algorithmic description of solving the critical-section problem and addresses the requirements of mutual exclusion, progress, and bounded waiting requirements.
  • 15.  Processes are numbered P0 and P1.  Peterson's solution requires two data items to be shared between the two processes: ◦ int turn; ◦ Boolean flag[2]  The variable turn indicates whose turn it is to enter the critical section.  The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!
  • 16.  To enter the critical section, process Pi first sets flag[i] to be true and then sets turn to the value j, thereby asserting that if the other process wishes to enter the critical section, it can do so.  If both processes try to enter at the same time, turn will be set to both i and j at roughly the same time.  Only one of these assignments will last; the other will occur but will be overwritten immediately.
  • 17. do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE);
  • 18. do { flag[0] = TRUE; turn = 1; while ( flag[1] && turn == 1); CRITICAL SECTION flag[0] = FALSE; REMAINDER SECTION } while (TRUE); do { flag[1] = TRUE; turn = 0; while ( flag[0] && turn == 0); CRITICAL SECTION flag[1] = FALSE; REMAINDER SECTION } while (TRUE);
  • 19.  We now prove that this solution is correct. We need to show that:  1. Mutual exclusion is preserved?  2. The progress requirement is satisfied?  3. The bounded-waiting requirement is met?