Overclock.net banner

critical stage processing explanation

301 Views 3 Replies 2 Participants Last post by  rabidgnome229
standard variable turn= (0...1).

var flag: Array [0...1] of bookan

P1= flag[1]=true;
turn=2;
While (flag[2] and turn=2) skip

Critical Stage

flag[1]=false
---------------

This is supposed to be a code for two process who wish to enter a critical stage, but can't enter it at the same time. P1 would set flag[1]=true and turn=2 so that the while statement is broken, allowing it to enter the CS.
Then p1 would set flag[1]=false to allow p2 a chance to enter.

P2 is in competition with P1 to enter the CS, as such it would set flag[2]=true and turn=1, so it can break the while statement and enter the CS. If it can't do that it will be stuck in a loop until the while statement is broken.

My question is, what stops P2 from entering the CS while P1 is currently within the CS process? What stops P2 from following P1 into the CS almost instantly after p1 and executing a critical process and producing an error? Won't P2 set the values equal to what it needs to enter the CS while P1 is executing, and assuming execution takes longer then changing values, couldn't that have issues?

A more advance question, what if P2 is of hire priority to the system user(s) but P1 is able to execute the values of turn and flag first. How does the OS address that problem?
1 - 4 of 4 Posts
Quote:


Originally Posted by mothergoose729
View Post

My question is, what stops P2 from entering the CS while P1 is currently within the CS process?

p1 can enter the critical section under two conditions. Firstly, flag[2] could be false. If so p2 will set the turn to 1, and since p1 has set flag[1] to true p2 will not be able to enter the CS until p1 sets flag[1] to false (after it has exited the CS). Secondly, turn could equal 1. If so , since p1 has set flag[1] to true, p2 cannot enter the CS. It will have to wait until p1 sets flag[1] to false

Quote:


What stops P2 from following P1 into the CS almost instantly after p1 and executing a critical process and producing an error?

The two cannot enter the CS at the same time. Even if both flags are set, turn will only allow one to enter at a time. The blocked process must wait until the running process either exits the CS and sets its flag to false, or until it changes turn

Quote:


Won't P2 set the values equal to what it needs to enter the CS while P1 is executing, and assuming execution takes longer then changing values, couldn't that have issues?

p2 cannot set the values equal to what p2 needs to enter, only p1 can. Only p1 can turn flag[1] to false, and only p1 can set turn to 2

Quote:


A more advance question, what if P2 is of hire priority to the system user(s) but P1 is able to execute the values of turn and flag first. How does the OS address that problem?

If a low priority process has a resource that a high priority process needs, the higher priority process simply must wait until the resource is relinquished. There are ways to avoid priority inversion, but there is no safe way to force a process to give up a resource after it has started using it and before it has finished.
See less See more
  • Rep+
Reactions: 1
rabidgnome you are amazing. Thank you very much
See less See more
4
Quote:

Originally Posted by mothergoose729 View Post
rabidgnome you are amazing. Thank you very much

Happy to help


These threads are much more interesting than all of the "hello world wont compile" threads
See less See more
1 - 4 of 4 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top