#include
#include
#include
#define NULL 0
#define LEN sizeof(struct PCB)
struct PCB{
int InitAdd;
int EndAdd;
int Length;
int Release;
char name[5];
char state[5];
struct PCB *next;
};
struct PCB a,b,c,d,e,f,*Head;
//初始化
PCB* init(PCB *Head){
struct PCB *p;
a.InitAdd=0;a.Length=10;a.EndAdd=10;strcpy(a.name,"系统");strcpy(a.state,"空表目");
b.InitAdd=10;b.Length=10;b.EndAdd=20;strcpy(b.name,"作业");strcpy(b.state,"已分配");
c.InitAdd=20;c.Length=25;c.EndAdd=45;strcpy(c.name,"作业");strcpy(c.state,"已分配");
d.InitAdd=45;d.Length=20;d.EndAdd=65;strcpy(d.name,"空闲");strcpy(d.state,"未分配");
e.InitAdd=65;e.Length=45;e.EndAdd=110;strcpy(e.name,"作业");strcpy(e.state,"已分配");
f.InitAdd=110;f.Length=146;f.EndAdd=256;strcpy(f.name,"空闲");strcpy(f.state,"未分配");
Head=&a;
a.next=&b;
b.next=&c;
c.next=&d;
d.next=&e;
e.next=&f;
f.next=NULL;
printf("起始地址 长度 终止地址 name 状态 \n");
p=Head;
while(p!=NULL){
printf("%4d%10d%8d%12s%11s\n",p->InitAdd,p->Length,p->EndAdd,p->name,p->state);
p=p->next;
}
return Head;
}
//空闲区分配
void compare(struct PCB *Head,int l){
struct PCB *p,*s;
int k=0;
p=Head;
while(p!=NULL){
if(p->Length>=l&&strcmp(p->name,"空闲")==0){
k=1;
printf("作业分配成功!\n");
if(p->Length==l){
strcpy(p->name,"作业");
strcpy(p->state,"已分配");
break;
}
else{
s=(struct PCB *)malloc(LEN);
s->next=p->next;
p->next=s;
s->EndAdd=p->EndAdd;
strcpy(s->name,"空闲");
strcpy(s->state,"未分配");
s->InitAdd=p->InitAdd+l;
s->Length=p->Length-l;
p->Length=l;
p->EndAdd=p->InitAdd+l;
strcpy(p->name,"作业");
strcpy(p->state,"已分配");
break;
}
}
p=p->next;
}
if(k==0)
printf("作业等待!\n");
printf("起始地址 长度 终止地址 name 状态 \n");
p=Head;
while(p!=NULL){
printf("%4d%10d%8d%12s%11s\n",p->InitAdd,p->Length,p->EndAdd,p->name,p->state);
p=p->next;
}
}
//作业输入
void input(struct PCB *Head){
int N,l;
printf("请输入申请作业个数:\n");
scanf("%d",&N);
for(int i=0;inext!=NULL){
if(strcmp(p->name,"空闲")==0&&strcmp(p->next->name,"空闲")==0){
p->Length=p->Length+p->next->Length;
p->EndAdd=p->InitAdd+p->Length;
p->next=p->next->next;
continue;
}
p=p->next;
}
printf("起始地址 长度 终止地址 name 状态 \n");
p=Head;
while(p!=NULL){
printf("%4d%10d%8d%12s%11s\n",p->InitAdd,p->Length,p->EndAdd,p->name,p->state);
p=p->next;
}
}
//作业释放顺序
void release(PCB *Head){
PCB *p;
int sum=0,temp=1;
p=Head;
while(p!=NULL){
if(strcmp(p->name,"作业")==0)
sum++;
p=p->next;
}
printf("作业个数为:%d\n",sum);
printf("输入作业释放顺序:\n");
p=Head;
while(p!=NULL){
if(strcmp(p->name,"作业")==0)
scanf("%d",&p->Release);
p=p->next;
}
while(temp<=sum){
p=Head;
while(p!=NULL){
if(p->Release==temp){
strcpy(p->name,"空闲");
strcpy(p->state,"未分配");
mix(Head,temp);
break;
}
p=p->next;
}
temp++;
}
}
//主函数
int main(){
PCB *p;
Head=init(Head);
input(Head);
release(Head);
}
若查找可变式分区管理的首次适应算法,请看我的博客。
https://mp.csdn.net/mdeditor/86530244#
谢谢大家观看。