-
Notifications
You must be signed in to change notification settings - Fork 0
/
HardDisks.cpp
116 lines (104 loc) · 2.54 KB
/
HardDisks.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Author: Shania Dhani
* Date Modified May 12, 2020
*
* Hard Disks keeps track of all devices in the simulation and the
* processes that are using the hard disks and the processes waiting
* to use the hard disk.
*/
#include <iostream>
#include "HardDisks.hpp"
// default constructor
HardDisks::HardDisks() {}
// parameterized constructor
HardDisks::HardDisks(const int &total)
{
total_ = total;
for (int i = 0; i < total; i++)
{
IOQueue io_queue;
hard_disks_.push_back(io_queue);
}
}
// set total hard disks
void HardDisks::setTotal(const int &total)
{
total_ = total;
}
// @return hard disks in use by process
std::vector<IOQueue> HardDisks::getHardDiskUsage() const
{
return hard_disks_;
}
// @return total hard disks
int HardDisks::getTotalDisks() const
{
return total_;
}
// @return true if disk number is idle
bool HardDisks::isIdle(const int &disk_number) const
{
if (unsigned(disk_number) < hard_disks_.size())
{
if (hard_disks_[disk_number].onDisk().getPID() == -1)
{
return true;
}
}
return false;
}
// serve process on hard disk number
bool HardDisks::requestDisk(const int &disk_number, const Process &cpu_process)
{
if (unsigned(disk_number) < hard_disks_.size())
{
hard_disks_[disk_number].addProcess(cpu_process);
return true;
}
return false;
}
// end process at disk_number use
bool HardDisks::endDiskUse(const int &disk_number, Process &finished_process)
{
if (unsigned(disk_number) < hard_disks_.size())
{
finished_process = hard_disks_[disk_number].onDisk();
hard_disks_[disk_number].endProcess(); /* end process on hard disk */
return true;
}
return false;
}
// @return process using Hard Disk
Process HardDisks::usingHardDisk(const int &disk_number) const
{
return hard_disks_[disk_number].onDisk(); /* returns pid -1 */
}
// print hard disks snapshot
void HardDisks::printHardDisks()
{
for (unsigned i = 0; i < hard_disks_.size(); i++)
{
IOQueue &p = hard_disks_[i];
std::cout << " HDD ";
std::cout << std::to_string(i) << ": ";
std::cout << ((p.onDisk().getPID() == -1) ? "IDLE" : "P" + std::to_string(p.onDisk().getPID()));
std::cout << " I/O " << std::to_string(i) << ": ";
if (!p.isEmpty())
{
for (unsigned i = 0; i < p.getIOQueue().size(); i++)
{
Process q = p.getIOQueue()[i];
if (i != 0 && q.getPID() != -1)
{
std::cout << " <- "
<< "P" << q.getPID();
}
}
}
else
{
std::cout << "EMPTY";
}
std::cout << std::endl;
}
}