Bounded waiting in Peterson's solution - operating-system

Considering two processes:
Process 0
do{
flag[0] = TRUE;
turn = 1;
while (flag[1] && turn == 1);
critical section
flag[0] = FALSE;
remainder
}while(1)
Process 1
do{
flag[1] = TRUE;
turn = 0;
while (flag[0] && turn == 0);
critical section
flag[1] = FALSE;
remainder
}while(1)
Where flag[] and turn are shared variables.
Suppose process 0 starts executing first and it stops looping at while. Then process 1 goes and stops as well at while. Then process 0 resumes executing, while condition breaks and it executes its critical section. Fine. My question is how is bounded waiting guaranteed? In this scenario I can't seem to work it out:
Process 0 exits the critical section sets flag[0] = FALSE; but then Process 1 does not resume executing rather Process 0 starts all over again, sets flag[0] = TRUE; and can reenter critical section code. What am I missing?

Process 0 exits the critical section sets flag[0] = FALSE; but then
Process 1 does not resume executing rather Process 0 starts all over
again, sets flag[0] = TRUE;
Yes you are right till here but when Process 0 starts all over again and tries to re-enter critical section , it will execute the following two statements again:
flag[0] = TRUE;
turn = 1;
so turn will be 1 and as we know Process 1 has not yet entered critical section due to which the flag[1] is still true and therefore the loop condition
while (flag[1] && turn == 1);
will be true,This means that process 0 will not be able to enter critical section twice. This satisfies the condition of Bounded Waiting. Also whenever Process 1 will resume its execution the condition
while (flag[0] && turn == 0);
will become false as turn is 1 and it will indeed enter critical section.

Related

dht11 sensor only retuning unexpected number of pulse often

I am trying to make a simple water pump controller using a dth11 to make the pump turn on more frequently when the temperature is higher. i have it working but every 4th or 5th time i call measure on the dht11 sensor i get an error saying "InvalidPulseCount: Expected 82 but got 0 pulses" or "InvalidPulseCount: Got more than 82 pulses". I have added try block that is stopping the program from crashing but would really like to figure out why it is happening. I also had to edit the dht.py lib to have 82 instead of 84 as the default expected pulses because that was what was commonly returned.
here is my main.py file
from machine import Pin
from time import sleep_ms
import dht
import I2C_LCD_driver
sensor = dht.DHT11(Pin(28))
lcd = I2C_LCD_driver.lcd()
pump = Pin(7, machine.Pin.OUT)
counter = 0
pumpTime = 30
normalTime = 60
hotTime = 30
lowTemp = 19
# sensor variables only updated every 3 loops
lastMesure = 1
temp = 0
humid = 0
first = True
while True:
# sensor.messure can only be called ever 3 seconds
# start at 1 and set to zero in the first loop for our first messurement
lastMesure += -1
if lastMesure <= 0:
try:
sensor.measure()
lastMesure = 3
temp = round((sensor.temperature), 0)
humid = sensor.humidity
except:
print("something went wrong")
print("Counter: {:.0f} pumpping:{:0.f}".format(counter, pump.value()))
print("Temp: {:.0f}℃ HUMIDITY: {:.0F}% ".format(temp, humid))
# if the pump is running
if pump.value() == 1:
if counter >= pumpTime: # if it has been the set pump run time
pump.value(0) # turn off pump
counter = 0 # reset counter
else:
counter += 1
else:
# check current temp
# if warmer then {lowTemp} check for {hotTime} else check for {normalTime}
if (temp > lowTemp and counter >= hotTime) or counter >= normalTime:
pump.value(1) # turn on pump
counter = 0 # reset counter
else:
counter += 1
# print current data to the screen
lcd.lcd_clear()
lcd.lcd_display_string("T: {:.0f}C H:{:.0f}%".format(temp, humid), 1)
if pump.value() == 1:
status = f'Pumping {pumpTime - counter}s'
lcd.lcd_display_string(status, 2);
else:
lcd.lcd_display_string("Pump off ", 2)
sleep_ms(1000)
here is a picture of my breadboard set up. I have run it both with and without a 1k pull up resistor on the data pin

Two Process Solution for Critical Section Problem- Algorithm 1

I have started learning Critical Section Problem and its various solutions. To explain my question, let me first give a brief background of it.
The general structure for a two Process Solution for Critical Section Problem- Algorithm 1 is:
turn = 0;
do
{
while (turn != 0) ; //if not P0's turn , wait indefinitely
// critical section of Process P0
turn = 1; //after P0 leaves critical section, lets P1 in
//remainder section
} while (1); //loop again
The problem with this Algorithm is that it doesn't support the necessary requirement of Progress. It forces the critical section to be owned in equal turns by P0 -> P1 -> P0 -> P1 -> ...
To get over this problem Algorithm 2 is used where variable turn is replaced by an array flag[]. The general structure of algorithm 2 is:
do
{
flag[0] = T ;
while (flag[1]);//if flag[1] is true wait indefinitely
// critical section of Process P0
flag [0] = F; //P0 indicated it no longer needs to be in critical section
//remainder section
} while (1); //loop again
Here, a process can execute its critical section repeatedly if it needs. (Although this algorithm too doesn't supports progress)
Now my question, why can't we use the variable turn inside the do-while loop in Algorithm 1 in the same way as we use the variable flag[] in ALgorithm 2? Below code will explain what I mean:
For process 0:
do
{
turn = 0;
while (turn != 0) ; //if not P0's turn , wait indefinitely
// critical section of Process P0
turn = 1; //after P0 leaves critical section, lets P1 in
//remainder section
} while (1); //loop again
For process 1:
do
{
turn = 1;
while (turn != 1) ; //if not P0's turn , wait indefinitely
// critical section of Process P0
turn = 0; //after P1 leaves critical section, lets P0 in
//remainder section
} while (1); //loop again
Wouldn't the above code allow a process to repeatedly execute its critical section, if needed and hence solve the problem in Algorithm 1?
I know there is something wrong here or else this solution would have been used generally, don't know just exactly what it is.
The critical section is no longer protected. Among the arbitrary scheduling sequences there is this one (one line = exclusive execution during this time by process X):
process action contents of 'turn' critical section entered (by process)
0 "turn = 1;" 1 no
0 "} while(1);" 1 no
1 "while (turn != 1);" 1 no
1 "// critical section P1" 1 yes (1)
0 "do{" 1 yes (1)
0 "turn = 0;" 0 yes (1)
0 "while (turn != 0);" 0 yes (1)
0 "// critical section P0" 0 collision!

which solution of critical section is fulfilled in this algorithm

initial value of flag is
flag[2] is false
Process P0
do {
flag[0] = true;while (flag[1]) ;
<CS>
flag [0] = false;
<RS>
} while (1);
Process P1
do {
flag[1] = true;while (flag[0]) ;
<CS>
flag [1] = false;
<RS>
} while (1);
i want to know that if both processes run at the same time then isn't progress condition false?
My reason is that it creates a deadlock b/w two processes to get into critical section.I also want to know is mutual exclusion is being fulfilled there?

How does Test and Set Instruction provide Bounded Waiting in below code?

In the below code how is bounded waiting condition satisfied ,I am unable to get the usage of these statements ,why have they applied the condition j!=i and j=(j+1)%n and then the condition mentioned inside if clause using (j==i),please clarify this ,according to me it should only check for waiting[j] , so as to confirm if any other process is waiting for lock or not ,I am unable to get the idea of working of this algorithm ,please explain it .
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
do {
waiting[i] = true;
while (waiting[i] && test_and_set(&lock)) ;
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
waiting[j] = false;
/* remainder section */
} while (true);
Bounded waiting means no process should wait for a resource for infinite amount of time.
You have n processes, process i is currently executing, when it enters the critical section, it sets its waiting to false.
Now, what happens when process i has finished ?
Process i will look for an index j (process j) that is waiting to run in critical section. In other words, we are looking for a process that is waiting to enter the critical section that is different from the current process that ran in critical section.
if (i==j), then no such process exists, we set lock to false. Otherwise, we set the process that is waiting to run and prevent starvation. And this way you satisfy Bounded Waiting.
This is achieved because you do a circular search, you first check processes
i+1, i+2, .... n, 0, 1, ... ,(i-1)

Why flag[other processes] == true Petersen's Solution

I am recently studying Peterson's Solution to Critical Section problem.
Let i and j two processes entering critical section,
I am not understanding why we set flag[j] == true in While loop when
its i's turn to enter critical section.
do
{
flag[i] = true;
turn = j;
while(**flag[j] == true** && turn == j);
Critical Section
flag[i] = false;
}
while(true);
while(flag[j] == true && turn == j);
This line says that if the other process is executing its critical section, then the first process should keep on waiting till the second process sets its flag variable(shared variable) to false.
Only after the second process executes its critical section, and sets its flag variable(shared variable) to false, the first process would start executing its critical section.