0% found this document useful (0 votes)
7 views7 pages

Lab 6 Op&Sys

The document outlines a laboratory exercise on threads and multitasking, detailing tasks that involve running and modifying a thread demo program. It discusses the behavior of threads in critical sections, how their execution can vary, and modifications to ensure that only one thread is in the critical section at a time. Additionally, it suggests improvements to allow one thread to execute more frequently than the other while maintaining mutual exclusion.

Uploaded by

miguel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Lab 6 Op&Sys

The document outlines a laboratory exercise on threads and multitasking, detailing tasks that involve running and modifying a thread demo program. It discusses the behavior of threads in critical sections, how their execution can vary, and modifications to ensure that only one thread is in the critical section at a time. Additionally, it suggests improvements to allow one thread to execute more frequently than the other while maintaining mutual exclusion.

Uploaded by

miguel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operating Systems

Laboratory 6

alberto suárez luis


904276
Introduction to Threads / multitasking
Task 1
Copy code from cs file (Thread_Demo_1), run and
analyse the results.

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

Modify your program to include a public integer


value called threadnumber initialized to 1 and
modify the threads in the manner shown below:
public class Version_2
{
public static bool done = false;
public static int threadnumber = 1;
public static void T1()
{
System.Random rnd = new Random();
while (!done)
{
while (threadnumber == 2) ;
Console.WriteLine("T1 in critical section");
Thread.Sleep(rnd.Next(100));
Console.WriteLine("T1 is leaving critical section");
threadnumber = 2;
}
}
public static void T2()
{
System.Random rnd = new Random();
while (!done)
{
while (threadnumber == 1) ;
Console.WriteLine("T2 in critical section");
Thread.Sleep(rnd.Next(100));
Console.WriteLine ("T2 is leaving critical section");
threadnumber = 1;
}
}

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.

iii). Modify your threads to include a Sleep time


outside the critical section as shown

public class Version_2


{

public static bool done = 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(100));
Console.WriteLine("T1 is leaving critical section");
threadnumber=2;
}
}

public static void T2()


{
System.Random rnd = new Random();
while (!done)
{
Thread.Sleep(rnd.Next(1000));
while(threadnumber==1);
Console.WriteLine("T2 in critical section");
Thread.Sleep(rnd.Next(100));
Console.WriteLine("T2 is leaving critical section");
threadnumber=1;
}
}

static void Main(string[] args)


{
Thread p = new Thread(new ThreadStart(T1));
p.Start();
Thread q = new Thread(new ThreadStart(T2));

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.

What determines the fastest run time of both T1 and T2?

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();
}
}
}

You might also like