This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simulate.py
58 lines (49 loc) · 2.04 KB
/
Simulate.py
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
import Queue,csv
MaxTime = 300
ContextSwitch = 0
class Process: # Definining Process
def __init__(self, pid, arrival, burst):
self.pid = pid
self.arrival = arrival
self.burst = burst
def __str__(self):
return '|ID:{:3d}| Arrival Time:{:3d}| Burst Time:{:3d}|'.format(self.pid,self.arrival,self.burst)
class Simulator:
def __init__(self, tq):
self.quantum = tq
self.clock = 0
self.list = []
self.ProcessQueue = Queue.Queue()
self.RunQueue = Queue.Queue()
def ProcessAdd(self, pid, arrival, burst):
process = Process(pid, arrival, burst)
self.list.append(process)
def Check(self, clock):
for process in self.list:
if process.arrival == self.clock:
self.ProcessQueue.put(process)
break
def Scheduling(self):
self.timer = 0
for process in self.list:
if process.arrival == self.clock:
self.ProcessQueue.put(process)
break
while self.clock < MaxTime:
self.clock = self.clock + 1
self.Check(self.clock)
if not self.ProcessQueue.empty():
process = self.ProcessQueue.get()
if(process.burst < self.quantum):
self.timer = self.timer + process.burst
bur = process.burst
process.burst = 0
print '|ID:{:3d}| Start:{:3d}| End:{:3d}| Time Left:{:3d}'.format(process.pid, self.timer - bur, self.timer, process.burst)
else:
process.burst = process.burst - self.quantum
print '|ID:{:3d}| Start:{:3d}| End:{:3d}| Time Left:{:3d}'.format(process.pid, self.timer, self.timer + self.quantum, process.burst)
self.timer = self.timer + self.quantum
self.clock = self.clock + 1
self.Check(self.clock)
if not process.burst <= 0:
self.ProcessQueue.put(process)