存储器动态分区算法c语言,【操作系统】C语言模拟操作系统实现动态分区分配算法...

#define _CRT_SECURE_NO_WARNINGS 1

#include

#include

#include

#include

#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

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

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

{

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

{

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

{

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

{

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

{

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

{

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

{

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

{

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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值