2017-03-24 12 views
0

現在、私は、リンクされたリストデータ構造の同意を使用してオペレーティングシステムのメモリ管理シミュレーションプログラムを開発しています。コードを実行すると、パーツインタプリタは、非タイプの属性エラーがあると言います。それは私を混乱させます。それは私のコード内のすべての部分の私のishole属性を除いて動作します。誰にでもエラーを教えてもらえますか?リンクを使用したメモリ管理システムの実装

class Node: 
    def __init__(self,size,hole_or_not,s_index,e_index,psid): 
     self.block_size=size 
     self.ishole=hole_or_not 
     self.stat_index=s_index 
     self.end_index=e_index 
     self.ps_id=psid 
     self.next=None 

class MemoryManagement: 
    def __init__(self,os_size=400,totalsize=2560): 
     self.head=Node(os_size,False,0,os_size-1,'os') 
     self.head.next=Node(totalsize-os_size,True,os_size,totalsize,'free') 
     self.totalfreespace=totalsize-os_size 
     self.os_allocated=os_size 

    def allocate(self,pid,size): 
     current=self.head 
     while current.ishole!=False or current.block_size<size: 
      if current.ishole and current.block_size==size: 
       current.ps_id=pid 
       current.ishole=False 
      elif current.ishole and current.block_size>size: 
       hold_start_memory_address=current.stat_index 
       hold_end_memory_address=current.end_index 
       hold_block_size_current=current.block_size 
       current.ps_id=pid 
       current.block_size=size 
       current.end_index=hold_start_memory_address+size-1 
       current.ishole=False 
       remain=Node(hold_block_size_current-size,True,hold_start_memory_address+size,hold_end_memory_address-1,'free') 
       remain.next=current.next 
       current.next=remain 
      current=current.next 
     else: 
      print('----') 

    def printstack(self): 
     current=self.head 
     while current.next: 
      print(current.ps_id,current.block_size,current.stat_index,current.end_index,current.ishole) 
      current=current.next 

は、その後、私は

o=MemoryManagement() 
o.allocate('p1',100) 
o.allocate('p2',500) 
o.allocate('p3',1200) 
o.allocate('p4',1000) 
o.printstack() 

お気に入りのテストケースを使用しそれから私はこのようなエラーが発生しましたが、私は理由を理解することはできません。それだけで

while current.ishole!=False or current.block_size<size: 
    AttributeError: 'NoneType' object has no attribute 'ishole' 
    [Finished in 0.13s] 

答えて

0

機能class MemoryManagementallocate()で提案したアルゴリズムは、いくつかのロジックで行方不明提示属性isholeに起こります。

通報1は - オンwhileループと一つif-else状態でwhile-elseループを分割します。

メインのwhileループでは、既存のすべてのノードを探索できます。すべて ノードがチェックされていて、pid ==> 'メモリが不足しています。各currentノードのその後

def allocate(self,pid,size): 
    current=self.head 
    while (current is not None): 
     ... 
     current=current.next 
     # not enough memory to allocate 

十分sizeが利用可能な場合は、確認してください。 1つのノードが割り当てられたpidを追加することを許可すると、単純に戻ります。

current=current.nextは、whileループ(上記)で移動する必要があります。

 if current.ishole!=False or current.block_size<size: 
      print("current: %s" % (str(current))) 
      if (current.ishole): 
       if (current.block_size > max_size): 
        max_size = current.block_size 
      if current.ishole and current.block_size==size: 
       current.ps_id=pid 
       current.ishole=False 
      elif current.ishole and current.block_size>size: 
       hold_start_memory_address=current.stat_index 
       hold_end_memory_address=current.end_index 
       hold_block_size_current=current.block_size 
       current.ps_id=pid 
       current.block_size=size 
       current.end_index=hold_start_memory_address+size-1 
       current.ishole=False 
       remain=Node(hold_block_size_current-size,True,hold_start_memory_address+size,hold_end_memory_address-1,'free') 
       remain.next=current.next 
       current.next=remain 
       # successfully allocated then return 
       return 
      #current=current.next <== replaced by the while-loop above 
     else: 
      print('----') 

ボーナス1からallocate()関数から返されたブール値を追加します。上記提案されたソースコードで

割り当てが失敗した場合、(単に# not enough memory to allocate後) Falseを返すために有用であり得るか、割り当てが成功した場合(単に# successfully allocated then return後)Trueを返すこと。

+0

ありがとう –