#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#define MAX 128
#define S 1
typedef struct
{
int i; //记录页面号
char *t;//记录页面的起始地址
int flag;//表明该页面是否是空闲的
}page;
typedef struct node
{
int c;//在头结点中存放的是该进程所占内存的大小
int i;//在非头结点中存放的是进程号,在后续结点中存放的是该进程所占的内存的页号
int fg;//表明该进程号已经存在,如果有新进程要编号必须用另外的编号
struct node *next;
}nd;
//int f=0;
nd *head[MAX/4];
page pg[MAX/4];
int init(char *);
int getm(int);
int releasem(int);
int display();
int sleep(int);
int main()
{
char *p;
int n,m,j,k,i,l;
p=(char *)malloc(MAX*sizeof(char));
init(p);
for(i=0;i<MAX/4;i++)
{
pg[i].i=i;
pg[i].t=p+i*4;
pg[i].flag=0;
}
//for(i=0;i<100;i++)printf(“%c —-%d\n”,p[i],p[i]);
while(1)
{
printf(“\n自动演示内存管理请按1\n手工管理按2\n退出按0 \n”);
scanf(“%d”,&l);
if(l==2)
{
while(1)
{
printf(“__________________________________\n”);
printf(“|1 申请内存 |\n”);
printf(“|2 释放内存 |\n”);
printf(“|3 显示内存状况 |\n”);
printf(“|0 退出 |\n”);
printf(“__________________________________”);
printf(“ 请输入选择:”);
scanf(“%d”,&k);
switch(k)
{
case 1:system(“cls”);
getm(0);getchar();break;
case 2:system(“cls”);releasem(0);getchar();break;
case 3:system(“cls”);
display();getchar();break;
case 0:return 0;
default:printf(“输入错误,请重新输入”);break;
}
}
}
else if(l==1)
{
//
long t;
int i;
srand((unsigned)time(&t));
system(“cls”);
for(i=0;i<5;i++)
{
printf(“-”);
sleep(1);
}
printf(“->”);sleep(1);
printf(“现在”);sleep(1);
printf(“开始”);sleep(1);
printf(“演示”);sleep(1);
printf(“内存”);sleep(1);
printf(“管理\n”);sleep(1);
for(i=0;i<4;i++)
{
getm(1);
display();
sleep(3);
}
}
else break;
}
}
int init(char *p)
{
int i;
for(i=0;i<MAX/4;i++)
{
pg[i].flag=0;
head[i]=(nd *)malloc(sizeof(nd));
head[i]->next=NULL;
head[i]->fg=0;
}
for(i=0;i<MAX;i++)
{
*(p+i)=95;
}
return 0;
}
int getm(int x)
{
int i,j,m,n,k=0,flag=0,a=0,b=0;
nd *p,*q;
char ch;
long t;
srand((unsigned)time(&t));
if(x==0)
{
while(1)
{
printf(“输入进程号:”);
/*
if(isdigit(ch)==0)
{
printf(“进程号不合法,请重新输入\n”);
continue;
}
n=ch-48;
*/ scanf(“%d”,&n);
if(n<0||n>10)
{
printf(“进程号不合法,请重新输入\n”);
continue;
}
q=head[n];
if(head[n]->fg==1){printf(“该进程号已经存在,请重新输入!\n”);continue;}
else head[n]->fg=1;
printf(“输入该进程申请的内存大小:”);
scanf(“%d”,&m);
head[n]->c=m;
break;
}
}
if(x==1)
{
while(1)
{
n=rand()%10;
q=head[n];
if(head[n]->fg==1){continue;}
else head[n]->fg=1;
printf(“产生新进程—>进程号:%d\n”,n);
while(1)
{
m=rand()%MAX;
if(m>MAX/10)break;
}
printf(“该进程申请的内存大小:%d\n”,m);
head[n]->c=m;
break;
}
}
if(m%4==0)j=m/4;
else j=m/4+1;
for(i=0,k=0;i<MAX/4;i++)
{
if(pg[i].flag==0)k++;
}
if(k>=j)
{
head[n]->i=n;
for(i=0;i<MAX/4;i++)
{
if(a>=j){break;}
if(pg[i].flag==0)
{
p=(nd *)malloc(sizeof(nd));
q->next=p;
p->i=i;
q=p;
p->next=NULL;
flag=1;
pg[i].flag=1;
if(a==j-1&&m%4!=0)
{
for(b=0;b<m%4;b++)*(pg[i].t+b)=(n+48);
}
else
{
for(b=0;b<4;b++)*(pg[i].t+b)=(n+48);
}
a++;
}
}
return 0;
}
if(x==0){printf(“\n内存剩余空间不足,请释放先释放一部分内存!!\n\n\n”);return 0;}
if(x==1)
{
// system(“cls”);
printf(“\n内存剩余空间不足,开始释放一部分内存!!\n\n\n”);
// sleep(S);
while(1)
{
releasem(1);
// display();
// sleep(S);
if(m%4==0)j=m/4;
else j=m/4+1;
for(i=0;i<MAX/4;i++)
{
if(pg[i].flag==0)k++;
}
if(k<j)continue;
else
{
head[n]->i=n;
for(i=0;i<MAX/4;i++)
{
if(a>=j){break;}
if(pg[i].flag==0)
{
p=(nd *)malloc(sizeof(nd));
q->next=p;
p->i=i;
q=p;
p->next=NULL;
flag=1;
pg[i].flag=1;
if(a==j-1&&m%4!=0)
{
for(b=0;b<m%4;b++)*(pg[i].t+b)=(n+48);
}
else
{
for(b=0;b<4;b++)*(pg[i].t+b)=(n+48);
}
a++;
}
}
return 0;
}
}
}
return 0;
}
int releasem(int x)
{
int n=0,i=0,j=0,flag=0;
nd *p;
long t;
srand((unsigned)time(&t));
//*******************************************************************************
if(x==0)
{
while(1)
{
printf(“请输入要释放内存的进程号:”);
scanf(“%d”,&n);
if(head[n]->fg==0){printf(“该进程号不存在,请重新输入!\n”);continue;}
break;
}
}
//*******************************************************************************
//*******************************************************************************
if(x==1)
{
while(1)
{
n=rand()%10;
if(head[n]->fg==0){continue;}
printf(“释放进程%d的内存\n”,n);
sleep(S);
break;
}
}
//*******************************************************************************
p=head[0];
i=0;
for(i=0;i<MAX/4;i++)
{
if((*head[i]).i==n)
{
flag=1;
head[i]->fg=0;
p=head[i]->next;
while(p!=NULL)
{
pg[p->i].flag=0;
// for(j=0;j<4;j++)*(pg[i].t+j)=95;
p=p->next;
}
break;
}
}
return 0;
}
int display()
{
FILE *fp;
fp=fopen(“d:mem1.txt”,”a++”);
int i=0;
printf(“模拟内存分配状况\n”);
for(i=0;i<MAX/4;i++)
{
if(pg[i].flag==1)
{
// printf(“”)
printf(“%c “,*(pg[i].t));fprintf(fp,”%c “,*(pg[i].t));
printf(“%c “,*(pg[i].t+1));fprintf(fp,”%c “,*(pg[i].t+1));
printf(“%c “,*(pg[i].t+2)); fprintf(fp,”%c “,*(pg[i].t+2));
printf(“%c “,*(pg[i].t+3)); fprintf(fp,”%c “,*(pg[i].t+3));
}
else{ printf(“_ _ _ _ “);fprintf(fp,”_ _ _ _ “);}
if((i+1)%4==0){printf(“\n”);fprintf(fp,”\n”);}
}
printf(“\n\n”);fprintf(fp,”\n\n”);
printf(“起始地址:%d\n”,pg[0].t);fprintf(fp,”起始地址:%d\n”,pg[0].t);
printf(“进程状况:\n”);fprintf(fp,”进程状况:\n”);
for(i=0;i<MAX/4;i++)
{
if(head[i]->fg==1)
{ printf(“进程号:%d分配内存大小%d\n”,i,head[i]->c);fprintf(fp,”进程号:%d分配内存大小%d\n”,i,head[i]->c);}
}
printf(“\n”); fprintf(fp,”\n”);
fclose(fp);
return 0;
}
int sleep(int x)
{
long t,j,k;
j=time(&t);
while(1)
{
k=time(&t);
if(k-j==x)return 0;
}
}
>> 本文固定链接: http://www.vcgood.com/archives/2020
>> 转载请注明: jinfan1009 2007年12月16日 于 C语言帝国 发表