

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


April 9 The purpose of Project3 is to gain experience controlling concurrent processes with semaphores. Write a concurrent simulation of the Dining Philosophers problem. You may use Ada or Java. Each of the five philosophers must be represented by an Ada task or a Java thread (use a philosopher task type in Ada or a philosopher thread class in Java). Semaphores must be used exclusively for synchronization and mutual exclusion (go to the web site and use the code in AdaSemaphore.doc or JavaSemaphore.doc). Each philosopher should be assigned a number in the range 0..4. This can be done using a parameter in the Ada task type or a parameter in the Java constructor. NOTE: You may NOT use the Ada rendezvous nor the protected type nor the Java monitor in this project, except as provided in the Semaphore implementations. In the simulation, each philosopher thinks for a time period and sleeps for a time period. Each philosopher should terminate after completing 10 think/eat cycles. Thinking times should be uniformly distributed between 2 and 10 seconds, and eating times should be uniformly distributed between 1 and 4 seconds. To obtain uniformly distributed times, you will need to use a random number generator. Java has a predefined random() method in the Math class. Since Ada has no such predefined method, your Ada program should execute the pseudo-random number-generator code below within each philosopher task whenever you need to obtain the next random number (Seed : Integer and Random : Float are should be local variables within the philosopher task ): loop Seed := (13849 + 27181Seed ) mod 65536; exit when Seed /= 0; end loop; Random := Float( Seed ) / 65536.0; -- The value of Random is between 0.0 and 1. Make sure each Ada philosopher starts with a different Seed value to assure that each philosopher behaves independently. An initial value for Seed can be provided with a second parameter in the Ada task type. In both Ada and Java, once a random number random between 0.0 and 1.0 is obtained, a statement such as _time = 1.0 + random3.0_ would give a time uniformly distributed in the range 1.0 to 4.0. Eating and thinking times should be simulated using Ada's "delay" statement or Java’s “sleep” method. In Ada, delay time needs to be of type Duration (simply use: delay Duration( time )). Output should be directed to the screen and should record the activity of the five philosophers. Writing to output should only be done under mutual exclusion. Allocate an additional semaphore
for this purpose. Do not use the protected type in Ada. See appendix at end for information on formatting numerical output in Ada. Philosopher activities to be noted in output are: Think, WaitOnRoom, WaitLfFork, WaitRtFork, Eat, and Terminate. The start of each activity should be recorded on a separate line of output together with the start time and length of time to wait (if applicable). The activity of a particular philosopher should appear in a column for that philosopher. See the sample output at the end of this discussion for the required format. The time of day should be displayed in the first column in hour:minute:second format. To obtain the time of day in Ada, use the function Clock in package Calendar for the time of day in seconds (Seconds := Natural( Calendar.Seconds( Calendar.Clock ) ) ), and then calculate the hour, minute, and second from midnight. You will need with Ada.Calendar; at the top of your program. In Java, use the Date class. Sample output SIMULATION OF THE PROBLEM OF THE DINING PHILOSOPHERS Time Phil #0 Phil #1 Phil #2 Phil #3 Phil #4 WaitAmt
15:35:12 WaitRtFork 15:35:12 WaitOnRoom 15:35:12 Eat 2. 15:35:12 Think 9. 15:35:12 WaitLfFork 15:35:12 Eat 1. 15:35:14 Think 2. 15:35:14 Eat 3. 15:35:14 Think 4. 15:35:16 WaitOnRoom 15:35:16 WaitLfFork 15:38:18 WaitRtFork 15:35:18 Eat 2. 15:35:18 Think 6. 15:35:19 WaitOnRoom 15:35:19 WaitLfFork 15:35:19 WaitRtFork 15:35:19 Eat 1. 15:35:21 Think 3. 15:35:21 WaitRtFork 15:35:21 Eat 2. 15:35:21 Think 3. Submit