Lab 6 Op&Sys
Lab 6 Op&Sys
Laboratory 6
a. Do you get the same results each time you run the program? No
Why or why not?
Because the time the threat rests is a random amount from 0 to 100 ms.
b. Can both threads be in their respective critical sections at the same time? Yes, as
we can see in this screenshot, randomly may coincide.
1
c. Can one of the threads execute more often than the other? Yes, it is due to the
amount of time they sleep during execution. As we can see on the screenshot of the
question d.
d. Change the maximum Sleep time of one of the threads from 100 to 1000 and run the
program.
e. How many executions of the while loop were completed for thread T1 5, T2 37?
f. Why do threads continue to execute after the Boolean done is set to true by the
main?
Because done true forces T1 and T2 to finish, but they are still running, so once it
says run finished, both T1 and T2 are leaving critical section.
2
TASK 2
a. What is difference in the execution of this version of the program compared to the
previous version?
At the beginning when not entering at the same time the threads can no longer be
in the critical sections at the same time, as shown by the start and end of the
programme.
b. Can both threads be in their respective critical sections at the same time?
No
c. Change the maximum Sleep time of one of the two threads from 100 to 1000 and run
the program.
d. How many executions of the while loop were completed for thread T1, T2?
T1 8 , T2 7.
e. ii). What is the maximum number of times each thread can execute after done is set
to true?
3
One time, if they were sleeping while the boolean was changed.
4
q.Start();
Thread.Sleep(5000);
done = true;
Console.WriteLine("Run Finished");
Console.ReadLine();
}
}
}
Does this version of the program prevent both threads from being in their respective critical
sections at the same time? Yes, as we can see.
Discuss how you could change your program and threads so that one of the threads could
execute more often than the other without both being in their respective critical sections at
the same time. Hint: Consider including additional Boolean parameters T1_wants_to_run,
and T2_wants_to_run.
We can add 2 public static Booleans one for T1 (T1_wants_to_run) and one for T2
(T2_wants_to_run), at the start their value is false but when they are into the loop they go
true.
public static bool done = false;
public static bool flag_T1 = false;
public static bool flag_T2 = false;
public static int threadnumber = 1;
public static void T1()
{
System.Random rnd = new Random();
while (!done)
{
Thread.Sleep(rnd.Next(1000));
while (threadnumber == 2) ;
Console.WriteLine("T1 in critical section");
Thread.Sleep(rnd.Next(1000));
Console.WriteLine("T1 is leaving critical section");
5
threadnumber = 2;
flag_T1 = true;
}
}
public static void T2()
{
System.Random rnd = new Random();
while (!done)
{
Thread.Sleep(rnd.Next(100));
while (threadnumber == 1) ;
Console.WriteLine("T2 in critical section");
Thread.Sleep(rnd.Next(100));
Console.WriteLine("T2 is leaving critical section");
threadnumber = 1;
flag_T2 = true;
}
}
static void Main(string[] args)
{
Thread p = new Thread(new ThreadStart(T1));
p.Start();
Thread q = new Thread(new ThreadStart(T2));
q.Start();
Thread.Sleep(5000);
done = true;
Console.WriteLine("Run Finished");
Console.ReadLine();
}
}
}