Skip to content

Instantly share code, notes, and snippets.

@endeav0r
Created December 5, 2011 19:35
Show Gist options
  • Save endeav0r/1434933 to your computer and use it in GitHub Desktop.
Save endeav0r/1434933 to your computer and use it in GitHub Desktop.
Operating Systems Project 5 Solution
import binascii
import subprocess
import sys
bench_files = ['bench_database.txt',
'bench_kiosk.txt',
'bench_mixed.txt',
'bench_test.txt',
'bench_webapp.txt']
CPU_POLICIES = ['FIRST COME FIRST SERVED',
'SHORTEST JOB FIRST',
'SHORTEST REMAINING TIME FIRST',
'ROUND ROBIN']#, 'HIGHEST RESPONSE RATIO']
MEMORY_POLICIES = ['FIRST IN FIRST OUT',
'LEAST RECENTLY USED',
'SECOND CHANCE CLOCK',
'INCREMENTAL CLOCK']
DISK_POLICIES = ['FIRST COME FIRST SERVED',
'LEAST RECENTLY USED',
'SECOND CHANCE CLOCK',
'INCREMENTAL CLOCK']
QUANTUMS = [1, 2, 4, 8, 16, 32]
FRAMES = [20, 30, 40]
SPEEDS = [8, 12, 16, 20]
DENSITIES = [1, 2, 3]
def simcli (filename, cpu, memory, disk, quantum, frames, speed, density) :
cpu = str(cpu)
memory = str(memory)
disk = str(disk)
quantum = str(quantum)
frames = str(frames)
speed = str(speed)
density = str(density)
command = 'java SimCLI ' + ' '.join([filename, cpu, memory, disk, quantum, frames, speed, density])
print(command)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0].strip()
output_string = output.decode('ascii')
pieces = list(map(lambda x: int(x), output_string.split(',')))
return pieces
pieces = simcli(bench_files[0], 1, 1, 1, 1, 1, 1, 1)
possibilities = (len(CPU_POLICIES) + 1) * (len(MEMORY_POLICIES) + 1) * \
(len(DISK_POLICIES) + 1) * (len(QUANTUMS) + 1) #* \
#(len(FRAMES) + 1) * (len(SPEEDS) + 1) * \
#(len(DENSITIES) + 1)
def bench_file (filename) :
lowest = 1000000000
best_cpu = -1
best_memory = -1
best_disk = -1
best_quantum = -1
best_frame = 10
best_speed = 4
best_density = 1
for p in range(possibilities) :
cpu_policy = p % len(CPU_POLICIES) + 1
p = int(p / len(CPU_POLICIES))
memory_policy = p % len(MEMORY_POLICIES) + 1
p = int(p / len(MEMORY_POLICIES))
disk_policy = p % len(DISK_POLICIES) + 1
p = int(p / len(DISK_POLICIES))
quantum = QUANTUMS[p % len(QUANTUMS)]
#p = int(p / len(QUANTUMS))
#frame = FRAMES[p % len(FRAMES)]
#p = int(p / len(FRAMES))
#speed = SPEEDS[p % len(SPEEDS)]
#p = int(p / len(SPEEDS))
#density = DENSITIES[p % len(DENSITIES)]
frame = best_frame
speed = best_speed
density = best_density
results = simcli(filename, cpu_policy, memory_policy, disk_policy, quantum, frame, speed, density)
result = 0
for r in results :
result += r
if r < lowest :
print('lowest: ' + str(lowest))
lowest = r
best_cpu = cpu_policy
best_memory = memory_policy
best_disk = disk_policy
best_quantum = quantum
best_frame = frame
best_speed = speed
best_density = density
print(filename)
print(CPU_POLICIES[best_cpu])
print(MEMORY_POLICIES[best_memory])
print(DISK_POLICIES[best_disk])
print("QUANTUM: " + str(best_quantum))
print("FRAME: " + str(best_frame))
print("SPEED: " + str(best_speed))
print("DENSITY: " + str(best_density))
print()
if len(sys.argv) > 1 :
bench_file(sys.argv[1])
else :
bench_file(bench_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment