ダイナミックパーティション割り当て方式のシミュレーション-python実装


1 (1) (2) 。 2 (1) alloc() free()。 , : , 。 (2) , 1280KB, : • 1 130KB。 • 2 60KB。 • 3 100KB。 • 2 60KB。 • 4 200KB。 • 3 100KB。 • 1 130KB。 • 5 140KB。 • 6 60KB。 • 7 50KB。 • 6 60KB。 , , 。 * c , python, python

 
  
#!/usr/bin/env python 
#coding=utf-8
import copy
class node(object):
    def __init__(self,start,end,length,state=1,ID=0):
        self.start=start
        self.end=end
        self.length=length
        self.state=state    ##state 1:     
        self.Id=ID       ##ID 0    ,       

def showList(list):
    print ("      ").decode('utf-8').encode('gb2312')
    id=1
    for i in range(0,len(list)):
        p=list[i]
        if p.state == 1:
            print (id,' :start ', p.start, " end ", p.end, " length ", p.length)
            id += 1
#      
def alloc1(taskID,Tasklength,list):
    for i in range(0,len(list)):
        p=list[i]
        if p.state==1 and p.length>Tasklength:
            node2 = node(p.start + Tasklength, p.end, p.length - Tasklength, 1, 0)
            a = node(p.start, p.start + Tasklength - 1, Tasklength, state=0, ID=taskID)
            del list[i]
            list.insert(i,node2)
            list.insert(i,a)
            showList(list)
            return
        if p.state==1 and p.length==Tasklength:
            p.state=0
            showList(list)
            return
    print "      "

def free1(taskID,li):

    for i in range(0,len(li)):
        p=li[i]
        if p.Id==taskID:
            p.state=1
            x=i
            break
    #       
    if x-1>0:
        if li[x-1].state==1:
            a=node(li[x-1].start,li[x].end,li[x-1].length+li[x].length,1,0)
            del li[x-1]
            del li[x-1]
            li.insert(x-1,a)
            x=x-1
    #       
    if x+1 Tasklength:
            s = p.start
        elif p.state == 1 and p.length == Tasklength:
            ss12 = p.start
    if s == -1 and ss12 == -1:
        print "      "
        return
    for i in range(0, len(li)):
        p = li[i]
        if p.start==s:
            node2 = node(p.start + Tasklength, p.end, p.length - Tasklength, 1, 0)
            a = node(p.start, p.start + Tasklength - 1, Tasklength, state=0, ID=taskID)
            del li[i]
            li.insert(i, node2)
            li.insert(i, a)
            showList(li)
            return
        elif p.start==ss12:
            p.state = 0
            showList(li)
            return

a=node(0,639,640,state=1,ID=0)
b=[]
b.append(a)
print type(b)
print "  0        ,  1        "
x = raw_input()
x = float(x)
if x==0:
    alloc1(1,130,b)
    alloc1(2,60,b)
    alloc1(3,100,b)
    free1(2,b)
    alloc1(4,200,b)
    free1(3,b)
    free1(1,b)
    alloc1(5,140,b)
    alloc1(6,60,b)
    alloc1(7,50,b)
    free1(6,b)
elif x==1:
    alloc2(1,130,b)
    alloc2(2,60,b)
    alloc2(3,100,b)
    free1(2,b)
    alloc2(4,200,b)
    free1(3,b)
    free1(1,b)
    alloc2(5,140,b)
    alloc2(6,60,b)
    alloc2(7,50,b)
    free1(7,b)
    free1(6,b)