优先数处理机调度

//用位示图管理磁盘存储空间
#include <iostream.h>
struct mapping //页号与块号对应 
{
    
int page; //页号 
    int mass; //块号 
    struct mapping *next; 
}

struct pagelist 
{
    
int length; //页表长度 
    mapping *map;  
}

struct request
{
    
int num; //进程号 
    int size; //请求页面数
    pagelist *plist; //页表始址
    int length; //页表长度
    char state;  //状态,Y表示已分配,N表示未分配 
    struct request *next;
}
;
request 
*S; //请求表 
static int a[8][8];
void allot();
void back();
void reallot(); 
void display(); 
int main()
{
    S
=new request;
    S
->next=NULL; 
    
int a;
    cout
<<"              用位示图管理磁盘存储空间"<<endl; 
    cout
<<"1:分配磁盘"<<endl; 
    cout
<<"2:回收磁盘"<<endl; 
    cout
<<"0:退出"<<endl;    
    
while(1)
    
{
        cout
<<endl<<"请按键选择:";
        cin
>>a;
        
if(a>2||a<0)
        
{
            cout
<<endl<<"输入错误,请重新输入:"
            
continue;
        }

        
switch(a)
        
{
            
case 1: allot(); display(); break;
            
case 2: back();  display(); break;
            
case 0goto end; 
        }
 
    }
 
    end: 
    getchar();
    getchar();
}

void allot()
{
    
int i,j,m=0
    request 
*r=S,*p=new request;
    p
->next=NULL; 
    cout
<<"请输入进程号和申请的空间大小:"<<endl;
    cin
>>p->num>>p->size;
    
if(p->size<=0)
    
{
        cout
<<"申请空间大小错误."<<endl;
        delete p;
        
return;
    }
      
    
while(r->next!=NULL) //将p插到请求链表表尾 
        r=r->next;
    r
->next=p;    
    
for(i=0;i<8;i++)   //检查磁盘空间大小 
    {
       
for(j=0;j<8;j++)
         
if(a[i][j]==0)  m++;
    }

    
if(m<p->size)  //磁盘空间不够时 
    {
        cout
<<endl<<"磁盘空间不够."<<endl;
        p
->length=0;
        p
->state='N';
        p
->plist=0
        
return;
    }
   //磁盘空间够时 
    m=0
    p
->length=p->size; 
    p
->state='Y'
    p
->plist=new pagelist;  //建立页表 
    p->plist->length=p->length;
    p
->plist->map=new mapping;  //建立页表与物理块的对应关系
    p->plist->map->next=NULL; 
    mapping 
*t=p->plist->map; 
    cout
<<"\t柱面号\t磁道号\t物理记录号"<<endl; 
    
for(i=0;i<8;i++)
    
{
       
for(j=0;j<8;j++
         
if(a[i][j]==0)
         
{   
             a[i][j]
=1;
             m
++;
             mapping 
*q=new mapping;
             q
->next=NULL; 
             q
->page=m-1;
             q
->mass=i*8+j;
             cout
<<""<<m<<"块:";
             cout
<<"\t"<<i;
             cout
<<"\t"<<j/4;
             cout
<<"\t"<<j%4<<endl;              
             
while(t->next!=NULL)  //插到物理块与页号对应关系链表的表尾 
                t=t->next;
             t
->next=q;
             q
->next=NULL;    
             
if(m==p->size)    goto end;
         }
  
    }
 
    end:
    cout
<<"分配成功."<<endl<<endl;    
}
 
void back()
{
    
int b; 
    cout
<<"请输入要回收磁盘空间的进程号:"<<endl;
    cin
>>b;
    request 
*p=S->next;
    
while(p!=NULL&&p->num!=b)
       p
=p->next;
    
if(p==NULL)
    
{
        cout
<<endl<<"此进程不存在."<<endl;  
        
return;
    }

    
if(p->state=='N')
    
{
        cout
<<endl<<"错误,此进程没有占据磁盘空间."<<endl;
        
return;
    }

    cout
<<endl<<"共回收"<<p->size<<"个物理块空间,具体情况如下:"<<endl;
    
int i,j;
    cout
<<endl<<"  每块字节号  位数号"<<endl;
    mapping 
*q=p->plist->map->next;
    
for(int k=0;k<p->size;k++)
    
{
        i
=q->mass/8;
        j
=q->mass%8;
        a[i][j]
=0
        cout
<<"\t"<<i<<"\t"<<j<<endl;
        mapping 
*t;
        p
->plist->map->next=q->next;
        t
=q;
        q
=p->plist->map->next; 
        delete t;
    }
     
    
if(S->next==p)
       S
->next=p->next;
    
else
    
{
        request 
*r=S;
        
while(r->next!=p)
          r
=r->next;
        r
->next=p->next;  
    }
           
    delete p
->plist->map;
    delete p
->plist;
    delete p;  
    reallot();  
}

void reallot()
{
    
int m=0;
    request 
*r=S,*p=new request;
    p
->next=NULL;  
    
while(r->next!=NULL)
    
{
        p
=r->next;
        r
=r->next; 
        
if(p->state=='N')  //如果存在等待分配内存的进程 
        
            
for(int i=0;i<8;i++)   //检查磁盘空间大小 
            {
               
for(int j=0;j<8;j++)
               
if(a[i][j]==0)  m++;
            }
 
            
if(m<p->size)  //内存仍不够 
            {  m=0;   continue;} 
            
else
            
{   
                cout
<<endl<<"进程"<<p->num<<"分配情况如下:"<<endl; 
                m
=0
                p
->length=p->size; 
                p
->state='Y'
                p
->plist=new pagelist;  //建立页表 
                p->plist->length=p->length;
                p
->plist->map=new mapping;  //建立页表与物理块的对应关系
                p->plist->map->next=NULL; 
                mapping 
*t=p->plist->map; 
                cout
<<"\t柱面号\t磁道号\t物理记录号"<<endl; 
                
for(int i=0;i<8;i++)
                
{
                   
for(int j=0;j<8;j++
                   
if(a[i][j]==0)
                   
{   
                      a[i][j]
=1;
                      m
++;
                      mapping 
*q=new mapping;
                      q
->next=NULL; 
                      q
->page=m-1;
                      q
->mass=i*8+j;
                      cout
<<""<<m<<"块:";
                      cout
<<"\t"<<i;
                      cout
<<"\t"<<j/4;
                      cout
<<"\t"<<j%4<<endl;              
                      
while(t->next!=NULL)  //插到物理块与页号对应关系链表的表尾 
                        t=t->next;
                      t
->next=q;
                      q
->next=NULL;    
                      
if(m==p->size)  {m=0;  goto end;} 
                   }
  
                }
 
                end:
                      cout
<<"分配成功."<<endl<<endl;  
             }
 
        }
 
    }

}
 
void display()
{
    request 
*p=S->next;
    cout
<<"进程号\t请求页面数\t页表地址\t页表长度\t状态"<<endl;
    
while(p!=NULL)
    
{
        
if(p->state=='N'
          cout
<<p->num<<"\t"<<p->size<<"\t\t"<<p->plist<<"\t\t"<<p->length<<"\t\t"<<p->state<<endl;
        
else  
          cout
<<p->num<<"\t"<<p->size<<"\t\t"<<p->plist<<"\t"<<p->length<<"\t\t"<<p->state<<endl; 
        p
=p->next;
    }

    cout
<<endl; 
    
for(int i=0;i<8;i++)
    
{
       
for(int j=0;j<8;j++)
          cout
<<"    "<<a[i][j]; 
       cout
<<endl;
    }

}
     
 
posted on 2006-02-24 22:00  newzpflying  阅读(809)  评论(1)    收藏  举报