程序代写代做代考 Java html Hive algorithm concurrency Thread Synchronisation/Semaphores

Thread Synchronisation/Semaphores
Operating Systems Lab Class 6
In this lab session we will look at the problem of synchronising the access of multiple threads to a shared resources. In part(a) you will learn how to use synchronized, wait and notify, while in part(b) you will learn how to use Semaphores to solve this problem.
1 Shared Resources Problem
Assume that a finite number of resources of a single resource type must be managed. For example, many commercial software packages provide a given number of application licenses, indicating the number of applications that may be run at any given time. When the application is run, the license count is decremented, and when it is no longer in use, the count is incremented. When all licenses are in use, a new instance of the application cannot be started until a previous session has returned a license.
We are going to write a program in which multiple threads have to coordinate their getting and returning of resources to the shared resources in order to avoid inconsistencies. A thread should block if there are not sufficient resources available.
2 Getting Started
In this lab, we are going to prevent race conditions among shared resources by using two ways: • Part (a) The wait, notify, and notifyAll methods to coordinate thread communication. • Part (b) Semaphores: acquire and release.
Part (a)
1. Download the os-lab6a.zip archive from Canvas and unzip it into your workspace.
2. Start NetBeans and open the project. The project should contain four files in the src direc-tory: Resources.java (contains the main method), ResourceManager.java, GetTask.java and ReleaseTask.java.
3. Before you begin, read through the code and then compile and run it before making any modifications. This should work without problems.
2.1 Implementation 1
4. The program will work as follows:
1

• The program takes three arguments: maxResources, numberOfResourcesToGet and num- berOfResourcesToRelease. For example, this input 3 5 2 means that the maximum number of available resources is 3, the number of resources that will be taken is 5, and the number of resources will be returned is 2. Hence, in this example the number of remaining resources is 0. Running this example, java Resources 3 5 2 should output
maxResources=3 , numberOfResourcesToGet=5 , numberOfResourcesToRelease=2 (get one resource) Thread-0 ==> available resources=2
(get one resource) Thread-1 ==> available resources=1
(get one resource) Thread-2 ==> available resources=0
Thread-3 is waiting
Thread-4 is waiting
(release one resource) Thread-5 ==> available resources=1 (release one resource) Thread-6 ==> available resources=2 (get one resource) Thread-4 ==> available resources=1 (get one resource) Thread-3 ==> available resources=0 Still available resources=0
Still used resources=3
• In the first step, complete Resources.java to parse the command line arguments and create the threads that run ReleaseTask and GetTask; also implement GetTask.java and ReleaseTask.java; but do not yet implement ResourceManager.java.
• Notethatwhenathreadwishestoobtainanumberofresources,itinvokesthedecreaseCount() method. Similarly, when a thread wants to return a number of resources, it calls increaseCount().
2.2 Reflection
5. Run this program several times with different inputs and observe the output. What do you observe? Explain what is happening and why.
2.3 Implementation 2
6. Complete now the remaining classes:
• Fill in the gaps in ResourceManager.java.
You find hints about the possible algorithm in the comment blocks of the locking func- tions. Your implementation will require the use of the wait() and notify() (respectively notifyAll()) methods as defined in the java.lang.Object class. Look at the Java API to understand what they are doing. Also read about synchronized methods.
• Test your program with different inputs.
7. Extra question: Identify the race condition and the data involved with it? What are the other
mechanisms that can be used to fix the race condition?
Part (b) Semaphores
Semaphore is a synchronisation tool which can be used to deal with the critical-section problem. Semaphores are used to manage and protect access to shared resources. Semaphores are very similar to Mutexes. Whereas a Mutex permits just one thread to access a shared resource at a time, a semaphore can be used to permit a fixed number of threads to access a pool of shared resources, as shown in Figure 1.

Figure 1: Semaphore
Before accessing the resource, a thread must acquire a permit from the semaphore. After finishing with the resource, the thread must return the permit back to the semaphore. A task acquires a permit by invoking the semaphore’s acquire() method and releases the permit by invoking the semaphore’s release() method. Once a permit is acquired, the total number of available permits in a semaphore is reduced by 1. Once a permit is released, the total number of available permits in a semaphore is increased by 1.
2.4 Implementation 3
8. Download the os-lab6b.zip archive from Canvas and unzip it into your workspace.
9. Now, follow the same instructions as in Implementation 1 and Implementation 2. You need to complete the same classes (the main class is now in ResourcesSemaphore.java) but now by using Semaphores.
10. Extra question: Show that your solution satisfies all three requirements for the critical-section problem (Mutual exclusion, Progress and Bounded waiting).
3 Java API References
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
http://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Semaphore.html