CS 3100
Fall 2006
Programming Assignment 4
Due Friday, December 8, 2006
'Disk Scheduling' Program
Based on an assignment created by Dr.  Sarraille.


Writing this program will help you get an appreciation for applications of priority queues, introduce you to an interesting algorithm, and give you additional experience with designing data structures.


Write a program that simulates a (simplified) disk scheduler executing the "sweep algorithm" (aka the "elevator algorithm"). The program will keep track of the position & direction of the disk arm, respond to commands to enqueue & serve track requests, and respond to commands to print out the contents of the queues & state variables.


In a textbook written by authors Daniel F. Stubbs and Neil W. Webre the sweep algorithm is explained this way:

"Suppose that we let the disk arm sweep across the disk from the inner to outer tracks. Any requests that are in front of the head in the direction that it is traveling will be served using an SSTF [shortest seek time first] strategy. The head sweeps across the disk in one direction, stopping at each track that has an I/O request pending. It 'ignores' any requests that fall behind it. When it has satisfied the last request that it finds in front of it, it turns around and starts a reverse sweep from outer tracks to inner tracks, again serving any requests that it finds in front of it, using an SSTF strategy. ...

... The set of requests ahead of the read/write head in the direction of the current sweep is a priority queue based on track number. The set of requests behind the head is also a priority queue, but one that will not be served until the head turns about and begins to sweep in the opposite direction. The priorities of the second queue are inverted on track number. If higher (lower) track numbers were high priority in the inner-to-outer sweep, then lower (higher) track numbers would be given higher priority in the outer-to-inner sweep. Regardless of the details, conceptually we have two separate priority queues."

[Source: Stubbs & Webre; "Data Structures with Abstract Data Types and Pascal;" 2nd edition, pp. 103-4; Brooks/Cole Publishing, 1989; ISBN 0-534-09264-0.]


  1. The disk we are simulating has only one surface and ten tracks numbered from 0 to 9.

  2. When the program starts the disk arm is initially positioned at track 0 and the scheduler is set to move the arm in the forward direction: from track 0 towards track 9.

  3. The program uses two priority queues (implemented as heaps) to save pending requests.

  4. The two priority queues are operated as described by Stubbs and Webre in the quotation above.

  5. Assume there will be no need to store more than 20 items in either of the priority queues.

  6. The scheduler serves from the forward priority queue when set to move in the forward direction and serves from the reverse priority queue when set to move in the reverse direction,

  7. The program has to carefully handle new requests for service on the track currently occupied by the disk arm. It must not be possible for continual requests of that type to indefinitely postpone further movement of the arm,

  8. To facilitate ordering elements of priority queues the program will maintain a counter T used as a timestamp. The program will increment T after placing an item in either of the priority queues. The items will have a field for the number of the requested track, and also a field for the value of T at the time of the request. The items in the priority queues will be ordered so that when there is more than one request in the queue for the same track the requests will be served in first-come-first-served order.
You can use this code for Heap.h, Heap.cpp, PQ.h, PQ.cpp.


The program inputs a series of commands from standard input. The commands are described in detail below.

The program must not expect any other input.


After reading each command the program echoes the command to standard output. It then executes the command and prints an appropriate response to the command on standard output.

The program also writes separator strings to standard output to separate the outputs corresponding to each input command.

The program must not write output of any other kind.


Input to the program will consist of a series of commands in the following forms.

Note: the argument <trackNum> stands for an integer VARIABLE; 0 <= trackNum <= 9.

Command names given below, like EnqRequest, are literals and will appear in the input file exactly as they appear below.

Here is the list of things you have to turn in:
  1. At the start of class on the due date, place the following items on the "counter" in front of me:

    Make sure that all of the code and script content shows on the paper. Make sure all content is plainly readable and properly formatted.

  2. Upload to the homework submission system before midnight on the due date:

    Your final source code (example format) and your test script. I will compile and test your programs on the suns in the lab.