Scheduler
priorityDiscipline.cpp
Go to the documentation of this file.
1 
10 #include "priorityDiscipline.h"
11 
12 #include <algorithm>
13 #include <vector>
14 
15 #include <scheduler/process.h>
16 #include <scheduler/queue.h>
17 
18 using namespace Scheduler;
19 
20 
21 std::shared_ptr<Process> PriorityDiscipline::selectNextTask(Queue *readyQueue, std::shared_ptr<Process> running, unsigned int)
22 {
23  updatePriorities(readyQueue, running);
24 
25  int highestPriority = std::numeric_limits<int>::min();
26  std::shared_ptr<Process> candidate = running;
27  if (running != nullptr)
28  highestPriority = candidate->getPriority();
29  for (auto it = readyQueue->begin() ; it != readyQueue->end(); it++)
30  {
31  if ((*it)->getPriority() > highestPriority)
32  {
33  candidate = (*it);
34  highestPriority = candidate->getPriority();
35  }
36  }
37  return candidate;
38 }
39 
41 {
42  return (trigger == newprocess || trigger == ready);
43 }
44 
45 
46 void PriorityDiscipline::updatePriorities(Queue * readyQueue, std::shared_ptr<Process> running)
47 {
48  std::vector<std::shared_ptr<Process> > ranking(readyQueue->size());
49  unsigned int i = 0;
50  for (auto it = readyQueue->begin(); it != readyQueue->end(); it++, i++)
51  {
52  ranking[i] = (*it);
53  }
54  if (running != nullptr)
55  ranking.push_back(running);
56  std::sort(ranking.begin(), ranking.end(), getComparator());
57  for (unsigned int i = 0; i < ranking.size(); i++)
58  ranking[i]->setPriority(ranking.size()-i);
59 
60 }
61 
63 {
64  return [](std::shared_ptr<Process> a, std::shared_ptr<Process> b){return a->getPriority() > b->getPriority();};
65 }
66 
67 
68 
69 
70 
TriggeringEvent
Definition: eventType.h:16
This class implements the ready queue and the wait queue. Those queues contain processes ready to run...
Definition: queue.h:28
std::shared_ptr< Process > selectNextTask(Queue *readyQueue, std::shared_ptr< Process > running, unsigned int)
select the best task to run at this point.
size_t size() const
Definition: queue.cpp:75
iterator begin()
Definition: queue.cpp:65
virtual ComparatorPointer getComparator()
return a function pointer to a function that provides comparison of tasks&#39; priorities ...
virtual void updatePriorities(Queue *readyQueue, std::shared_ptr< Process > running)
sets the correct priorities to the tasks, especially if dynamic priorities are used.
iterator end()
Definition: queue.cpp:69
virtual bool preempts(TriggeringEvent trigger)
returns true if the argument is a scheduling trigger for this specific discipline ...
bool(* ComparatorPointer)(std::shared_ptr< Process >, std::shared_ptr< Process >)