操作系统-最先适应算法代码


操作系统最先适应算法代码,使用节点实现,进行内存的分配与回收,还可以查看链表
资源截图
代码片段和文件信息
#include 
using namespace std;

struct node
{
int size;
int beginning;
int name;
bool free;
node *next;
};
void allocation(node *head);
void recycle(node *head);
void examine(node *head);

int main()
{
bool a = true;

node *head = new node;
head->next = NULL;
node *p = new node;
p->next = head->next;
head->next = p;
p->size = 1024;
p->beginning = 0;
p->free = true;
p->name = 1024;

while(a)
{
    cout<<“******最先适应算法******“<     cout<<“1.分配内存   2.回收内存“<     cout<<“3.查看分配   4.退出“< cout<<“************************“<     int choose;
    cin>>choose;
switch(choose)
{
case 1:allocation(head);
   break;
case 2:recycle(head);
   break;
case 3:examine(head);
   break;
case 4:
  cout<<“退出分配!“<   a=false;
  break;
default:
 cout<<“输入错误!请重新输入“< }
}
}
void allocation(node *head)
{

 cout<<“******分配内存******“<  cout<<“请输入分配的进程个数“<  int number;
 cin>>number;

 int size;
 int name;
 for(int i = 0;i  {
 cout<<“请输入进程的大小“<  cin>>size; 
 cout<<“请输入进程的名字“<  cin>>name;
   node *p;
   for(p = head->next;p!=NULL;p=p->next)
   {
   if(p->free == true && p->size>size)
   {
   node *s = new node;
   s->next = p->next;
   p->next =s;

   s->size =p->size-size;
   s->name = p->name;
   s->beginning = p->beginning+size;
   s->free = true;


   p->size = size;
   p->free = false;
   p->name = name;

    cout<<“第“< cout<<“****************************“< break;
   }
   else if(p->free == true && p->size == size)
   {
   p->free = false;
   p->name = name;
    cout<<“第“< cout<<“****************************“< break;
   }
   else if(p->next == NULL && p->size    {
   cout<<“第“<    cout<<“****************************“<    }
   else
   {
   continue;
   }
   }
 }
}
void recycle(node *head)
{
cout<<“请输入回收的内存编号“< int recyclename;
cin>>recyclename;
node *p*q;
p=q=head;
for(p=p->next;p!=NULL;q=pp=p->next)
{

if(p->name == recyclename)
{
p->free = true;
cout<name< cout<free<
if(q->free == true)
{
q->size = q->size + p->size;
q->next = p->next;
p=q;
}


if(p->next->free == true)
{
p->size = p->size + p->next->size;
p->next = p->next->next;
}
cout<<“释放内存成功“< break;
}
else if(p->next == NULL && p->name != recyclename )
{
cout<<“释放内存失败“< break;
}
else
{

continue;
}
}
}
void examine(node *head)
{
node *p;
p = head->next;
while(p !=NULL)
{
cout<<“****************************“< cout<<“进程名字:“<name< cout<<“开始位置:“<< p->beginning<

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-11-20 21:30  操作系统3
     目录           0  2018-11-20 21:19  操作系统3Debug
     文件       40960  2018-11-20 21:19  操作系统3Debug操作系统3.exe
     文件      379856  2018-11-20 21:19  操作系统3Debug操作系统3.ilk
     文件      560128  2018-11-20 21:19  操作系统3Debug操作系统3.pdb
     目录           0  2018-11-20 21:16  操作系统3ipch
     目录           0  2018-11-20 21:16  操作系统3ipch操作系统3-fcfd94c5
     文件    14352384  2018-11-20 21:16  操作系统3ipch操作系统3-fcfd94c5操作系统3-c6c18782.ipch
     目录           0  2018-11-19 20:25  操作系统3操作系统3
     目录           0  2018-11-20 21:19  操作系统3操作系统3Debug
     文件        9590  2018-11-20 21:19  操作系统3操作系统3DebugCL.read.1.tlog
     文件         282  2018-11-20 21:19  操作系统3操作系统3DebugCL.write.1.tlog
     文件         552  2018-11-20 21:19  操作系统3操作系统3Debugcl.command.1.tlog
     文件        1106  2018-11-20 21:19  操作系统3操作系统3Debuglink.command.1.tlog
     文件        2768  2018-11-20 21:19  操作系统3操作系统3Debuglink.read.1.tlog
     文件         500  2018-11-20 21:19  操作系统3操作系统3Debuglink.write.1.tlog
     文件         408  2018-11-20 21:19  操作系统3操作系统3Debugmt.command.1.tlog
     文件         690  2018-11-20 21:19  操作系统3操作系统3Debugmt.read.1.tlog
     文件         232  2018-11-20 21:19  操作系统3操作系统3Debugmt.write.1.tlog
     文件      216064  2018-11-20 21:19  操作系统3操作系统3Debugvc100.idb
     文件      241664  2018-11-20 21:19  操作系统3操作系统3Debugvc100.pdb
     文件         381  2018-11-20 21:19  操作系统3操作系统3Debug操作系统3.exe.intermediate.manifest
     文件          61  2018-11-20 21:19  操作系统3操作系统3Debug操作系统3.lastbuildstate
     文件        2159  2018-11-20 21:19  操作系统3操作系统3Debug操作系统3.log
     文件         713  2018-11-19 20:25  操作系统3操作系统3Debug操作系统3.vcxprojResolveAssemblyReference.cache
     文件           0  2018-11-19 20:25  操作系统3操作系统3Debug操作系统3.write.1.tlog
     文件       58830  2018-11-20 21:19  操作系统3操作系统3Debug最先算法.obj
     文件        3231  2018-11-19 20:25  操作系统3操作系统3操作系统3.vcxproj
     文件         950  2018-11-19 20:25  操作系统3操作系统3操作系统3.vcxproj.filters
     文件         143  2018-11-19 20:09  操作系统3操作系统3操作系统3.vcxproj.user
     文件        3232  2018-11-20 20:53  操作系统3操作系统3最先算法.cpp
............此处省略3个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)