-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.h
82 lines (68 loc) · 2.44 KB
/
scheduler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
//scheduler.h
////////////////////////////////////////////////////
/*This file should not be modified by the students*/
////////////////////////////////////////////////////
#include <iostream>
#include <cassert>
#include <cstdlib>
using namespace std;
/* Thread Structure, to be used to create thread obects
which will be processed by the scheduler. Students should
use their own data structure to store threads which should
be defined in 'myscheduler.h'.*/
struct ThreadDescriptorBlock {
int tid;
int remaining_time;
int arriving_time;
int priority;
};
enum Policy {
FCFS, //First Come First Serve
STRFwoP, //Shortest Time Remaining First, without preemption
STRFwP, //Shortest Time Remaining First, with preemption
PBS //Priority Based Scheduling, with preemption
};
/*The 'Scheduler' class is an abstract class which will be
extended by 'myschedule' class in 'myschedule.h'.
Please note, the minimum number of CPUs allowed is 2.
*/
class Scheduler{
public:
Scheduler(Policy p, unsigned int n) : policy(p), num_cpu(n){
timer = 0;
assert(n >= 1);
CPUs = new ThreadDescriptorBlock*[n]();
}
void Go() //This function initializes the scheduling process once all the threads are created.
{
while(Dispatch())
{
++timer;
for(unsigned int i = 0; i < num_cpu; ++i)
{
if (CPUs[i] != NULL)
{
--CPUs[i]->remaining_time;
if (CPUs[i]->remaining_time == 0) //Prints Thread ID, Finishing Time and Processor Number if a thread has finished executing
{
cout << "Thread ID : " << CPUs[i]->tid << " Finishing Time : " << timer << " CPU No. : " << i << '\n';
CPUs[i] = NULL;
}
}
}
}
cout << "All the Threads have been executed !! .. Exiting Scheduler ..\n";
system("pause");
}
///Dispatch schedules the next thread to execute based on scheduler policy and current work load
virtual bool Dispatch() = 0;
///Method to create a new thread with initial conditions as defined.
///all threads must be called before the test bench invokes the Go() method to start simulation of the scheduler.
virtual void CreateThread(int arriving_time, int remaining_time, int priority, int tid) = 0;
protected:
const Policy policy; //To denote the scheduling policy
unsigned int num_cpu; //To denote number of processors
ThreadDescriptorBlock **CPUs; //Pointer array to let processors access the threads
int timer; //Global Timer for all the processors, single increment in timer equals one cpu cycle.
};