Repository navigation
Expand file tree
/
Copy pathpcb.py
More file actions
286 lines (234 loc) · 9.78 KB
/
Copy pathpcb.py
File metadata and controls
286 lines (234 loc) · 9.78 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Author: Anna Cristina Karingal
# Name: pcb.py
# Created: February 27, 2015
# Last Updated: May 11, 2015
# Description: Class for the PCB (Process Control Block) that contains and
# sets all information about a process, its state and any
# parameters passed to it by a system call
from __future__ import division
import sys
from functools import total_ordering
from math import floor, ceil
import io
param_fields = ["file","log", "phys" ,"rw","len", "cyl"]
@total_ordering
class PCB:
def __init__(self, id_num, size, pages, page_size, alpha, tau, loc="ready"):
"""
Initialize with new pid & location, empty system call params.
Calculate next burst based on given history parameter alpha and inital
burst estimate tau.
"""
self.pid = id_num
self.proc_loc = loc
self.proc_size = size
self.pg_size = page_size
# Set params & burst history
self.params = dict.fromkeys(param_fields)
self.alpha = alpha
self.burst_history = []
self.last_est_burst = tau
self.next_est_burst = tau
self.curr_burst = 0
# Set up empty page table
self.page_table = dict.fromkeys(range(pages))
def set_proc_loc(self, p_loc):
""" Sets location of process, i.e. which queue/device it is in"""
self.proc_loc = p_loc
def get_proc_size(self):
return self.proc_size
## Methods to print out contents/properties of PCB
def __repr__(self):
return "process #" + str(self.pid)
def __str__(self):
return "process #" + str(self.pid)
def status(self):
""" Prints which queue/device process is currently in """
if self.proc_loc.lower()== "cpu" or self.proc_loc.lower() == "job pool":
return "{a!s} is in {q!s}".format(a = str(self).capitalize(), q = self.proc_loc.upper())
else:
return "{a!s} is in {q!s} Queue".format(a = str(self).capitalize(), q = self.proc_loc.upper())
def snapshot(self):
"""
Prints PCB attributes and any current system call parameters in a
formatted fashion, on a single line
"""
print "{:<3}".format(str(self.pid)),
for key, val in self.params.iteritems():
if key == "log" or key == "phys":
print"{:^{w}}".format(hex(int(val)) if val else ("--" if self.proc_loc.lower()== "cpu" or self.proc_loc.lower() == "ready" else "0x0"), w=len(key)+2),
else:
print"{:^{w}}".format(str(val)[:6] if val else "--", w=len(key)+2),
print "{:^5}".format(str(int(self.avg_burst_time()))),
print "{:^5}".format(str(sum(self.burst_history))),
print "{:^6}".format(str(self.proc_size)),
self.display_page_table()
def display_page_table(self):
l = 0
for page,frame in self.page_table.iteritems():
l += 1
if l > 1:
print "{:^8}{:^8}".format(hex(page),hex(frame)).rjust(76)
else:
print "{:^8}{:^8}".format(hex(page),hex(frame))
print ""
def headers(self):
"""
Prints name of system call parameters and PCB attributes in formatted
fashion, on a single line
"""
print "{:<4}{:^30}|{:^5}|{:^11}|{:^25}".format("", "FILE", " DISK", "CPU BURST", "MEM")
print "{:<4}".format("PID"),
for key,val in self.params.iteritems():
print"{:<{w}}|".format(str(key).replace("_"," ").capitalize()[:10], w=len(key)+1),
print "{:^4}|{:^5}|{:^6}|{:^8}|{:^8}".format("Avg", "Tot", "Size","Page", "Frame")
## Methods to compare PCBs
def __eq__(self, other):
"""
Compares equality based on different parameters that depend on which
queue PCB is in.
If in ready queue, compare based on next estimated burst time.
If in disk drive queue, compare based on requested cylinder.
Else, compare by PID.
"""
# If process is in ready queue or CPU, compare using next est burst
if self.proc_loc.lower()[0] == "r" or self.proc_loc.lower() == "cpu":
return self.next_est_burst == other.next_est_burst
# If process is in disk drive, compare using cylinder number
elif self.proc_loc.lower()[0] is "d":
return self.params["cyl"] == other.params["cyl"]
# If process is in job pool, compare using process size
elif self.proc_loc.lower()[0] is "j":
return self.proc_size == other.proc_size
# All other locations, compare using PID
else:
return self.pid == other.pid
def __lt__(self, other):
"""
Compares PCBs based on different parameters depending on which queue PCB
is in.
All other comparisons (>, <= and >=) are generated by
functools/total_ordering
"""
# If process is in ready queue or CPU, compare using next est burst
if self.proc_loc.lower()[0] == "r" or self.proc_loc.lower() == "cpu":
return self.next_est_burst < other.next_est_burst
# If process is in disk drive, compare using cylinder number
elif self.proc_loc.lower()[0] is "d":
return self.params["cyl"] < other.params["cyl"]
# If process is in job pool, compare using process size
elif self.proc_loc.lower()[0] is "j":
return self.proc_size < other.proc_size
# All other locations, compare using PID
else:
return self.pid < other.pid
## Calculating burst times
def calc_next_est_burst (self):
"""
Calculates next estimated burst time based on previous estimated burst
time, last recorded burst time, and history parameter alpha
"""
self.next_est_burst = (self.burst_history[-1] * (1-self.alpha)) + (self.last_est_burst * self.alpha)
self.last_est_burst = self.next_est_burst
def record_burst_time(self, burst):
"""
Updates burst history, total CPU time and calculates next estimated
burst time with given input
"""
self.curr_burst += burst
self.burst_history.append(self.curr_burst)
self.calc_next_est_burst()
def update_burst_time(self, elapsed):
"""
Given an elapsed amount of time, updates current CPU burst time used
and next_est_burst based on how much time elapsed.
"""
# Update current burst record with elapsed CPU time
self.curr_burst += elapsed
# Next est burst cannot be less than 0
if self.next_est_burst - elapsed >= 0:
self.next_est_burst -= elapsed
else:
self.next_est_burst = 0
def avg_burst_time(self):
"""
Returns average burst time for each CPU burst
"""
return sum(self.burst_history) / len(self.burst_history) if self.burst_history else 0
def tot_burst_time(self):
"""
Returns total of all CPU bursts
"""
return sum(self.burst_history)
def clear_curr_burst (self):
"""
Resets current bust time to zero
"""
self.curr_burst = 0
## Allocating Memory
def allocate_memory(self, page, frame):
if page in self.page_table:
self.page_table[page] = frame
else:
raise IndexError
## Setting/clearing system call params for pcb
def set_syst_call_params(self):
"""
Sets system call params for file name & starting memory location
"""
self.params["file"] = raw_input("File Name >>> ")
set_loc = False
while not set_loc:
l = io.get_valid_hex("Starting Memory Location in Hex")
if l < self.proc_size:
set_loc = True
offset = int(l % self.pg_size)
pg = int(floor(l / self.pg_size))
self.params["log"] = l
self.params["phys"] = (self.pg_size * self.page_table[pg]) + offset
else:
print io.err("Invalid starting memory location")
def set_read_write_params(self, dev_type):
"""
Sets system call params for read/write and file length (if write)
"""
if (dev_type.lower() == "printer"):
self.params["rw"] = "w"
else:
while self.params["rw"] == None:
rw = raw_input("Read or Write? >>> ")
if rw.lower() in ["r", "read"]:
self.params["rw"] = "r"
elif rw.lower() in ["w", "write"]:
self.params["rw"] = "w"
else:
print io.err("Invalid read/write parameters")
print "Please enter either 'r', 'read', 'w' or 'write'"
if self.params["rw"] == "w":
set_len = False
while not set_len:
l = io.get_valid_int("File Length")
if l + self.params["log"] <= self.proc_size:
self.params["len"] = l
set_len = True
else:
print io.err("Invalid length (too long)")
def set_cylinder_params(self, max_num_cylinders):
"""
Prompts user for which disk drive cylinder to access, validates
input and sets appropriate system call parameter.
Precondition: Process is in disk drive
"""
while self.params["cyl"] == None:
c = io.get_valid_int("Cylinder")
if c > max_num_cylinders:
print "Invalid cylinder number. Please try again."
else:
self.params["cyl"] = c
def clear_params(self):
""" Clears all system call & read/write params """
for p in self.params:
self.params[p] = None