-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocesses.cpp
More file actions
executable file
·156 lines (130 loc) · 6.79 KB
/
Copy pathprocesses.cpp
File metadata and controls
executable file
·156 lines (130 loc) · 6.79 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
Joel Brigida
COP4610-002
Dr. Borijovie Furht
Programming Term Project: Process Scheduler
Description:
This file is the implementation file for Processes Queue Class
*/
#include <iostream>
#include <string>
#include <fstream>
#include "processes.h"
using namespace std;
Process::Process(string the_name) // initialize an empty process
{
//cout << "Default Constructor Called" << endl;
front = back = 0; // set front and back pointers to NULL
proc_name = the_name; // initialize process name to a NULL string
total_wait_time = 0; // initialize total wait time of process to zero
time_finished = 0;
time_response = 0;
nodes_remaining = 0; // set nodes_remaining to 0
total_nodes = 0; // set node_count to 0
}
Process::~Process()
{
//cout << "* Destructor Called *" << endl;
while (!Empty())
{
deQueue(); // delete nodes until the list is empty
}
}
void Process::Make_process(string filename, string obj_name)
{
cout << "Constructing Process from " << filename << endl;
ifstream file_in; // object to read the integers from file
int next_value; // takes value of number read from file
file_in.open(filename);
if(file_in.fail())
{
cout << "Function \'Make_Process\' failed to open input file...exiting program" << endl;
exit(1);
}
while (file_in >> next_value)
{
add_back(next_value, obj_name); // add a node w/ burst time and the process name which it originated.
}
file_in.close();
}
void Process::add_back(const int& time, const string& any_name) // adds a new node to the BACK
{
//cout << "Adding a node to the back..." << endl;
if (Empty())
{
front = back = new node; // declare a new node, set front and back to point to the same new node.
nodes_remaining++; // increment the # of nodes remaining in the queue
total_nodes++; // increment total number of nodes in the queue
front->wait_time = 0; // set initial wait time to zero
front->time_q = time; // add the time value to the new node
front->node_number = 1; // 1st node is always = 1
front->burst_type = 'C'; // 1st node is always a CPU burst
front->name = any_name; // need to know which process this node originated from
front->next = 0; // since it's a single node queue, need to set "next" and "prev" fields to NULL
}
else // if not empty
{
back->next = new node; // declare a new node after the current back node. (default constructor makes *back & *front of new node = NULL already
nodes_remaining++; // increment the # of nodes remaining in the queue
total_nodes++; // increment total number of nodes in the queue
back->wait_time = 0;
back = back->next; // move the back pointer to the new node, which is now in the back
back->name = any_name; // need to know which process this node originated from
back->time_q = time; // add the data to the new node
back->node_number = total_nodes; // new node takes position in the queue as the total # of nodes.
if (total_nodes % 2 == 0)
{
back->burst_type = 'I'; // Even nodes are CPU bursts
}
else // if (total_nodes % 2 != 0)
{
back->burst_type = 'C'; // Odd nodes are I/O bursts
}
back->next = 0; // set the "next" field of the back node to NULL
}
}
void Process::deQueue() // deletes a node from the FRONT
{
//cout << "Deleting front node..." << endl;
if (!Empty())
{
node *p = front; // declare pointer to point to the front node.
front = front->next; // move the front pointer to the next one down the queue
if (front == 0) // if front is NULL after moving pointer, then there are no nodes left in the queue,
{
back = 0; // so set the back pointer to NULL also (as a precaution)
}
delete p; // delete unwanted node.
nodes_remaining--; // decrement the # of nodes remaining in queue
}
else
{
cout << "Cannot deQueue because queue is empty" << endl;
}
}
void Process::Print_proc_nodes(string name_of_proc)
{
cout << "Print_proc_nodes Called" << endl;
node *p = this->front; // declare a pointer to point to the front
cout << "Printing process data for: {" << name_of_proc << "}" << endl;
cout << "proc_name = " << proc_name << endl;
cout << "# nodes remaining: " << nodes_remaining << endl;
cout << "# total nodes created: " << total_nodes << endl;
cout << "total_wait_time: " << total_wait_time << endl;
cout << "Process time_finished: " << time_finished << endl;
cout << "Process time_response: " << time_response << endl;
if (p == 0)
{
cout << "Zero Nodes in Queue...Nothing to print" << endl;
}
else // if (p != 0)
{
while(p != 0) // print the data in each node
{
cout << "(Node #: " << p->node_number << " Name: " << p->name << " Time_q: "
<< p->time_q << " Type: " << p->burst_type << " Wait_time " << p->wait_time << ")" << endl;
p = p->next; // traverse to the next node
}
}
cout << endl;
}