Scheduler
queue.cpp
Go to the documentation of this file.
1 
10 #include "queue.h"
11 
12 #include <cassert>
13 #include <iostream>
14 #include <sstream>
15 
16 #include "process.h"
17 
18 using namespace Scheduler;
19 
20 Queue *Queue::readyQueue = nullptr;
21 Queue *Queue::waitQueue = nullptr;
22 
23 void Queue::add(std::shared_ptr<Process> p)
24 {
25  queue.push_back(p);
26  assert(p != nullptr);
27 }
28 
29 
31 {
32  if (!readyQueue)
33  readyQueue = new Queue;
34  return readyQueue;
35 }
36 
38 {
39  if (!waitQueue)
40  waitQueue = new Queue;
41  return waitQueue;
42 }
43 
44 Queue::Queue()
45 {
46 }
47 
48 std::shared_ptr<Process> Queue::remove(std::shared_ptr<Process> p)
49 {
50  if (p == nullptr)
51  return nullptr;
52  for (auto it = queue.begin(); it != queue.end(); it++)
53  {
54  if ((*it) == p)
55  {
56  queue.remove(p);
57  return (p);
58  }
59  }
60  return nullptr;
61 }
62 
63 
64 
66 {
67  return queue.begin();
68 }
70 {
71  return queue.end();
72 }
73 
74 
75 size_t Queue::size() const
76 {
77  return queue.size();
78 }
79 
80 bool Queue::isEmpty() const
81 {
82  return (size() == 0);
83 }
84 
85 
86 void Queue::print() const
87 {
88  std::cout << "The queue contains:\n";
89  for (auto it = queue.begin(); it != queue.end(); it++)
90  {
91  std::cout << " pid "<<(*it)->getPid()<<"\n";
92  }
93 }
94 
95 std::string Queue::getDisplay() const
96 {
97  if (queue.empty())
98  {
99  return "nothing";
100  }
101  std::ostringstream stream;
102  for (auto it = queue.begin(); it != queue.end(); it++)
103  {
104  stream << (*it)->getPid();
105  if ((*it)->isRealTime())
106  stream <<"("<<(*it)->getDeadlineTime()<<")";
107  stream << " ";
108  }
109  return stream.str();
110 }
111 
112 
113 
114 
115 
116 int Queue::getMaxSize() const
117 {
118  return Process::getMaxNumber();
119 }
120 
121 
122 
123 
124 
125 
126 
127 
static unsigned long int getMaxNumber()
returns the maximum number of process that can ever be created
Definition: process.cpp:94
This class implements the ready queue and the wait queue. Those queues contain processes ready to run...
Definition: queue.h:28
void print() const
print a summary of the queue&#39;s content
Definition: queue.cpp:86
int getMaxSize() const
get the maximum size of the queues
Definition: queue.cpp:116
size_t size() const
Definition: queue.cpp:75
std::string getDisplay() const
get the queue&#39;s content as a string, for command-line output
Definition: queue.cpp:95
static Queue * getReadyQueue()
get a pointer to the system&#39;s ready queue
Definition: queue.cpp:30
std::list< std::shared_ptr< Process > >::iterator iterator
Definition: queue.h:52
iterator begin()
Definition: queue.cpp:65
static Queue * getWaitQueue()
get a pointer to the system&#39;s wait queue
Definition: queue.cpp:37
std::shared_ptr< Process > remove(std::shared_ptr< Process > p)
removes the process from the queue. Note: this does not delete the process. Just remove it from queue...
Definition: queue.cpp:48
iterator end()
Definition: queue.cpp:69
void add(std::shared_ptr< Process > p)
add a process to the queue
Definition: queue.cpp:23
bool isEmpty() const
Definition: queue.cpp:80