How to test progress and bounded waiting in Peterson's algorithm?

  • Comp Sci
  • Thread starter shivajikobardan
  • Start date
In summary, Petersons's solution for critical section problem satisfied all three. Mutual exclusion was achieved, progress was tested, and bounded waiting was verified.
  • #1
shivajikobardan
674
54
Homework Statement
How to test progress and bounded waiting in Peterson's algorithm?
Relevant Equations
How to test progress and bounded waiting in Peterson's algorithm?
This is Petersons's solution for critical section problem. I want to test mutual exclusion, progress and bounded waiting for it. Of course, it satisfied all three.

Code:
Process0

while(true)
{
intendToEnter0=true;
turn=1;
while(intendToEnter1==T && turn==1);
critical section
intendToEnter0=false;
non-critical section
}Process1while(true)
{
intendToEnter1=true;
turn=0;
while(intendToEnter0==T && turn==0);
critical section
intendToEnter1=false;
non-critical section
}

1) Mutual Exclusion

Definition:

No two cooperating processes can enter into their critical section at the same time. For example, if a process P1 is executing in its critical section, no other cooperating process can enter in the critical section until P1 finishes with it.

Test:

Say P1 is in its critical section.
That means turn=0 & intendToEnter1=true.

Now, P0 attempts to enter its critical section.
That means it sets intendToEnter0=true & turn=1.

In AND condition if any condition is false, the output is false. Both condition are true here, so it waits.So, mutual exclusion is achieved.2) Progress

Definition:

If no process is in its CS and there are one or more processes that wish to enter their CS, this selection cannot be postponed indefinitely.

No process in remainder section can participate in this decision.

How to test?

3) Bounded Waiting

Definition:

After a prcess P has made a request to enter its CS, there is a limit on the number of times that the other processes are allowed to enter their CS, before P's request is granted.
How to test?

How to test?
 
Physics news on Phys.org
  • #2
Here is a discussion on the nuances of implementing a critical section, with multiple examples.
https://rosettacode.org/wiki/Events

It is not merely a single "flavor" which may be why you are confused.
 
  • #3
At the design level, this type of code would normally be tested by exhaustive inspection. At the atomic operation level, each place in the code where shared memory is either set of read is identified. Then all possible combinations of preemption at those points is considered and it is demonstrate by argument that none will allow execution of both critical sections at the same time - while also allowing for eventual execution after proper waiting. Of course, in this case, the design is well known and well documented. So that kind of testing is done.

At the implementation level, I would suggest a couple of shared variables RunningProcess0CS and RunningProcess1CS. I'll leave it to you exactly how you would use and check those variables. Hopefully the OS you are working with has some king of "yield" to prompt context switching. This can sometime be done by "waiting 0 seconds".

Right away, I will tell you that all shared variables (turn, intentToEnter*, RunningProcess*CS) must be declared volatile. Also, you must be running in a system where preemption is possible and that each of the two process while loops has the potential of being preempted by the other process.
 

1. How does Peterson's algorithm ensure progress?

Peterson's algorithm ensures progress by using a turn variable to determine which process has access to the critical section at any given time. If a process wants to enter the critical section, it must first set its turn variable to its own process number, indicating that it wants to enter. This ensures that only one process can enter the critical section at a time, thus ensuring progress.

2. How does Peterson's algorithm prevent deadlock?

Peterson's algorithm prevents deadlock by using a flag variable to indicate when a process wants to enter the critical section. If a process sets its flag to true, it indicates that it wants to enter the critical section. However, if the other process's flag is also true, the algorithm uses the turn variable to determine which process has priority. This prevents both processes from being stuck waiting for each other, thus preventing deadlock.

3. How does Peterson's algorithm handle bounded waiting?

Peterson's algorithm handles bounded waiting by using the turn variable to ensure that each process has a fair chance to enter the critical section. If a process finds that the other process's turn variable is set to its process number, it knows that the other process has already entered the critical section and it must wait. This ensures that no process can be blocked indefinitely, thus enforcing bounded waiting.

4. Can Peterson's algorithm be used for more than two processes?

Yes, Peterson's algorithm can be extended to work for more than two processes. However, as the number of processes increases, the complexity of the algorithm also increases. For n processes, the algorithm requires n turn variables and n flag variables, making it less practical for larger numbers of processes.

5. Are there any limitations or drawbacks to using Peterson's algorithm?

One limitation of Peterson's algorithm is that it requires busy waiting, which can waste CPU cycles and decrease system performance. Additionally, the algorithm only works for mutually exclusive access to a single critical section and cannot be used for more complex synchronization scenarios. It also relies on processes to cooperate and follow the algorithm correctly, which may not always be the case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • STEM Academic Advising
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
7
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
Back
Top