python 作业求助!重奖
本帖最后由 LawranceH 于 2013-08-15 14:10:08 编辑 求助python高手。加我qq:771684767,备注:python作业
要求能看懂英文。总共3个部分,给出第一部分参照下。有兴趣加我qq,给part 1的part1.py文件。
能帮我写出来给予1000元rmb报酬,可充话费。qq详聊。
Part 1
The scheduler
The Scheduler class implements a simple scheduler that runs on Linux (or any Unix). It is not a
real scheduler in the sense that it is invoked automatically by the operating system when a new
process needs to be scheduled. Instead, it is an emulation of a scheduler that runs once a second,
making scheduling decisions.
The scheduler works by sending signals to the processes it schedules. It uses the same job control
signals that the shell uses to suspend jobs and then resume them, e.g., by typing ctrl-Z in bash and
then typing fg. These signals are known as SIGSTOP and SIGCONT. When a process receives the
SIGSTOP signal it pauses, only to resume when it receives the SIGCONT signal. The code in the
scheduler is:
# Suspends the currently running process by sending it a STOP signal.
def suspend(process):
os.kill(process.pid, signal.SIGSTOP)
# Resumes a process by sending it a CONT signal.
def resume(process):
if process.pid: # if the process has a pid it has started
os.kill(process.pid, signal.SIGCONT)
else:
process.run()
def run(self):
current_process = None
while True:
next_process = self.select_process()
if next_process == None: # no more processes
controller_write.write('terminate\n')
sys.exit()
if next_process != current_process:
if current_process:
self.suspend(current_process)
current_process = next_process
self.resume(current_process)
time
class Scheduler():
def __init__(self):
self.ready_list = []
# Add a process to the run list
def add_process(self, process):
#pass # replace with your code
with threading.Lock():
index = 0
for item in self.ready_list:
if item.priority > process.priority:
index = self.ready_list.index(item)
if index == 0:
self.ready_list.insert(index, process)
else:
self.ready_list.insert(index + 1, process)
def remove_process(self, process):
#pass # replace with your code
with threading.Lock():
self.ready_list.remove(process)
# Selects the process with the best priority.
# If more than one have the same priority these are selected in round-robin fashion.
def select_process(self):
#pass # replace with your code
with threading.Lock():
if not len(self.ready_list):
return self.ready_list[0]
else:
return None
# Suspends the currently running process by sending it a STOP signal.
@staticmethod
def suspend(process):
os.kill(process.pid, signal.SIGSTOP)
# Resumes a process by sending it a CONT signal.
@staticmethod
def resume(process):
if process.pid: # if the process has a pid it has started
os.kill(process.pid, signal.SIGCONT)
else:
process.run()
def run(self):
current_process = None
while True:
#print('length of ready_list:', len(self.ready_list))
next_process = self.select_process()
if next_process == None: # no more processes
controller_write.write('terminate\n')
sys.exit()
if next_process != current_process:
if current_process:
self.suspend(current_process)
current_process = next_process
self.resume(current_process)
time.sleep(1)
# need to remove dead processes from the list
try:
current_process_finished = (
os.waitpid(current_process.pid, os.WNOHANG) != (0, 0)
)
except ChildProcessError:
current_process_finished = True
if current_process_finished:
print('remove process', current_process.pid, 'from ready list')
self.remove_process(current_process)
current_process = None