public void Demote(Job j, int origin) { if (origin == this._queues.Length - 1) { this[QUEUE_TIME_16].Enqueue(j); } else { this[origin + 1].Enqueue(j); #if VERBOSE Verbose.Queue(j.Name, this.GetQueueName(origin), this.GetQueueName(origin + 1)); #endif } }
public void Enqueue(Job j) { _jobs.Enqueue(j); }
public void Enqueue(Job j) { this[QUEUE_READY].Enqueue(j); }
public int RetriveJob(ref Job j, ref int queueID) { for (int i = 1; i < this._queues.Length; i++) { if (!this[i].Empty) { j = this[i].Dequeue(); queueID = i; return this[i].ProcessTime; } } return 0; }
/// <summary> /// Worker function for processing jobs in the queue as well as queue scheduling. /// </summary> private static void Worker() { // How long to hold a Job on the worker // before queueing it on the next queue. int holdJobTime = 0; // The Job currently being worked on. Job current = new Job(); // Extra things to track time. int lastTime = workerTime; // ID of the queue where the current job came from. int origin = 0; while (true) { // Check to see if there's anything in the ready queue // that needs to be added on to the 1 process time queue. while (js.QueueOnTime(workerTime)) ; // Check to see if there is a job to be working on // by testing to see if holdJobTime has a value greater than 0. if (holdJobTime == 0 && js.JobAvaliable) { // Retrive for how long the current job should run for and // at what time the job should be moved to the next queue if it hasn't completed yet. holdJobTime = workerTime + js.RetriveJob(ref current, ref origin); #if VERBOSE Verbose.Retrive(js.GetQueueName(origin), current.Name, holdJobTime); #endif } else { // Condition to maintain proper stepping in each // time frame. if (lastTime != workerTime && current.Name != "") { #if VERBOSE Verbose.Process(current.Name, current.RemainingTime); #endif // Process the current job for 1 time unit. current.Process(); // Create output strings Verbose.TimeLine += workerTime + " "; if (workerTime.ToString().Length > current.Name.Length) Verbose.JobLine += AddSpaces(current.Name, workerTime.ToString().Length); else Verbose.JobLine += current.Name + " "; #if !VERBOSE Console.Clear(); // Instruction repeated here so that they will be redrawn with the other lines. Console.WriteLine("Press \'Q\' to exit.\nPRess \'S\' to pause/suspend operations.\nPress \'R\' to resume operations."); Console.WriteLine(Verbose.JobLine); Console.WriteLine(Verbose.TimeLine); #endif // If there isn't any time left for the current job // set the time to hold the job equal to the current time // This will force the next job to come up on the next time frame. if (current.RemainingTime <= 0) holdJobTime = workerTime; // If there are no jobs avaliable, the ready queue is empty, and the // current job has not time left to process, then exit the thread (while loop). if (!js.JobAvaliable && js.ReadyQueueEmpty && current.RemainingTime <= 0) { break; } // The current time as been processed and we move // on to the next time frame. lastTime = workerTime; } // If we're still in the same time frame. else { // If the time to hold the current job for // is equal to the current time then demote the current job to the next queue // if it has more time left to process, otherwise, just reset the time to hold // the job back to 0. if (holdJobTime == workerTime) { if (current.RemainingTime > 0) js.Demote(current, origin); holdJobTime = 0; } } } } threadClock.Abort(); Console.WriteLine("All jobs complete.\nPress Q to exit."); }