Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

operating systems Synchronization Problem Write a simple two-thread program that consists of the following: - The main thread spawns 2 threads (call then thread1 and

operating systems
image text in transcribed
image text in transcribed
Synchronization Problem Write a simple two-thread program that consists of the following: - The main thread spawns 2 threads (call then thread1 and thread2). - The main thread must wait for both threads to be finished. - Each tread will sleep for a random amount of time (between 3 and 10 seconds) and then print that the thread is finished. - Arrange the program so that thread2 is started before thread1. - Run the program 20 times and record the number of times (and percentages) that thread 1 finishes first and how many time thread 2 finishes first. - Include a copy of the program in your report. Critical Regions and Race Conditions Obtain a copy of the syncproblem.py program and put it onto your Linux computer and run it. The program has two threads that will attempt to fill an array of numbers either 1000 or 2000 by looking for a zero and then adding 1000 (thread 1) or 2000 (thread 2) to the value. Unfortunately, the block of code in the "if" statement is based on a global variable. There is a chance that one thread will be in the middle of the if statement when the second thread interrupts it causing both threads to increment 1000 and 2000 causing a value of 3000 to appear. This behavior is completely unpredictable and if you experiment with different list sizes you will end up exposing the problem. The debug() function will count the number of 1000,2000 , and 3000 values in the list when the program is finished. The idea is that we should always have a zero of 0 for the 3000 counter. The unpredictability is called 'non-determinism' and also goes by the name 'race condition' because the result depends on the winner of the critical region. Protecting Critical Regions: Fix the problem by introducing a semaphore to the code. Make sure to run the program multiple times with different sizes of lists to see if the problem has been fixed. A correct solution should still have two threads but there should never be any 3000 values appearing. Note that there is no guarantee that you will see the other values, but you must not see any 3000 values. Lab Report Submission For this lab you will need to submit: - Copy of the lab book. - A small paragraph at the end summarizing the experiment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions