操作系統實驗2之帶內存分配的處理機調度

一、實驗題目
1) 假設主存空間大小,預設操作系統所佔大小並構造未分分區表;未分分區表目內容:起址、長度、狀態(未分/空表目);
2) 結合實驗一,PCB的內容增加爲:
PID;
要求運行時間;
優先權;
狀態;
所需主存大小;
主存起始位置;
PCB指針;
3) 採用最先適應算法分配主存空間;
4) 進程完成後,回收主存,並與相鄰空閒分區合併。
 
二、程序截圖

圖1 主程序窗口

圖2 內存同步演示窗口

三、新增的內存塊類和修改後的雙鏈表類
Cmm.h
fndef CMM_H
#define CMM_H

#define DEFAULT_SIZE 
20
#define MM_TOTAL_SIZE 
100
#define MM_MAX_SIZE 
30
typedef unsigned 
int uint;
/*要使用Clist,需提供prep,nextp,和<(),>()*/
class Cmm
{
public:
    uint begin;
    uint size;
    Cmm
* prep;
    Cmm
* nextp;
public:
    Cmm():begin(
0),size(0),prep(NULL),nextp(NULL){}
    
/*p緊鄰this,即p,this順序*/
    bool extent_front(Cmm
* p)
    
{
        
if( p && this->begin==p->begin+p->size )
        
{
            
this->begin=p->begin;
            
this->size+=p->size;
            
return true;
        }

        
return false;
    }

    
/*this緊鄰p,即this,p順序*/
    bool extent_back(Cmm
* p)
    
{
        
if( p && p->begin==this->begin+this->size )
        
{
            
this->size+=p->size;
            
return true;
        }

        
return false;
    }

    bool div(uint needs)
    
{
        
if( size<needs )
            
return false;
        size
-=needs;
        begin
+=needs;
        
return true;
    }

}
;
#endif
list.h(模板化後,類的實現須和定義放到同一個文件中)
#ifndef PCBLIST_H
#define PCBLIST_H

/*hasa not isa,良好的鏈表應該這樣吧*/
//template<class T>
//class Cnode
//{
//public:
//    T* prep;
//    T*  nextp;
//    T data;
//};

template
<class T>
class Clist
{
public:
    T
* head;
    T
* end;
public:
    
int count;
public:
    Clist():count(
0){end=new T;head=end;}
    
void push_back(T*);
    
void insert_before(T* inpcb,T* nextpcb);
    T
* pop(T*);
    T
* pop_front();
    T
* findp(int id);
    
void remove(T*);
    
//void swap(T*,T*);
    void sort();
    
void clear();
    
~Clist();
}
;
#endif

template
<class T>
Clist
<T>::~Clist()
{
    clear();
}


template
<class T>
void Clist<T>::clear()
{
    
if( count )
    
{
        T
* tp=head;
        T
* p=tp;
        
while( tp=tp->nextp )
        
{
            delete p;
            p
=tp;
        }

        count
=0;
        head
=end;
    }

}


template
<class T>
void Clist<T>::push_back(T* p)
{
    
if( head==end )
    
{
        head
=p;
        p
->nextp=end;
        end
->prep=p;
    }

    
else
    
{
        p
->nextp=end;
        p
->prep=end->prep;
        end
->prep->nextp=p;
        end
->prep=p;
    }

    
++count;
}


template
<class T>
void Clist<T>::insert_before(T* inpcb,T* nextpcb)
{
    
if( head==nextpcb )
    
{
        inpcb
->nextp=head;
        inpcb
->prep=NULL;
        head
->prep=inpcb;
        head
=head->prep;
        
++count;
    }

    
else if( end==nextpcb )
            push_back(inpcb);
    
else
    
{
        inpcb
->nextp=nextpcb;
        inpcb
->prep=nextpcb->prep;
        nextpcb
->prep->nextp=inpcb;
        nextpcb
->prep=inpcb;
        
++count;
    }

}


template
<class T>
T
* Clist<T>::pop(T* p)
{
    
if( p==head )
    
{
        head
=p->nextp;
        p
->nextp->prep=NULL;
    }

    
else
    
{
        p
->prep->nextp=p->nextp;
        p
->nextp->prep=p->prep;
    }

    p
->prep=p->nextp=NULL;
    
--count;
    
return p;
}


template
<class T>
T
* Clist<T>::pop_front()
{
    
return pop(head);
}


template
<class T>
void Clist<T>::remove(T* p)
{
    pop(p);
    delete p;
}


template
<class T>
T
* Clist<T>::findp(int id)
{
    
ifthis->head==end ) return NULL;
    
else
    
{
        T
* p=head;
        
while( p->pid!=id ) p=p->nextp;
        
if( p==end ) return NULL;
        
return p;
    }

}

//template<class T>
//void Clist<T>::swap(T* a,T* b)
//{
//    T* p1;
//    if( a==b )
//        return;
//    a->nextp->prep=b;
//    a->prep->nextp=b;
//    b->nextp->prep=a;
//    b->prep->nextp=a;
//    p1=a->nextp;
//    a->nextp=b->nextp;
//    b->nextp=p1;
//    p1=a->prep;
//    a->prep=b->prep;
//    b->prep=p1;
//}
//直接插入排序以減少排序次數
template<class T>
void Clist<T>::sort()
{
    T
* ap;
    T
* bp=head;
    T
* tp;
    
while( bp!=end && bp->nextp!=end )
    
{
        ap
=bp->nextp;
        
if*ap>*bp && ap!=end )
        
{
            
//if:如果ap的優先級比頭結點還大就直接插在頭部
            
//else:判斷插入位置,並插入
            if*head<*ap ) 
            
{
                pop(ap);
                insert_before(ap,head);    
            }

            
else
            
{
                tp
=bp->prep;
                
while*tp<*ap && tp!=head )
                    tp
=tp->prep;
                pop(ap);
                insert_before(ap,tp
->nextp);
            }

        }

        
else
            bp
=bp->nextp;
    }

}

------------------------------------
倦鳥投林兮日落山,我之故鄉兮在何方?
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章