首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python 作业!重奖

2013-09-06 
python 作业求助!重奖本帖最后由 LawranceH 于 2013-08-15 14:10:08 编辑求助python高手。加我qq:771684767

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()

The resume also starts processes running for the first time by calling process.run, a method in the
SimpleProcess class.
Every second the scheduler checks to see which runnable process has the highest priority. If the
current process still has the highest priority it continues. Otherwise the current process is suspended
and the best priority process is resumed.
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


If there are several processes with the same priority they are scheduled round-robin. e.g., If the
ready queue looks like this
process 1:priority 4 - process 2:priority 4 - process 3:priority 2
Process 1 will be scheduled first; then process 2. These two processes will take turns until they are
no longer runnable. Then process 3 can run. Python Linux
[解决办法]
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


第一部分,正确与否啊?

热点排行