Skip to content

Commit

Permalink
Testiranje os_sleep al je preciznost 3 sekunde (bice bolje nadam se)
Browse files Browse the repository at this point in the history
  • Loading branch information
NatasaKovacevicCoconut committed Mar 30, 2024
2 parents 0fa0bd1 + 3586add commit d77a159
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"src"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
4 changes: 3 additions & 1 deletion linkovi.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
https://afrodita.rcub.bg.ac.rs/~dmilicev/publishing/OS1/Dragan%20Milicev%20-%20Operativni%20sistemi%201%20-%20predavanja%202020%20-%20Deo%20III/#92 slajdovi 52-55

http://os.etf.rs/OS2/predavanja/ slajdovi 2 - 26
http://os.etf.rs/OS2/predavanja/ slajdovi 2 - 26

https://github.com/lazar2222/Operating-Systems-2-Project
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
Binary file removed src/__pycache__/cpu.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/cpuScheduler.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/process.cpython-310.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions src/algorithmPlanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def getProcess(self):

def putProcess(self, proc):
#proc.size = field(compare=False)
self.priorityQueue.put((-proc.size, proc))
self.priorityQueue.put((proc.size, proc))

def getNumberOfProcesses(self):
return self.priorityQueue.qsize()

return self.priorityQueue.qsize()
6 changes: 2 additions & 4 deletions src/cpu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import time
import cpu
from operatingSystem import OS

class CPU:
_instance = None
Expand All @@ -17,10 +15,10 @@ def run(self):
while self.os.hasProcesses():
proc = self.os.getProcess()
if not proc:
time.sleep(1)
time.sleep(0.001)
else:
if proc.remainingIterations > 0:
print("Process [", proc.id, "] is running.", sep='')
print("{:.3f}".format(time.time()), ": Process [", proc.id, "] is running.", sep='')

time0 = time.time()
stop = False
Expand Down
2 changes: 0 additions & 2 deletions src/cpuScheduler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import algorithmPlanner

class CPUScheduler:
def __init__(self, prioritizedAlgorithm):
self.prioritizedAlgorithm = prioritizedAlgorithm
Expand Down
1 change: 0 additions & 1 deletion src/operatingSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ def sleep(self, proc):

def finishProcess(self, proc):
print("Process [", proc.id, "] is finished.", sep='')
pass

2 changes: 1 addition & 1 deletion src/process.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Process:
_idCounter = 0
def __init__(self, iterationNumber, size, sleepInterval, breakpoint):
def __init__(self, iterationNumber, size, sleepInterval):
self.remainingIterations = iterationNumber
self.sleepInterval = sleepInterval
self.size = size
Expand Down
42 changes: 42 additions & 0 deletions src/test_algo_planner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from algorithmPlanner import *
from process import Process
from operatingSystem import OS
from cpu import CPU

def test_queue_order():
cpu = CPU()
os = OS(cpu, FirstComeFirstServe())
cpu.setOS(os)

p1 = Process(5, 30, 2)
p3 = Process(6, 20, 2)
p2 = Process(3, 25, 1)

os.createProcess(p1)
os.createProcess(p2)
os.createProcess(p3)


assert os.getProcess() == p1
assert os.getProcess() == p2
assert os.getProcess() == p3

def test_priority_queue_order():
cpu = CPU()
os = OS(cpu, ShortestProcessFirst())
cpu.setOS(os)

p1 = Process(5, 30, 2)
p2 = Process(6, 20, 2)
p3 = Process(3, 25, 1)

os.createProcess(p1)
os.createProcess(p2)
os.createProcess(p3)


assert os.getProcess() == p2
assert os.getProcess() == p3
assert os.getProcess() == p1

40 changes: 40 additions & 0 deletions src/test_os_sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from algorithmPlanner import *
from process import Process
from operatingSystem import OS
from cpu import CPU
import time

def test_os_sleep():
cpu = CPU()
os = OS(cpu, FirstComeFirstServe())
cpu.setOS(os)

time1 = 1.0
time2 = 2.0
time3 = 3.0

p1 = Process(5, 30, time1)
p3 = Process(6, 20, time2)
p2 = Process(3, 25, time3)

os.createProcess(p1)
os.createProcess(p2)
os.createProcess(p3)

dt = 3.0

t1_0 = time.time()
os.sleep(p1)
t1_1 = time.time()
assert abs(t1_1 - (t1_0 + time1)) <= dt

t2_0 = time.time()
os.sleep(p2)
t2_1 = time.time()
assert abs(t2_1 - (t2_0 - time2)) <= dt

t3_0 = time.time()
os.sleep(p3)
t3_1 = time.time()
assert abs(t3_1 - (t3_0 - time3)) <= dt

0 comments on commit d77a159

Please sign in to comment.