关于操作系统的内存管理问题,100分请高手,答案好的再加100分!急!就今天!请最好用C或C++编写。谢谢!

内存管理问题!请发到我的邮箱里谢谢!!!!!havenjj@live.cn
1. 内容:主存储器空间的分配和回收。
2. 目的:帮助了解在不同的存储管理方式下,应怎样实现主存空间的分配和回收。
3. 题目(任选一个):在可变分区管理方式下,采用最先适应算法实现主存空间的分配和回收.用外碎片的处理用“拼接”和“搬迁”技术.
4. 要求:自行假设主存空间大小,预设操作系统所占大小并构造空闲分区表。表目内容:起址、长度、状态(未分/空表目);
结合实验一,PCB增加为:{PID,要求运行时间,优先权,状态,所需主存大小,主存起始位置,PCB指针}进程完成后,回收主存,并与相邻空闲分区合并。

PS:这里是实验1,我要做的是上面的东西,谢谢,请最好使用C或者C++来进行操作,谢谢!

1. 内容:选择一个调度算法,设计实现处理机调度的程序
2. 目的:多道系统中,当就绪进程数大于处理机数时,须按照某种策略决定哪些进程优先占用处理机。本实验模拟实现处理机调度,以加深对处理机调度的理解。
3. 题目(任选一个) 按优先权调度算法实现处理机调度的程序; 采用响应比高者优先调度算法实现处理机调度。设计按时间片轮转实现处理机调度的程序。
4. 要求: 采用图形界面;可随时增加进程;规定道数,设置后备队列和挂起状态。若内存中进程少于规定道数,可自动从后备队列调度一作业进入。被挂起进程入挂起队列,设置解挂功能用于将指定挂起进程解挂入就绪队列。 每次调度后,显示各进程状态。'

在此注意,请发到我的邮箱里,谢谢havenjj@live.cn

第1个回答  2010-04-18
#include <iostream.h>
#include <stdio.h>
#include <string.h>

struct program
{
char name[30];
long start;
long length;
struct program *next;
};

struct space
{
long start;
long length;
struct space *next;
};

void creat();
void allot();
void back();
void callback(program *r);
void sort(space *L);
void sort(program *S);
void display(space *L);
void display(program *S);

space *L;
program *S;

void creat()
{
L=new space;
space *p=new space;
p->start=0;
p->length=128;
p->next=NULL;
L->next=p;
S=new program;
S->next=NULL;
}
void allot()
{
program *q;
q=new program;
cout<<"请输入进程名和占用空间大小:"<<endl;
cin>>q->name>>q->length;
if(q->length<=0)
{
cout<<"进程空间大小错误."<<endl;
delete q;
return;
}
space *p,*r;
p=L;
r=p;
while(p->next!=NULL&&p->next->length<q->length)
{
r=p;
p=p->next;
}
if(p->next==NULL)
{
cout<<"占用空间过大,分配失败"<<endl;
delete q;
return;
}
else
{
q->start=p->next->start;
q->next=S->next;
S->next=q;

p->next->length-=q->length;
if(p->next->length!=0)
p->next->start+=q->length;
else
{
if(p->next->next!=NULL)
p->next=p->next->next;
else
{
r->next=NULL;
delete p->next;
}
}
}
display(L);
display(S);
}
void back()
{
char name[30];
cout<<"输入要回收的进程名:";
cin>>name;
program *p;
p=S;
while(p->next!=NULL)
{
if(strcmp(p->next->name, name)==0)
{
callback(p);
return;
}
p=p->next;
}
if(p->next==NULL)
cout<<"此进程不存在,内存回收失败"<<endl;

}
void callback(program *t)
{
program *r;
r=t->next;
space *p,*q;
long n;
n=r->length;
if(L->next==NULL)
{
space *w=new space;
w->start=0;
w->length=n;
w->next=NULL;
L->next=w;
t->next=r->next;
delete r;
cout<<"此进程内存回收完毕."<<endl;
display(L);
display(S);
return;
}
p=L->next;
while(p!=NULL&&p->start<r->start)
{
q=p;
p=p->next;
}
if((q->start+q->length==r->start)&&(r->start+n==p->start)) //上下均空
{
q->next=p->next;
q->length=q->length+p->length+n;
t->next=r->next;
delete r;
}
else if(r->start+n==p->start) //下邻空
{
p->start-=n;
p->length+=n;
t->next=r->next;
delete r;
}
else if(q->start+q->length==r->start)
{
q->length+=n;
t->next=r->next;
delete r;
}
else
{
space *sp=new space;
sp->start=r->start;
sp->length=n;
sp->next=L->next;
L->next=sp;
t->next=r->next;
delete r;
}
cout<<"此进程内存回收完毕."<<endl;
display(L);
display(S);
}
void display(space *L)
{
sort(L);
space *p=L->next;
cout<<endl<<"空闲区情况:"<<endl;
if(p==NULL)
{ cout<<"无空闲区了."<<endl; return;}
while(p!=NULL)
{
cout<<"起始地址:"<<p->start<<" 长度:"<<p->length<<endl;
p=p->next;
}
}
void display(program *S)
{
sort(S);
program *p=S->next;
cout<<endl<<"进程内存分配情况:"<<endl;
if(p==NULL)
{ cout<<"内存中无进程."<<endl; return;}
while(p!=NULL)
{
cout<<"进程名:"<<p->name<<" 起始地址:"<<p->start<<" 长度:"<<p->length<<endl;
p=p->next;
}
cout<<endl;
}
void sort(space *L) //链表排序
{
space *p=L->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=L;
while(q->next!=NULL&&q->next->start<p->start)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}
void sort(program *S) //链表排序
{
program *p=S->next, *q, *r;
if(p!=NULL)
{
r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
q=S;
while(q->next!=NULL&&q->next->start<p->start)
q=q->next;
p->next=q->next;
q->next=p;
p=r;
}
}
}

void main()
{
creat();
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(); break;
case 2: back(); break;
case 0: goto end;
}
}
end:
getchar();
}

#include "iostream.h"
#include "iomanip.h"

#define ERR_NOFREEAREA 1
#define ERR_NOADEQUACYAREA 2
#define ERR_ALLOCATED 4

#define ERR_NOJOBS 1
#define ERR_NOSUCHJOB 2
#define ERR_RECLAIMED 4

typedef struct tagUsedNode
{
long address;
long length;
int flag; //作业名
struct tagUsedNode *next;
} USED_AREA , *USED_TABLE;

typedef struct tagFreeNode
{
long address;
long length;
struct tagFreeNode *next;
} FREE_AREA , *FREE_TABLE;

//空闲区、作业区链表
USED_TABLE usedTable = NULL;
FREE_TABLE freeTable = NULL;

//给作业分配空间
//jobname: 作业名
//jobsize: 作业所需空间大小
int Allocate( int jobname , long jobsize )
{
//如果没有空闲区
if( freeTable == NULL )
return ERR_NOFREEAREA;
FREE_TABLE p = freeTable;
FREE_TABLE q = p;
//找首次适应空闲区
while( p != NULL && p->length < jobsize )
{
q = p;
p = p->next;
}
//如果找不到有足够空间的分区
if( p == NULL )
return ERR_NOADEQUACYAREA;

USED_TABLE x = new USED_AREA;
x->address = p->address;
x->length = jobsize;
x->flag = jobname;
x->next = NULL;

//如果该分区大于作业需求,空间大小减去作业大小
if( p->length > jobsize )
{
p->length -= jobsize;
p->address += jobsize;
}
//如果该分区等于作业大小,删除该分区
else
{
if( p == freeTable )
freeTable = NULL;
else
q->next = p->next;
delete p;
}
//作业加入“作业表”中
USED_TABLE r = usedTable;
USED_TABLE t = r;

while( r != NULL && r->address < x->address )
{
t = r;
r = r->next;
}

if( usedTable == NULL )
usedTable = x;
else
{
x->next = r;
t->next = x;
}

return ERR_ALLOCATED;
}

//回收作业空间
//jobname: 作业名
int Reclaim( int jobname )
{
if( usedTable == NULL )
return ERR_NOJOBS;
USED_TABLE p = usedTable;
USED_TABLE q = p;
while( p != NULL && p->flag != jobname )
{
q = p;
p = p->next;
}
//如果没有该作业
if( p == NULL )
return ERR_NOSUCHJOB;
//回收后的空间加入到空闲区
FREE_TABLE r = freeTable;
FREE_TABLE t = r;
FREE_TABLE x;
while( r != NULL && r->address < p->address )
{
t = r;
r = r->next;
}

x = new FREE_AREA;
x->address = p->address;
x->length = p->length;
x->next = NULL;

if( r == freeTable )
{
x->next = r;
freeTable = x;
t = freeTable;
}
else
{
x->next = r;
t->next = x;
}
//合并分区
while( t->next != NULL && t->address + t->length == t->next->address )
{
t->length += t->next->length;
r = t->next;
t->next = t->next->next;
delete r;
}
//删除该作业
if( p == usedTable )
{
usedTable = usedTable->next;
}
else
q->next = p->next;
delete p;

return ERR_RECLAIMED;
}

int Init()
{
freeTable = new FREE_AREA;
freeTable->address = 0;
freeTable->length = 1024;
freeTable->next = NULL;
return 1;
}

void jobrequest()
{
int jobname;
int jobsize;

cout<<"...................."<<endl;
cout<<"作业名: ";
cin >> jobname;
cout<<"作业长度: ";
cin >> jobsize;

if( Allocate( jobname , jobsize ) == ERR_ALLOCATED )
cout<<"该作业已成功获得所需空间"<<endl;
else
cout<<"该作业没有获得所需空间"<<endl;
cout<<"...................."<<endl;
}

void jobreclaim()
{
int jobname;
cout<<"...................."<<endl;
cout<<"作业名: ";
cin >>jobname;
int result = Reclaim( jobname );
if( result == ERR_RECLAIMED )
cout<<"该作业已成功回收"<<endl;
else if( result == ERR_NOSUCHJOB || result == ERR_NOJOBS )
cout<<"系统没有作业或该作业不存在"<<endl;
cout<<"...................."<<endl;
}

void freeTablePrint()
{
cout<<"........................................"<<endl;
cout<<setw(10)<<"address"<<setw(10)<<"length"<<setw(10)<<"state"<<endl<<endl;
FREE_TABLE p = freeTable;
USED_TABLE q = usedTable;
int x , y;
while( p || q )
{
if( p )
x = p->address;
else
x = 0x7fffffff;
if( q )
y = q->address;
else
y = 0x7fffffff;

if( x < y )
{
cout<<setw(10)<<p->address<<setw(10)<<p->length<<setw(10)<<"空闲"<<endl;
p = p->next;
}
if( x > y )
{
cout<<setw(10)<<q->address<<setw(10)<<q->length<<setw(10)<<"已分配"<<setw(10)<<"ID="<<q->flag<<endl;
q = q->next;
}
}
cout<<"........................................"<<endl;

}

void main()
{
Init();

int choose;
bool exitFlag = false;
while( !exitFlag )
{
cout<<"选择功能项 ( 0 - 退出 1 - 分配主存 2 - 回收主存 3 - 显示主存 )"<<endl;
cout<<"?>";
cin>>choose;
switch( choose )
{
case 0:
exitFlag = true;
break;
case 1:
jobrequest();
break;
case 2:
jobreclaim();
break;
case 3:
freeTablePrint();
break;
}
}
}本回答被网友采纳
第2个回答  2010-04-17
太天真了
第3个回答  2010-04-17
不容易
第4个回答  2010-04-19
操作系统的课程设计嘛~~学计算机不能太懒啊~~
相似回答