Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal Practice threads synchronization and cooperatively execute some tasks, while solving some related issues like deadlocks and races. Reference: the textbook chapter 4 to 6

Goal
Practice threads synchronization and cooperatively execute some tasks, while solving some
related issues like deadlocks and races.
Reference: the textbook chapter 4 to 6.
Description
Please read the complete assignment document carefully before starting.
Write a program (C Pthread is suggested but you can also use java or windows if you prefer)
which will have to create 4 threads. These 4 threads share some resources and cooperatively
execute some tasks.
The program should have 3 integer variables shared among all threads, int: a,b and turn.
These variables should be the only global variables. Any other needed variables should be
declared as a local variable to each function/thread. The main() thread should ask the user to
enter a zero or positive values: for variables a,b, and turn (use scanf). turn can take only
values from 0 to 3. Then the main() thread should create the 4 theads and t3).
Each thread will run on a different function with the prototype: void *thrn(), where n is the
thread number (0 to 3).
Once started, the thread scheduling should work as follows:
a. The threads will run in round robin (RR)
b. The value of the variable turn define which thread will run first e.g., if turn=2 then
Th2 Th 3 Th 0 Th 1 Th 2dots
c. The value of the variable a define the number of threads RR repetition before
terminating e.g., if a=2 and turn =0 then
Th0Th1Th2Th3Th0Th1Th2Th3.
d. Only 1 thread is allowed to execute its critical section (CS) at a time. E.g., when Th0
is executing, all other threads should be in a busy wait (BW).
e. When an executing thread exit its CS it must set the value of variable turn to the
next Thread to execute.
f. Then main() thread (or the parent thread) should wait (join) after all 4 children
threads to terminate before continuing the execution of its remaining section to
produce some useful data then terminates.
Each thread task is as follows:
a. Th0: calculates b=b+1, print the Thead # and b value for an a repetition
b. Th1: calculates b=b+2, print the Thead # and b value for an a repetition
c. Th2: calculates b=b+3, print the Thead # and b value for an a repetition
d. Th3: calculates b=b+4, print the Thead # and b value for an a repetition
Solve everything thank you
image text in transcribed

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

Students also viewed these Databases questions