Problem 005

Problem

Description

Work Queue

Being the awesome researcher that he is [1], the course instructor has added priority scheduling to the Work Queue system used by the Cooperative Computing Lab. In the old version, jobs were queued up and executed in first-in-first-out (FIFO) order. Noticing that some jobs may require more immediate attention than others, the instructor has modified the submit job command to take into account the priority level of the submitter, and thus insert jobs with higher priority towards the front of the queue.

Of course, like all new software, there are some kinks to work out [2]. To aid in debugging this new priority-based work queue, you are to visualize the state of the work queue at each event time step. The work queue log consists of the following types of events:

  1. Submit <owner> <jobid>:

    This will add job A to the work queue with the priority associated with the user pbui. In this system, the lower the number the higher the priority. If two jobs have the same priority, then FIFO semantics shall take place. Since there should never be more than 26 jobs in the queue at once, jobids are simply the letters of the alphabet.

  2. Run:

    This will remove the job on the front of the queue and run it whenever the necessary resources are available.

  3. Retry <jobid>:

    This will re-insert job A into the queue. Since this is a retry (most likely due to node failure), it should be moved the front of the queue for immediate execution.

  4. Evict <jobid>:

    This will remove job A from the queue if it is there. Since jobids are unique, only one job is removed at a time.

Input

Here is an example of the a event queue log:

*Users*
pbui        2
dthain      0
hbui        1
cmoretti    1
rconnaug    3
*Events*
Submit  pbui        A
Submit  dthain      B
Submit  rconnaug    C
Submit  hbui        D
Run
Submit  pbui        E
Evict   A
Run
Retry   B
Run
Run
Retry   B

The first part of the log denotes the users and their priority. The second part is the list of work queue events.

Output

Here is the output for the event queue above:

WQ[0001] = [ A ]
WQ[0002] = [ B A ]
WQ[0003] = [ B A C ]
WQ[0004] = [ B D A C ]
WQ[0005] = [ D A C ]
WQ[0006] = [ D A E C ]
WQ[0007] = [ D E C ]
WQ[0008] = [ E C ]
WQ[0009] = [ B E C ]
WQ[0010] = [ E C ]
WQ[0011] = [ C ]
WQ[0012] = [ B C ]
[1]That's why I got my Master's so fast... oh wait.
[2]Normally my code is perfect, but the sake of this problem, I'll pretend that I'm fallible.

Solution

Approach

Implement a priority queue and insert and remove items as specified. Take care when inserting to keep track of retries and the actual priority queue.

Input/Output

Source Code