3.10 Problems

 < Day Day Up > 



3.10 Problems

  1. Modify the producer/consumer problem to include a random wait in the active objects (producer and consumer) between the times they place items in the buffer or take items from the buffer. How does this impact the program?

  2. Modify the producer/consumer program in Problem 1 to have five producers and one consumer. How difficult is this? How does this impact the program? Now change size of the buffer to ten items. How does this affect the program? Create two consumers. How is the program affected?

  3. Modify the BoundedBuffer object to use the relationship between the bufferSize and numberOfItems variables to specify the state of the object. For example, instead of the buffer having a FULL state, it would have a state where "numberOfItems = = bufferSize." What does this say about how the state can be specified in a passive object?

  4. Modify the gas station problem to include two pumps where a customer chooses a pump upon arrival at the station and gets in the line for that pump. Run the simulation where customers randomly choose a pump. Rerun the simulation so that customers can choose a pump based on how many customers are already waiting at each pump. Does the checking of the lines affect the overall wait time? What about the maximum wait time for a customer?

  5. This program is a simulation of an elevator in a building with 6 floors. In this simulation, the elevator is always moving between floors, arriving at a floor, allowing passengers to get on or off the elevator if anyone is waiting for that floor, and then leaving the floor. The elevator takes 1.0 seconds to travel between the floors, and when it reaches the top or bottom floor it reverses direction and continues to run. The 50 passengers will choose floors to get on and get off. The passengers sleep a random amount of time up to 2 minutes and then wait for the elevator to arrive. Once the elevator arrives at the floors they are leaving, they get on and wait for the elevator to arrive at the floors they are going to. When the elevator gets to their destination floors, they get off, and these passengers are no longer considered in the simulation. The design for the elevator is as follows: Of the three types of objects, two objects are active (the elevator and the passenger) and one object is passive (a floor). Because the building has 6 floors, an array of 6 floors must be included in this simulation. The pseudo-code for the elevator and the passenger is given in Exhibit 10, as well as the state diagram for the elevator in Exhibit 11. Implement this elevator simulation.

    Exhibit 10: Pseudo-Code for the Elevator and the Passenger

    start example

     Pseudo-Code for the Elevator floorNumber = 1 increment = 1 forever { floors[floorNumber].elevatorArrives() floors[floorNumber].elevatorLeaves() Sleep for 1 second. if (floorNumber = = 1) increment = 1; else if (floorNumber = = MAX_FLOORS) increment = -1 floorNumber = floorNumber + increment } Pseudo-Code for the Passenger Sleep a random amount of time < 2 minutes. Choose a floorNumber to start at. floors[floorNumber].waitForElevator() floors[floorNumber].getOnOffElevator() Choose a destination floorNumber. floors[floorNumber].waitForElevator() floors[floorNumber].getOnOffElevator() 

    end example

    Exhibit 11: Problem 6: State Diagram for the Floor Object

    start example

    click to expand

    end example

  6. Can a waitForElevator transition from state Loading to state Loading be added safely to the state diagram in Exhibit 11? How does this change the semantics of the program?

  7. The design for the floor object shown in Exhibit 12 uses the same elevator and passenger objects described in pseudo-code in Problem 5. Critique the following elevator design. Can any problems be identified? If so, enumerate them.

    Exhibit 12: Problem 7: State Diagram for the Floor Object

    start example

    click to expand

    end example

  8. Critique the elevator design described in Exhibit 13 and shown in Exhibit 14. Can any problems be identified? If so, enumerate them.

    Exhibit 13: New Pseudo-Code for the Elevator and the Passenger

    start example

     Pseudo-Code for the Elevator floorNumber = 1 increment = 1 forever {   floors[floorNumber].elevatorLeaves()   floorNumber = floorNumber + increment   Sleep for 1 second   floors[floorNumber].ElevatorArrives()   if  (floorNumber = = 1)   increment  = 1;   else if (floorNumber  = = MAX_FLOORS)   increment = -1 } Pseudo-Code for the Passenger Sleep a random amount of time < 2 minutes. Choose a floorNumber to start at. floors[floorNumber].getOnOffElevator() Choose a destination floorNumber. floors[floorNumber].getOnOffElevator() 

    end example

    Exhibit 14: Problem 8: State Diagram for the Floor Object

    start example

    click to expand

    end example

  9. The design for the floor object shown in Exhibit 15 uses the same elevator and passenger objects described in pseudo-code in Exhibit 13. Critique the elevator design. Can any problems be identified? If so, enumerate them.

    Exhibit 15: Problem 9: State Diagram for the Floor Object

    start example

    click to expand

    end example

  10. Modify the elevator simulation in Problem 5 so that the passenger is more realistic. All passengers enter the building at floor 1 and then randomly choose floors to ride between until they come back to floor 1 and leave the building. Make the simulation more realistic by having the passengers all arrive between 8:30 and 9:30 in the morning and leave between 4:30 and 5:30 in the afternoon.

  11. Modify the design in Problem 5 to include two elevators and five elevators. If this is done well, the design using five elevators should require changing only the number of elevators declared and started compared to the design for two elevators.

  12. The barber shop problem is a famous problem in concurrency. This problem models a barber shop with the following characteristics: The barber shop has a single chair. If the barber finds a customer in the chair, the barber cuts the person's hair, which takes some fixed amount of time (5 seconds for this simulation). Customers who come to the barber shop and find the chair taken can wait on a bench in the shop. The next customer to use the barber's chair must be a customer already waiting on the bench. Finally, if a customer comes and finds no room on the bench, he must wait outside the barber shop until a seat on the bench opens up. Run a simulation of this barber shop when 30 customers come in randomly over the course of 2 minutes, and report the average wait time. Then run the simulation with 40 customers randomly arriving over the course of 2 minutes. What is the difference in the result?

  13. Run the barber shop problem with two barbers. What is the result of the simulation?

  14. The dining philosophers problem is a classic problem in concurrency that shows how deadlock can occur. The problem involves five philosophers who are eating Chinese food, and five chopsticks are available. The philosophers sit in a circle, each with one chopstick on the left and one chopstick on the right (they share the chopsticks with each other). To eat, each philosopher picks up the chopstick on the left and then the chopstick on the right, eats, and then puts the chopsticks back down. Write a simulation of this problem.

  15. The dining philosophers is a classic illustration of deadlock. If all the philosophers pick up the chopstick on the left, no one can pick up the chopstick on the right, and they all starve. One way to solve this problem is to create a philosopher who picks up the right chopstick first. Change the simulation in problem 9 to implement this deadlock-free solution. Why is deadlock avoided here?

  16. Another way to solve the dining philosophers problem is to allow only four philosophers to sit at the table at a time, making one wait if the table already has four diners. Write a solution to the dining philosophers problem that uses this design. Why is deadlock avoided?

  17. By reviewing the state transitions in the state diagram, and the possible actions that are taken by the active objects, do you think it is possible to predict the possibility of deadlock before a program is run? Describe an algorithm that would do this.

  18. Explain how deadlock could occur if two passive objects are in an ensemble.

  19. The problem illustrated in Exhibit 16 models Santa Claus at the North Pole. Santa Claus is a very busy man, and he only has time for three things: sleeping, answering elves, or delivering toys. To get his rest, he only wakes up when one of two things is true: (1) at least three elves have questions, or (2) all nine reindeer have been gathered together and hitched to the sleigh so Santa can deliver toys. If Santa is awake and any elves are waiting with questions, then Santa will answer their questions before going back to sleep. If Santa is awake and answering questions when all nine reindeer are ready to leave, it is more important to deliver toys than answer questions, so Santa will deliver the toys and then go back to answering questions from the elves. To simulate the elves, assume that 15 elves are working and they will work a random amount of time between 0 and 100 seconds before answering a question. Assume that the reindeer all wait 200 seconds for a Christmas to arrive and then are made ready to deliver gifts in an additional 0 to 30 seconds. Santa takes 5 seconds to answer each question and 30 seconds to deliver the gifts.

    Exhibit 16: Problem 19: Santa Claus at the North Pole

    start example

    click to expand

    end example

  20. Show a state diagram for the BinarySemaphore class in Chapter 2.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

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