18.13 HOMEWORK


18.13 HOMEWORK

  1. Write a clock program that displays an analog clock in which the second hand is updated every one second and the minute and the hour hands accordingly. Implement the timer part of the program as a thread that wakes up after every one second and gets the new time from the system. This thread can be a daemon thread. (A footnote in Section 18.10 explains what a daemon thread is.) For this exercise, you could declare your timer thread to be a daemon thread by

         Clock c = new Clock();     Thread t = new Thread(c);     t.setDaemon(true); 

    assuming that the Clock object c is of type Runnable.

  2. Write a multithreaded C++ program that solves the famous Dining Philosophers Problem. Your program should demonstrate how thread deadlock and starvation can be avoided by using mutex locks in conjunction with condition variables. The statement of the dining philosophers problem is:

    Five philosophers are sitting around a round table, with one chopstick between each pair of philosophers. Each philosopher alternates between two states, THINKING and EATING, both for random periods of time. Suppose we represent each philosopher with one thread, the goal in solving this problem is to come up with a scheduling strategy so that all the philosophers get to eat for roughly the same time.

    Obviously, only two philosophers can eat at one time. Moreover, a philosopher can eat only if both his/her two neighbors are not eating. Additionally, if it should happen that each philosopher has picked up one chopstick and is waiting for the other chopstick to be put down by a neighbor, we will have a deadlock. To see how we could get into a starvation situation for one of the philosophers, while only two philosophers can eat at any given time, it cannot be two adjacent philosophers. Let's say the philosophers are indexed 0, 1, 2, 3, and 4. Let's also say that 0 and 2 are eating. Let's assume that after 0 and 2 are done, 1 and 3 switch into the eating state. Now if after 1 and 3 are done eating, 0 and 2 resume eating, and so on back to 1 and 3, the philosopher 4 would end up starving.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net