动态分区代码

管道程序

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
{
int fds[2];
pid_t pid;
if(pipe(fds) == -1)
{
perror("pipe error");
exit(1);
}
pid=fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
if(pid == 0)//parent
{
char buf[1024];
int n;
close(fds[1]);
n = read(fds[0], buf, 1024);//从管道读端读出数据到buf
write(STDOUT_FILENO, "child:",6);
write(STDOUT_FILENO, buf, n);//将读到的数据打印到终端
}
else//child
{
close(fds[0]);
write(fds[1], "hello world\n", 12);//向管道写端写入数据“hello world\n”
wait(NULL);
}
return 0;
}

动态分区程序C

#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define N 10000
int n1;//空闲分区的个数
int n2;//作业区的个数
struct kongxian
{
	int start;  //起址
	int end;    //结束
	int length;  //长度
}kongxian[N];
struct zuoye
{
	int start;  //起址
	int end;   //结束
	int length;  //长度
}zuoye[N];
int cmp1(const void *a, const void *b)
{
	return (*(struct kongxian *)a).start - (*(struct kongxian *)b).start;
}
int cmp2(const void *a, const void *b)
{
	return (*(struct zuoye *)a).start - (*(struct zuoye *)b).start;
}
void init()
{
	n1 = 1;  //初始时只有一个空闲区
	n2 = 0;  //初始没有作业
	kongxian[0].start = 0;
	kongxian[0].end = 511;
	kongxian[0].length = 512;
}
void print1() //打印空闲分区
{
	int i;
	for (i = 0; i<n1; i++)
		printf("空闲分区ID:%d 起止:%d 结束:%d 长度:%d\n", i, kongxian[i].start, kongxian[i].end, kongxian[i].length);
}
void print2() //打印作业分区
{
	int i;
	for (i = 0; i<n2; i++)
		printf("作业分区ID:%d 起止:%d 结束:%d 长度:%d\n", i, zuoye[i].start, zuoye[i].end, zuoye[i].length);
}
int main()
{
	int i, j, t, len, flag, id;
	int front, middle, behind;
	int t1, t2;
	init();
	print1();
	printf("输入1装入新作业,输入0回收作业,输入-1结束\n");
	while (scanf("%d", &t) != EOF)
	{
		if (t == 1)  //装入新作业
		{
			printf("请输入作业的占用空间的长度 ");
			scanf("%d", &len);
			flag = 0;
			for (i = 0; i<n1; i++)
			{
				if (kongxian[i].length >= len)  //首次适应算法
				{
					flag = 1;
					break;
				}
			}
			if (!flag)
			{
				printf("内存分配失败\n");
			}
			else
			{
				//将该作业加入作业区里
				zuoye[n2].start = kongxian[i].start;
				zuoye[n2].end = zuoye[n2].start + len;
				zuoye[n2].length = len;
				n2++;  //作业数加1
				if (kongxian[i].length == len) //该分区全部用于分配,删除该空闲分区
				{
					for (j = i; j<n1 - 1; j++)
					{
						kongxian[j].start = kongxian[j + 1].start;
						kongxian[j].end = kongxian[j + 1].end;
						kongxian[j].length = kongxian[j + 1].length;
					}
					n1--;
				}
				else  //该空闲分区部分用于分配,剩余的留在空闲分区中
				{
					kongxian[i].start += len;
					kongxian[i].length -= len;
				}
			}
		}
		else if (t == 0)
		{
			printf("输入要回收的作业ID ");
			scanf("%d", &id);
			front = middle = behind = 0;
			for (i = 0; i<n1; i++)
			{
				if (kongxian[i].start>zuoye[id].end)
					break;
				if (kongxian[i].end == zuoye[id].start)  //待回收的作业上面有空闲分区
				{
					front = 1;
					t1 = i;
				}
				if (kongxian[i].start == zuoye[id].end)  //待回收的作业下面有空闲分区
				{
					behind = 1;
					t2 = i;
				}
			}
			if (!front&&!behind)  //待回收的作业上下均没有空闲分区
			{
				kongxian[n1].start = zuoye[id].start;
				kongxian[n1].end = zuoye[id].end;
				kongxian[n1].length = zuoye[id].length;
				n1++;  //空闲分区增加一个
				qsort(kongxian, n1, sizeof(struct kongxian), cmp1); //插入空闲分区后排序
				for (j = id; j<n2 - 1; j++)  //在作业分区中删除该作业
				{
					zuoye[j].start = zuoye[j + 1].start;
					zuoye[j].end = zuoye[j + 1].end;
					zuoye[j].length = zuoye[j + 1].length;
				}
				n2--;
			}
			if (front &&behind)  //待回收的作业上下均有空闲分区
				middle = 1;
			if (front&&!middle)  //合并待回收的作业和上面的空闲分区
			{
				kongxian[t1].end += zuoye[id].length;
				kongxian[t1].length += zuoye[id].length;
				for (j = id; j<n2 - 1; j++)  //在作业分区中删除该作业
				{
					zuoye[j].start = zuoye[j + 1].start;
					zuoye[j].end = zuoye[j + 1].end;
					zuoye[j].length = zuoye[j + 1].length;
				}
				n2--;
			}
			if (middle)  //合并待回收的作业和上下的空闲分区
			{
				kongxian[t1].end = kongxian[t2].end;
				kongxian[t1].length += (zuoye[id].length + kongxian[t2].length);
				//删除空闲分区t2
				for (j = t2; j<n1 - 1; j++)
				{
					kongxian[j].start = kongxian[j + 1].start;
					kongxian[j].end = kongxian[j + 1].end;
					kongxian[j].length = kongxian[j + 1].length;
				}
				n1--;
				for (j = id; j<n2 - 1; j++)  //在作业分区中删除该作业
				{
					zuoye[j].start = zuoye[j + 1].start;
					zuoye[j].end = zuoye[j + 1].end;
					zuoye[j].length = zuoye[j + 1].length;
				}
				n2--;
			}
			if (behind &&!middle) //合并待回收的作业和下面的分区
			{
				kongxian[t2].start -= zuoye[id].length;
				kongxian[t2].length += zuoye[id].length;
				for (j = id; j<n2 - 1; j++)  //在作业分区中删除该作业
				{
					zuoye[j].start = zuoye[j + 1].start;
					zuoye[j].end = zuoye[j + 1].end;
					zuoye[j].length = zuoye[j + 1].length;
				}
				n2--;
			}
		}
		else
		{
			printf("操作结束\n");
			break;
		}
		print1();
		print2();
	}
	return 0;
}

动态分区程序c++

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h> 
using namespace std;
typedef struct freetable	
{
	long start;	//空闲表起始地址
	long length;	//空闲长度
	long state;	//空闲块状态
	struct freetable *next;	//下一个空闲区的表头
}freetable;

freetable *flist=NULL,*blist=NULL;	//有两个表,一个表空闲表,一个分配表
long nfree=0;				//用顺序的任务号来标记分配表的头

int initializtion(int i);    //初始化链表,i=1时为空闲区,i=2时分配区
int showlist(int i);	     //查看表,i=1时为空闲区,i=2时分配区
int sortlist(int i);         //排序空闲区表,1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大,3最坏适应算法按分区从大到小
int firstbest(long j,long cnum,long csize);	//j:1最先适应算法,2最佳适应算法
int worstfit(long cnum,long csize);		//最坏适应算法
int recover(long cnum);				//回收功能
int menu();					//菜单
int check(int i,freetable *cc);//检查添加是否有重复

int check(int i,freetable *cc)//检查添加是否有重复
{
	freetable *nn,*p;	//使用两个指针来循环判断。
	long caddress=cc->start,ccsize=cc->length,cstate=cc->state;//令c来接受传进来的参数
	long plast,clast=caddress+ccsize;	//clast为此块的结尾地址
	nn=blist;	//nn为分配表的表头
	p=nn->next;	//p为分配表第二个值
	while(p!=NULL)
	{
		plast=p->start+p->length;  //尾地址
		if ( !( (p->start>caddress&&plast>clast)||(p->start<caddress&&plast<clast) ) ) return 0;
		if(i==2) if (p->state==cstate) return 0;//重复了
		p=p->next; 
	}
	nn=flist;	
	p=nn->next;	//空闲表表头
	while (p!=NULL)
	{
		plast=p->start+p->length;
		if ( !( (p->start>caddress&&plast>clast)||(p->start<caddress&&plast<clast) ) ) return 0;
		p=p->next;                  
	}
	return 1;	//没有重复
}

int initializtion(int i)	//初始化,1空闲区,2分配区
{
	freetable *nn,*p;
	int num,f=0,k;
	do
	{
		if (i==1)
		{
			cout<<"请输入预先设置的空闲区个数:";
			p=flist;
		}else
		{
			cout<<"请输入已分配区分配个数:";
			p=blist;
		}
		cin>>num;
		k=0;
		if (num>0) f=num;
	}while (f<=0);
	if (f)	
	{
		if (i==1) cout<<"请依次输入空闲区始址、长度:"<<endl;
		else cout<<"请依次输入已分配区始址、长度、进程编号:"<<endl;
	}
	while (num--)
	{
		k++;
		nn=(freetable  *)malloc(sizeof(freetable )); 
		scanf("%ld%ld",&nn->start,&nn->length);
		if (i==2) scanf("%ld",&nn->state);else nn->state=++nfree;
		if (!check(i,nn)) 
		{
			cout<<"第"<<k<<"个输入有重复,添加失败"<<endl;
		}else{nn->next=p->next;p->next=nn;}//因为第一个是哨兵节点
	}
	nn=NULL;
	cout<<"end"<<endl;
	return 0;
}
int showlist(int i)//查看表
{
	freetable *nn;
	if(i==1)
	{
		nn=flist->next;
		cout<<"空闲区"<<endl;
	}else if(i==2)
	{
		nn=blist->next;
		cout<<"分配区"<<endl;
	}
	cout<<"        标志        始址        长度"<<endl;
	while(nn)
	{
		printf("%10ld %12ld %12ld\n",nn->state,nn->start,nn->length);
		nn=nn->next;
	}
	nn=NULL;
	cout<<"end"<<endl;
	return 0;
}

int sortlist(int i)//排序空闲区表,1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大,3最坏适应算法按分区从大到小
{//链表所以选择排序
	if (flist==NULL||flist->next==NULL) return 0;//表不能为空
 	freetable *change,*last;
	freetable *newhead,*newlast=NULL;
	newhead=(freetable  *)malloc(sizeof(freetable ));newhead->next=NULL;
	while(flist->next!=NULL)
	{
		change=flist;
		last=flist->next;
		if (last!=NULL)//说明至少有一个,last->next才有意义
		{
			while(last->next!=NULL)
			{
				if (i==1) 
				{
					if (change->next->start < last->next->start) change=last;
				}else if (i==2) 
				{
					if (change->next->length < last->next->length) change=last;
				}else if (i==3) 
				{
					if (change->next->length > last->next->length) change=last;
				}
				last=last->next;
			}
		}
		last=change->next;
		change->next=last->next;
		last->next=newhead->next;
		newhead->next=last;
	}
	free(flist);
	flist=newhead;
	newhead=newlast=last=change=NULL;
	return 0;
}

int menu()//菜单选择
{
	flist=(freetable *)malloc(sizeof(freetable)); flist->next=NULL;//哨兵节点
	blist=(freetable *)malloc(sizeof(freetable)); blist->next=NULL;
	int i=1,j=1;
	while(i)
	{
		system("reset");
		cout<<"-------------主菜单--------------"<<endl;
		cout<<"0退出程序"<<endl<<"1添加空闲区/已分配表"<<endl<<"2查看空闲区/已分配表"<<endl<<"3分配功能"<<endl<<"4回收功能"<<endl;
		cout<<"您的选择:";
		cin>>i;j=1;
		system("reset");
		if (i==0) 
		{
			cout<<"谢谢使用"<<endl;
			break;
		}else if (i==1)
		{
			while(j)
			{
				cout<<"请选择添加"<<endl<<"0退出当前"<<endl<<"1空闲区"<<endl<<"2已分配表"<<endl;
				cout<<"您的选择:";
				cin>>j;
				system("reset");
				if (j==0) break;
				else if (j==1||j==2) {initializtion(j);showlist(j);}
				else cout<<"输入非法,请重新输入"<<endl;
			} 
		}else if (i==2)
		{
			while(j)
			{
				cout<<"请选择查看"<<endl<<"0退出当前"<<endl<<"1空闲区"<<endl<<"2已分配表"<<endl;
				cout<<"您的选择:";
				cin>>j;
				system("reset");
				if (j==0) break;
				else if (j==1||j==2) showlist(j);
				else cout<<"输入非法,请重新输入"<<endl;
			}
		}else if (i==3)
		{
			long cnum,csize;
			while(j)
			{
				cout<<"请选择算法"<<endl<<"0退出当前"<<endl<<"1最先适应算法"<<endl<<"2最佳适应算法"<<endl<<"3最坏适应算法"<<endl;
				cout<<"您的选择:";
				cin>>j;
				system("reset");
				if (j==0) break;
				if (j!=1&&j!=2&&j!=3) 
				{
					cout<<"输入非法,请重新输入"<<endl;
					continue;
				}
				showlist(1);showlist(2);
				cout<<"请输入需要分配的进程的进程号、需内存大小"<<endl;
				cin>>cnum>>csize;
				if (j==3) worstfit(cnum,csize);//最坏适应算法
				else firstbest(j,cnum,csize);//最先适应算法,最佳适应算法
			}
		}else if (i==4)
		{
			long cnum;
			while(j)
			{
				system("reset");
				cout<<"请输入需要回收的进程的进程号"<<endl;
				cin>>cnum;
				recover(cnum);
				cout<<"您的选择:0退出,1继续(不为0的数)";
				cin>>j;
			}
		}else cout<<"输入非法,请重新输入"<<endl;
	}
	return 0;
}

int firstbest(long j,long cnum,long csize)//j:1最先适应算法,2最佳适应算法
{
	sortlist(j);//j:1最先适应算法按照起始地址递增,2最佳适应算法按分区从小到大
	freetable *head=flist,*nn;
	while(head->next!=NULL)
	{
		if (head->next->length >= csize) 
		{
			if (head->next->length==csize)
			{
				nn=head->next;
				nn->state=cnum;
				head->next=nn->next;
				nn->next=blist->next;
				blist->next=nn;
			}else
			{
				nn=(freetable  *)malloc(sizeof(freetable ));
				nn->start=head->next->start;
				nn->state=cnum;
				nn->length=csize;
				head->next->length-=csize;
				head->next->start+=csize;
				nn->next=blist->next;
				blist->next=nn;
			}
			csize=-1;
			break;
		}
		head=head->next;
	}
	if (csize==-1) cout<<"分配成功"<<endl;
	else {cout<<"分配失败"<<endl;return 0;}
	showlist(1);showlist(2);
	return 0;
}

int worstfit(long cnum,long csize)//最坏适应算法
{
	sortlist(3);//3最坏适应算法按分区从大到小
	freetable *nn;
	if (flist->next!=NULL && flist->next->length >=csize)
	{
		if (flist->next->length==csize)
		{
			nn=flist->next;
			nn->state=cnum;
			flist->next=nn->next;
			nn->next=blist->next;
			blist->next=nn;
		}else
		{
			nn=(freetable  *)malloc(sizeof(freetable ));
			nn->start=flist->next->start;
			nn->state=cnum;
			nn->length=csize;
			flist->next->length-=csize;
			flist->next->start+=csize;
			nn->next=blist->next;
			blist->next=nn;
		}
		cout<<"分配成功"<<endl;
		showlist(1);showlist(2);
	}else {cout<<"分配失败"<<endl;return 0;}
	return 0;
}

int recover(long cnum)//回收功能
{
	freetable *busyhead=blist,*freehead=flist,*nn;
	if (busyhead->next==NULL)
	{
		cout<<"无需回收资源"<<endl;
		return 0;
	}
	while(busyhead->next!=NULL)
	{
		if (busyhead->next->state==cnum)//全添加到freelist
		{
			nn=busyhead->next;
			busyhead->next=nn->next;
			nn->next=freehead->next;
			freehead->next=nn;
		}else busyhead=busyhead->next;
	}
	sortlist(1);//1按照起始地址递增空闲区
	freehead=flist;
	freehead=freehead->next;
	if (freehead==NULL)
	{
		cout<<"无空闲资源"<<endl;
		return 0;
	}
	while(freehead->next!=NULL)//至少有下一个
	{
		if ((freehead->length+freehead->start) == freehead->next->start)//合并
		{
			nn=freehead->next;
			freehead->length=freehead->length+nn->length;
			freehead->next=nn->next;
			nn->next=NULL;
		}else freehead=freehead->next;
	}
	showlist(1);showlist(2);
	return 0;
}

int main()
{
	menu();
	return 0;
} 

动态分区算法C加强

#include<stdio.h>
#include<stdlib.h>
struct nodespace{
	int teskid;   // 任务号 
	int begin;    // 开始地址 
	int size;     // 大小 
	int status;   // 状态 0代表占用,1代表空闲 
	struct nodespace *next;  // 后指针 
};
 
void initNode(struct nodespace *p){
	if(p == NULL){	//如果为空则新创建一个 
		p = (struct nodespace*)malloc(sizeof(struct nodespace));
	}
	p->teskid = -1;
	p->begin = 0;
	p->size = 640;
	p->status = 1;
	p->next =NULL; 
}
 
/*
*  首次适应算法 
*/ 
void myMalloc1(int teskid,int size,struct nodespace *node){
	while(node != NULL){
		if(node->status == 1){  //空闲的空间 
			if(node->size > size){  //当需求小于剩余空间充足的情况 
				//分配后剩余的空间 
				struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
				p->begin = node->begin + size;
				p->size = node->size - size;
				p->status = 1;
				p->teskid = -1;
				//分配的空间 
				node->teskid = teskid; 
				node->size = size;
				node->status = 0;
				//改变节点的连接 
				p->next = node->next; 
				node->next = p;
				break; 
			}else if(node->size == size){ //需求空间和空闲空间大小相等时 
				node->teskid = teskid; 
				node->size = size;
				node->status = 0;
				break;
			}	
		}
		if(node->next == NULL){
			printf("分配失败,没有足够的空间!\n");
			break;
		}
		node = node->next;
	}
} 
 
/*
* 最佳适应算法 
*/
void myMalloc2(int teskid,int size,struct nodespace *node){
	//最佳块指针 
	struct nodespace *q = NULL;
	//首先找到第一个满足条件的空闲块 
	while(node != NULL){
		if(node->status == 1 && node->size >= size){
			q = node;
			break;
		}
		//如果下一个为空则说明没有空闲区可以分配 
		if(node->next == NULL){
			printf("分配失败,没有足够的空间!\n");
			break;
		} else{
			node = node->next;	
		}
		
	} 
	//遍历寻找最佳的空闲块 
	while(node != NULL){
		if(node->status == 1 && node->size >= size && node->size < q->size){  //空闲的空间 
			q = node;
		}
		node = node->next;
	}
	if(q->size > size){  			//最佳空闲块的大小大于需求大小 
		//分配后剩余的空间 
		struct nodespace *p = (struct nodespace*)malloc(sizeof(struct nodespace));
		p->begin = q->begin + size;
		p->size = q->size - size;
		p->status = 1;
		p->teskid = -1;
		//分配的空间 
		q->teskid = teskid; 
		q->size = size;
		q->status = 0;
		//改变节点的连接 
		p->next = q->next; 
		q->next = p;
	}else if(q->size == size){  	//最佳空闲块空间大小和需求相等
		q->teskid = teskid; 
		q->size = size;
		q->status = 0;
	}		
}
 
void myFree(int teskid,struct nodespace *node){
	if(node->next == NULL && node->teskid == -1){
		printf("还没有分配任何任务!\n");
	}
	
	while(node != NULL){
		if(node->status == 1 && node->next->status ==0 && node->next->teskid == teskid){  //释放空间的上一块空间空闲时 
			node->size = node->size + node->next->size;
			struct nodespace *q = node->next;
			node->next = node->next->next;
			free(q);
			if(node->next->status == 1){ //下一个空间是空闲空间时 
				node->size = node->size + node->next->size;
				struct nodespace *q = node->next;
				node->next = node->next->next;
				free(q);
			}
			break;
		}else if(node->status == 0 && node->teskid == teskid){  //释放空间和空闲空间不连续时 
			node->status = 1;
			node->teskid = -1;
			if(node->next != NULL && node->next->status == 1){ //下一个空间是空闲空间时 
				node->size = node->size + node->next->size;
				struct nodespace *q = node->next;
				node->next = node->next->next;
				free(q);
			}
			break;
		}else if(node->next == NULL){  //任务id不匹配时 
			printf("没有此任务!\n");
			break;
		}
		node = node->next;
	}
	
	 
}
 
void printNode(struct nodespace *node){
	printf("                        内存情况                        \n"); 
	printf(" -------------------------------------------------------\n");
	printf("| 起始地址\t结束地址\t大小\t状态\t任务id\t|\n");
	while(node != NULL){
		if(node->status==1){
			printf("| %d\t\t%d\t\t%dKB\tfree\t 无\t|\n", node->begin + 1, node->begin+node->size, node->size);
		}else{
			printf("| %d\t\t%d\t\t%dKB\tbusy\t %d\t|\n", node->begin + 1, node->begin+node->size, node->size, node->teskid);
		}
		node = node->next;
	}
	printf(" -------------------------------------------------------\n");
}
 
void destory(struct nodespace *node){
	struct nodespace *q = node;
	while(node != NULL){
		node = node->next;
		free(q);
		q = node;
	}
}
 
void menu(){
	printf("1.分配内存\n");
	printf("2.回收内存\n");
	printf("3.查看内存情况\n");
	printf("4.退出\n");
	printf("请输入选项:");
}
 
int main(){
	// node为整个空间 
	struct nodespace *init = (struct nodespace*)malloc(sizeof(struct nodespace));
	struct nodespace *node = NULL;
	initNode(init);			//初始化主链 
	node = init; 			//指向链表头 
	int option; 
	int teskid;
	int size;
	while(1){
		printf("请选择模式:\n 1.演示模式\n 2.自由模式\n 3.退出\n");
		scanf("%d",&option);
		if(option == 1){	//演示模式 
			while(1){		//循环选择实现的算法 
				printf("请选择算法:\n 1.首次适应算法\n 2.最佳适应算法\n 3.退出\n");
				scanf("%d",&option);
				if(option == 1){			//首次适应算法 
					printf("作业1 申请130 KB\n");
					myMalloc1(1,130,node);		//作业1 申请130 KB
					printNode(node);
					printf("作业2 申请60 KB\n");
					myMalloc1(2,60,node);		//作业2 申请60 KB
					printNode(node);
					printf("作业3 申请100 KB\n");
					myMalloc1(3,100,node);		//作业3 申请100 KB
					printNode(node);
					printf("作业2 释放60 KB\n");
					myFree(2,node);			//作业2 释放60 KB
					printNode(node);
					printf("作业4 申请200 KB\n");
					myMalloc1(4,200,node);		//作业4 申请200 KB
					printNode(node);
					printf("作业3 释放100 KB\n");
					myFree(3,node);			//作业3 释放100 KB
					printNode(node);
					printf("作业1 释放130 KB\n");
					myFree(1,node);			//作业1 释放130 KB
					printNode(node);
					printf("作业5 申请140 KB\n");
					myMalloc1(5,140,node);		//作业5 申请140 KB
					printNode(node);
					printf("作业6 申请60 KB\n");
					myMalloc1(6,60,node);		//作业6 申请60 KB
					printNode(node);
					printf("作业7 申请50 KB\n");
					myMalloc1(7,50,node);		//作业7 申请50 KB
					printNode(node);
					printf("作业6 释放60 KB\n");
					myFree(6,node);			//作业6 释放60 KB
					printNode(node);
					destory(node);	//销毁链表
					initNode(init);	//重新初始化 
					node = init;	//重新指向开头 
				}else if(option == 2){		//最佳适应算法 
					printf("作业1 申请130 KB\n");
					myMalloc2(1,130,node);		//作业1 申请130 KB
					printNode(node);
					printf("作业2 申请60 KB\n");
					myMalloc2(2,60,node);		//作业2 申请60 KB
					printNode(node);
					printf("作业3 申请100 KB\n");
					myMalloc2(3,100,node);		//作业3 申请100 KB
					printNode(node);
					printf("作业2 释放60 KB\n");
					myFree(2,node);			//作业2 释放60 KB
					printNode(node);
					printf("作业4 申请200 KB\n");
					myMalloc2(4,200,node);		//作业4 申请200 KB
					printNode(node);
					printf("作业3 释放100 KB\n");
					myFree(3,node);			//作业3 释放100 KB
					printNode(node);
					printf("作业1 释放130 KB\n");
					myFree(1,node);			//作业1 释放130 KB
					printNode(node);
					printf("作业5 申请140 KB\n");
					myMalloc2(5,140,node);		//作业5 申请140 KB
					printNode(node);
					printf("作业6 申请60 KB\n");
					myMalloc2(6,60,node);		//作业6 申请60 KB
					printNode(node);
					printf("作业7 申请50 KB\n");
					myMalloc2(7,50,node);		//作业7 申请50 KB
					printNode(node);
					printf("作业6 释放60 KB\n");
					myFree(6,node);			//作业6 释放60 KB
					printNode(node);
					destory(node);	//销毁链表
					initNode(init);	//重新初始化 
					node = init;	//重新指向开头 
				}else if(option == 3){		//退出
					break;
				}else{
					printf("您的输入有误,请重新输入!\n"); 
				}
			} 	
		}else if(option == 2){	//自由模式 
			while(1){		//循环选择使用的算法 
				printf("请选择算法:\n 1.首次适应算法\n 2.最佳适应算法\n 3.退出\n");
				scanf("%d",&option);
				int n = option;		//标记选择的算法,n == 1 表示首次适应算法, n == 2表示最佳适应算法 
				if(option != 3){
					while(1){
						menu();		//打印想要进行的操作 
						scanf("%d",&option);
						if(option == 1 && n == 1){			//首次适应 
							printf("请输入任务id以及申请的空间大小:\n");
							scanf("%d%d",&teskid,&size);
							myMalloc1(teskid,size,node);
							printNode(node);
						}else if(option == 1 && n == 2){	//最佳适应 
							printf("请输入任务id以及申请的空间大小:\n");
							scanf("%d%d",&teskid,&size);
							myMalloc2(teskid,size,node);
							printNode(node);
						}else if(option == 2){
							printf("请输入任务id:\n");
							scanf("%d",&teskid);
							myFree(teskid,node);
							printNode(node);
						}else if(option == 3){
							printNode(node);
						}else if(option == 4){
							destory(node);	//销毁链表
							initNode(init);	//重新初始化 
							node = init;	//重新指向开头 
							break;
						}else{
							printf("您的输入有误,请重新输入!\n");
							continue;
						}
					}
				}else if(option == 3){
					destory(node);	//销毁链表
					initNode(init);	//重新初始化 
					node = init;	//重新指向开头 
					break;
				}
				else{
					printf("您的输入有误,请重新输入!\n");
				}
			} 
				
		}else if(option == 3){	//退出  
			destory(node);
			return 0;
		}else {
			printf("您的输入有误,请重新输入!\n");
			continue;
		}
	}
	return 0;
}
posted @ 2019-12-02 12:46  凭实力闯荡  阅读(125)  评论(0编辑  收藏  举报