1. Assume that shared variable x is initially 0 and that threads T1 and T2 are as follows: …

1. Assume that shared variable x is initially 0 and that threads T1 and T2 are as follows:

T1 T2
x = x + 1; x = x – 1;

Don't use plagiarized sources. Get Your Custom Essay on
1. Assume that shared variable x is initially 0 and that threads T1 and T2 are as follows: …
From as low as $9/Page
Order Essay

What are the possible final values of x? (The statements are not inside critical sections.) Show your work.

2. Write a Java program with four threads. Your program should work as follows:
– Each thread performs a simple task which is to output its thread ID, increment a shared variable named count, and display the
resulting value of count. Count is initalized to 0.
– The threads work in pairs. Threads 1 and 2 work as a pair, and Threads 3 and 4 work as a pair.
– The threads in each pair alternate peforming their task. That is, Thread1 performs its task first, then Thread2, then Thread1, then
Thread2, etc. Same for Threads 3 and 4 with Thread 3 going first.
– The thread pairs also alternate. That is, Thread pair 1 and 2 perform their tasks then Thread pair 3 and 4 perform theirs, then
Thread1 and Thread2, then Thread3 and Thread4, etc.
– The user enters on the command line the number of times that the threads should alternate. For example, if the user enters 2, each
thread in each pair performs its work twice, and each thread pair is given 2 chances to execute.
– At then end of execution, display the final value of shared variable count.

You must use counting semaphores to force your threads to alternate.

Sample output for: java Alternate 2

T1:1 // each of T1 and T2 performs its task 2 times, with T1 and T2 alternating
T2:2
T1:3
T2:4
T3:5 // then each of T3 and T4 performs its task 2 times, with T3 and T4 alternating
T4:6
T3:7
T4:8
T1:9 // then each of T1 and T2 performs its task 2 times, with T1 and T2 alternating
T2:10
T1:11
T2:12
T3:13 // then each of T3 and T4 performs its task 2 times, with T3 and T4 alternating
T4:14
T3:15
T4:16
count is 16

Sample output for: java Alternate 3

T1:1
T2:2
T1:3 // each of T1 and T2 performs its task 3 times, with T1 and T2 alternating
T2:4
T1:5
T2:6
T3:7 // then each of T3 and T4 performs its task 3 times, with T3 and T4 alternating
T4:8
T3:9
T4:10
T3:11
T4:12
T1:13 // a 2nd time for pair T1 and T2
T2:14
T1:15
T2:16
T1:17
T2:18
T3:19 // a 2nd time for pair T3 and T4
T4:20
T3:21
T4:22
T3:23
T4:24
T1:25 // a 3rd time for pair T1 and T2
T2:26
T1:27
T2:28
T1:29
T2:30
T3:31 // a 3rd time for pair T3 and T4
T4:32
T3:33
T4:34
T3:35
T4:36
count is 36

I created a template for you in file Alternate.java (attached). Complete this code by completing the run() methods for the threads and defining the necessary semaphores.

The Java countingSemaphore and TDThread classes are in a Java jar file, which can be found in the following zip file:

http://www.cs.gmu.edu/~rcarver/ModernMultithreading/ModernMultithreadingJavaJar.zip

Class countingSemaphore has operations P() and V() (instead of the acquire() and release() operations, respectively, used in textbook.)

The Zip file also contains some directories with sample programs; see the directories with “Semaphores” in their names.

Assuming the jar file is in the same directory as your Alternate.java file:

Compile your program using:

Windows: javac -classpath .;ModernMultithreading.jar Alternate.java

Unix: javac -classpath .:ModernMultithreading.jar Alternate.java

Run your program using:

Windows: java -classpath .;ModernMultithreading.jar Alternate 2 // choose 2 or some other number of iterations

Unix: java -classpath .:ModernMultithreading.jar Alternate 2

If you are having deadlock problems, it may help to run your program with deadlock detection turned on:

Windows: java -classpath .;ModernMultithreading.jar -Dmode=trace -DdeadlockDetection=on Alternate 2

Unix: java -classpath .:ModernMultithreading.jar -Dmode=trace -DdeadlockDetection=on Alternate 2

Deadlock detection will provide helpful debugging information about the cuase of the deadlock. To make this information more readable, it helps to name your semaphores when you create them:

countingSemaphore mutex = new countingSemaphore(1,”mutex”); // initial value is 1, name is “mutex”

The name you supply will be used to identify your semaphore in the debug messages.

The Homework Writings
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All our papers are original and written from scratch. We will email you a plagiarism report alongside your completed paper once done.

Free Revisions

All papers are submitted ahead of time. We do this to allow you time to point out any area you would need revision on, and help you for free.

Title-page

A title page preceeds all your paper content. Here, you put all your personal information and this we give out for free.

Bibliography

Without a reference/bibliography page, any academic paper is incomplete and doesnt qualify for grading. We also offer this for free.

Originality & Security

At thehomeworkwritings.com, we take confidentiality seriously and all your personal information is stored safely and do not share it with third parties for any reasons whatsoever. Our work is original and we send plagiarism reports alongside every paper.

24/7 Customer Support

Our agents are online 24/7. Feel free to contact us through email or talk to our live agents.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

We work around the clock to see best customer experience.

Pricing

Flexible Pricing

Our prices are pocket friendly and you can do partial payments. When that is not enough, we have a free enquiry service.

Communication

Admission help & Client-Writer Contact

When you need to elaborate something further to your writer, we provide that button.

Deadlines

Paper Submission

We take deadlines seriously and our papers are submitted ahead of time. We are happy to assist you in case of any adjustments needed.

Reviews

Customer Feedback

Your feedback, good or bad is of great concern to us and we take it very seriously. We are, therefore, constantly adjusting our policies to ensure best customer/writer experience.