操作系统原理与实验——实验十三多道批处理作业调度(作业不可移动)

发布于:2024-05-09 ⋅ 阅读:(30) ⋅ 点赞:(0)

关键代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct  data{
    int hour;//当前小时
    int min;//当前分钟
}time;
struct node{
    char name[20];//进程名
    time arrive;//到达就绪队列时间
    int zx;//执行时间(预期时间)
    int size;
    int tai;
    int flag;
    time zhuangru; 
    time start;//开始执行时间(进程调度)
    time finish;//执行完成时间
    int zz;//周转时间=完成时间-到达就绪队列时间
    float zzxs;//带权周转时间系数=周转时间/执行时间
    struct node* next;
};
//内存结构体

typedef struct memory_node{
    int size; //内存大小
    int address; //内存始址
} memoryNode;
memoryNode memory;
//分区结构体
struct link_node{
    int size;//分区大小
    int address; //分区始址
    char flag[20]; //分区状态,空闲或者占用作业名
    struct link_node *next;
};
int resource;
//函数名:in参数:无
node* in(){
//函数功能:输入访问序列信息
	int n;
	node *q,*head;
	head = NULL; 
	printf("请输入内存大小:");
	scanf("%d",&memory.size);
	printf("请输入起始地址大小为:");
	scanf("%d",&memory.address);
	printf("请输入磁带机资源:");
	scanf("%d",&resource); 
	printf("请输入进程数量:");
	scanf("%d",&n);
	printf("请输入进程的参数:\n"); 
	for(int i=0;i<n;i++)
	{
		node *p = (node *)malloc(sizeof(node));
		p->flag = 0;
		p->next = NULL;
		scanf("%s %d:%d %d分钟 %dK %d台",&p->name,&p->arrive.hour,&p->arrive.min,&p->zx,&p->size,&p->tai);
		if(head == NULL)
		{
			head = p;
			q = p;
		} 
		else
		{
			q->next = p;
			q = p;
		}
	}
	return head;
}

void output(node *p) {
    //函数功能:按进程执行顺序分别输出每个进程全部结果信息和系统平均时间
   int sum=0,count=0; 
   float sum1=0; 
   printf("\n模拟进程FCFS调度过程输出结果:\n作业名   到达时间   执行时间(分钟)    装入时间   开始时间   完成时间  周转时间(分钟)  带权周转系数\n");
   while(p!=NULL)
   {
    	printf("%5s ",p->name);
    	printf("%5d:%02d ",p->arrive.hour,p->arrive.min);
    	printf("%8d(分钟) ",p->zx);
    	printf("%14d:%02d ",p->zhuangru.hour,p->zhuangru.min);
    	printf("%6d:%02d ",p->start.hour,p->start.min);
    	printf("%7d:%02d ",p->finish.hour,p->finish.min);
    	printf("%7d ",p->zz);
    	printf("%20.2f\n",p->zzxs);
    	sum=sum+p->zz;
    	sum1=sum1+p->zzxs;
    	count++;
    	p=p->next;
   }
   printf("系统平均周转时间为:%.2f\n",sum*1.0/count);
   printf("系统平均带权周转系数为:%.2f\n\n",sum1/count);
}
//函数名:FCFS参数:无
void FCFS(){
//函数功能:调用先来先服务算法
	printf("\n***********FCFS多道批处理作业调度***********\n");
	node *head = in();
	node *h,*q;
	h = NULL;
	q = head;
	while(q!=NULL)
	{
		node *p = (node *)malloc(sizeof(node));
		*p = *q;
		p->next = NULL;
		if(h == NULL)
		{
			h = p;
		}
		else
		{
			node *a = h,*b;
			while(a!=NULL)
			{
				if(p->arrive.hour*60+p->arrive.min<a->arrive.hour*60+a->arrive.min)
				break;
				b = a;
				a = a->next;
			}
			if(a == h)
			{
				p->next = h;
				h = p;
			}
			else
			{
				p->next = a;
				b->next = p;
			}
		}
		q = q->next; 
	}
	
	time now;
	now.hour = 0;
	now.min = 0;
	int qaq = 0,available = resource;
	q = h;
	node *duilie = NULL,*flow,*flagh;
	node *k,*flowk,*KK=NULL; 
	link_node *wfp,*yfp,*bl1,*bl2,*bl1pre,*bl2pre;
	wfp = NULL;
	yfp = NULL;
	
	link_node* i = (link_node*)malloc(sizeof(link_node));
	i->address = memory.address;
	i->flag[0] = '\0';
	i->size = memory.size;
	i->next = NULL;
	wfp = i;
	
	bl1 = wfp;
	while(h!=NULL)
	{
		flagh = h;
		//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 
		//大循环每次末尾都回收第一个 	
		q = h;
		if(duilie==NULL)
		{
			h = q->next;
			q->next = NULL;
			duilie = q;
			flow = duilie;
			if(duilie->arrive.hour*60+duilie->arrive.min>now.hour*60+now.min)
			{
				now.hour = duilie->arrive.hour;
				now.min = duilie->arrive.min;
			}
			duilie->start.hour = now.hour;
			duilie->start.min = now.min;
			duilie->zhuangru.hour = now.hour;
			duilie->zhuangru.min = now.min;
			
			link_node *i = (link_node *)malloc(sizeof(link_node));//分配给作业的结点 
			i->address = wfp->address;
			strcpy(i->flag,q->name);
			i->size = q->size;
			i->next = NULL;
			
			available -=q->tai;
			if(wfp->size==q->size)//修改未分配表 !!!!!
			{
				wfp = NULL;
			}
			else
			{
				wfp->address = wfp->address + q->size;
				wfp->size = wfp->size - q->size;
			}
			//修改已分配表 
			yfp = i; 
		}
		//如果不是第一个,则遍历去找符合条件的 
		node *hh = h,*hhpre;
		bl1 = wfp;
		while(hh!=NULL)
		{
			bl1 = wfp;
			while(bl1!=NULL)
			{	
				if(hh->size<=bl1->size)
				break;
				bl1pre = bl1;
				bl1=bl1->next;
			}

			if(hh->arrive.hour*60+hh->arrive.min<=duilie->start.hour*60+duilie->start.min+duilie->zx&&bl1!=NULL&&available-hh->tai>=0)
			{
				//满足条件 
				hh->start.hour = duilie->start.hour + (duilie->start.min + duilie->zx )/60;
				hh->start.min = (duilie->start.min + duilie->zx )%60;
				hh->zhuangru.hour = now.hour;
				hh->zhuangru.min = now.min;
				available -=hh->tai;
				link_node *i = (link_node *)malloc(sizeof(link_node));//分配给作业的结点 
				i->address = bl1->address;
				strcpy(i->flag,hh->name);
				i->size = hh->size;
				i->next = NULL;
				
				if(bl1 == wfp)//修改未分配表 
				{
					if(bl1->size==hh->size)
					{
						wfp = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				else
				{
					if(bl1->size==hh->size)
					{
						bl1pre->next = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				if(yfp == NULL)
				{
					yfp = i;
				}
				else
				{
					i->next = yfp;
					yfp = i;
				}
				
				if(hh==h)
				{
					h=hh->next;
					hh->next = NULL;
					if(duilie == NULL)
					{
						duilie = hh;
						flow = hh;
					}
					else
					{
						flow->next = hh;
						flow = hh;
					}
					hh = h;
				}
				else
				{
					hhpre->next = hh->next;
					hh->next = NULL;
					if(duilie == NULL)
					{
						duilie = hh;
						flow = hh;
					}
					else
					{
						flow->next = hh;
						flow = hh;
					}
					hh = hhpre->next;
				}
			}
			else
			{
				//不满足条件—————— 空间不满足 or 磁带机不满足
				hhpre = hh;
				hh=hh->next;
			}
		}
		//回收资源
		k=duilie;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;
		bl2 = yfp;
		while(bl2!=NULL)
		{
			if(strcmp(bl2->flag,duilie->name)==0)
			break;
			bl2pre = bl2;
			bl2 = bl2->next; 
		}
		if(bl2 == yfp)
		{
			yfp = bl2->next;
		}
		else
		{
			bl2pre->next = bl2->next;
		}
		bl2->flag[0] = '\0';
		bl2->next = NULL;
		bl1 = wfp;
		if(wfp==NULL)
		{
			wfp = bl2;
		}
		else
		while(bl1!=NULL)
		{
			if((bl2->address+bl2->size)<=wfp->address)//插头 
			{
				if(bl2->address+bl2->size<wfp->address)
				{
					bl2->flag[0] = '\0';
					bl2->next = wfp;
					wfp = bl2;
				}
				else
				{
					wfp->address = bl2->address;
					wfp->size = wfp->size + bl2->size;
					free(bl2);
				}
				break;
			}
			else if(bl1->next == NULL&&bl2->address>=bl1->address+bl1->size)//插尾
			{
				if(bl2->address>bl1->address+bl1->size)
				{
					bl2->flag[0] = '\0';
					bl2->next = NULL;
					bl1->next = bl2;
				}
				else
				{
					bl1->size = bl1->size + bl2->size;
					free(bl2);
				}
				break;
			} 
			else if(bl2->address+bl2->size<=bl1->address)//插中间 
			{
				if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size<bl1->address)//没有上邻,没有下邻
				{
					bl2->next = bl1;
					bl1->next = bl2;
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size<bl1->address)//有上邻,没有下邻
				{
					bl1pre->size = bl1pre->size + bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size==bl1->address)//没有上邻,有下邻
				{
					bl1->address = bl2->address;
					bl1->size = bl1->size +bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size==bl1->address)//有下邻,有下邻 
				{
					bl1pre->size = bl1pre->size + bl1->size + bl2->size;
					bl1pre->next = bl1->next;
					free(bl1);
					free(bl2);
				}
				break;
			} 
			bl1pre = bl1;
			bl1 = bl1->next;
		}
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}		
	}
	while(duilie!=NULL)//h==NULL但是duilie不等于NULL 
	{
		k=duilie;
		k->start.hour = now.hour;
		k->start.min = now.min;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;memory.size +=k->size;
		
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}
	}
	output(KK);
}
//函数名:SSTF参数:无
void SPF(){
//函数功能:调用短进程优先调度算法
	printf("\n***********SPF多道批处理作业调度***********\n");
	node *head = in();
	node *h,*q,*H;
	h = NULL;
	q = head;
	H = q;
	q = q->next;
	while(q!=NULL)
	{
		node *p = (node *)malloc(sizeof(node));
		*p = *q;
		p->next = NULL;
		if(h == NULL)
		{
			h = p;
		}
		else
		{
			node *a = h,*b;
			while(a!=NULL)
			{
				if(p->zx<a->zx)
				break;
				b = a;
				a = a->next;
			}
			if(a == h)
			{
				p->next = h;
				h = p;
			}
			else
			{
				p->next = a;
				b->next = p;
			}
		}
		q = q->next; 
	}
	H->next = h;
	h = H;
	
	time now;
	now.hour = 0;
	now.min = 0;
	int qaq = 0,available = resource;
	q = h;
	node *duilie = NULL,*flow,*flagh;
	node *k,*flowk,*KK=NULL; 
	link_node *wfp,*yfp,*bl1,*bl2,*bl1pre,*bl2pre;
	wfp = NULL;
	yfp = NULL;
	
	link_node* i = (link_node*)malloc(sizeof(link_node));
	i->address = memory.address;
	i->flag[0] = '\0';
	i->size = memory.size;
	i->next = NULL;
	wfp = i;
	
	bl1 = wfp;
	while(h!=NULL)
	{
		flagh = h;
		//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 
		//大循环每次末尾都回收第一个 	
		q = h;
		if(duilie==NULL)
		{
			h = q->next;
			q->next = NULL;
			duilie = q;
			flow = duilie;
			if(duilie->arrive.hour*60+duilie->arrive.min>now.hour*60+now.min)
			{
				now.hour = duilie->arrive.hour;
				now.min = duilie->arrive.min;
			}
			duilie->start.hour = now.hour;
			duilie->start.min = now.min;
			duilie->zhuangru.hour = now.hour;
			duilie->zhuangru.min = now.min;
			
			link_node *i = (link_node *)malloc(sizeof(link_node));//分配给作业的结点 
			i->address = wfp->address;
			strcpy(i->flag,q->name);
			i->size = q->size;
			i->next = NULL;
			
			available -=q->tai;

			if(wfp->size==q->size)//修改未分配表 !!!!!
			{
				wfp = NULL;
			}
			else
			{
				wfp->address = wfp->address + q->size;
				wfp->size = wfp->size - q->size;
			}
			//修改已分配表 
			yfp = i; 
		}
		//如果不是第一个,则遍历去找符合条件的
		node *hh = h,*hhpre;
		bl1 = wfp;
		while(hh!=NULL)
		{
			bl1 = wfp;
			while(bl1!=NULL)
			{	
				if(hh->size<=bl1->size)
				break;
				bl1pre = bl1;
				bl1=bl1->next;
			}
			
			if(hh->arrive.hour*60+hh->arrive.min<=duilie->start.hour*60+duilie->start.min+duilie->zx&&bl1!=NULL&&available-hh->tai>=0)
			{
				//满足条件
				hh->start.hour = duilie->start.hour + (duilie->start.min + duilie->zx )/60;
				hh->start.min = (duilie->start.min + duilie->zx )%60;
				hh->zhuangru.hour = now.hour;
				hh->zhuangru.min = now.min;
				available -=hh->tai;
				link_node *i = (link_node *)malloc(sizeof(link_node));//分配给作业的结点 
				i->address = bl1->address;
				strcpy(i->flag,hh->name);
				i->size = hh->size;
				i->next = NULL;
				
				if(bl1 == wfp)//修改未分配表 
				{
					if(bl1->size==hh->size)
					{
						wfp = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				else
				{
					if(bl1->size==hh->size)
					{
						bl1pre->next = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				if(yfp == NULL)
				{
					yfp = i;
				}
				else
				{
					i->next = yfp;
					yfp = i;
				}
				
				if(hh==h)
				{
					h=hh->next;
					hh->next = NULL;
					if(duilie == NULL)
					{
						duilie = hh;
						flow = hh;
					}
					else
					{
						flow->next = hh;
						flow = hh;
					}
					hh = h;
				}
				else
				{
					hhpre->next = hh->next;
					hh->next = NULL;
					if(duilie == NULL)
					{
						duilie = hh;
						flow = hh;
					}
					else
					{
						flow->next = hh;
						flow = hh;
					}
					hh = hhpre->next;
				}
			}
			else
			{
				//不满足条件——————>空间不满足  or  磁带机不满足
				hhpre = hh;
				hh=hh->next;
			}
		}
		//回收资源
		k=duilie;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;
		bl2 = yfp;
		while(bl2!=NULL)
		{
			if(strcmp(bl2->flag,duilie->name)==0)
			break;
			bl2pre = bl2;
			bl2 = bl2->next; 
		}
		if(bl2 == yfp)
		{
			yfp = bl2->next;
		}
		else
		{
			bl2pre->next = bl2->next;
		}
		bl2->flag[0] = '\0';
		bl2->next = NULL;
		bl1 = wfp;
		if(wfp==NULL)
		{
			wfp = bl2;
		}
		else
		while(bl1!=NULL)
		{
			if((bl2->address+bl2->size)<=wfp->address)//插头 
			{
				if(bl2->address+bl2->size<wfp->address)
				{
					bl2->flag[0] = '\0';
					bl2->next = wfp;
					wfp = bl2;
				}
				else
				{
					wfp->address = bl2->address;
					wfp->size = wfp->size + bl2->size;
					free(bl2);
				}
				break;
			}
			else if(bl1->next == NULL&&bl2->address>=bl1->address+bl1->size)//插尾
			{
				if(bl2->address>bl1->address+bl1->size)
				{
					bl2->flag[0] = '\0';
					bl2->next = NULL;
					bl1->next = bl2;
				}
				else
				{
					bl1->size = bl1->size + bl2->size;
					free(bl2);
				}
				break;
			} 
			else if(bl2->address+bl2->size<=bl1->address)//插中间 
			{
				if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size<bl1->address)//没有上邻,没有下邻
				{
					bl2->next = bl1;
					bl1->next = bl2;
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size<bl1->address)//有上邻,没有下邻
				{
					bl1pre->size = bl1pre->size + bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size==bl1->address)//没有上邻,有下邻
				{
					bl1->address = bl2->address;
					bl1->size = bl1->size +bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size==bl1->address)//有下邻,有下邻 
				{
					bl1pre->size = bl1pre->size + bl1->size + bl2->size;
					bl1pre->next = bl1->next;
					free(bl1);
					free(bl2);
				}
				break;
			} 
			bl1pre = bl1;
			bl1 = bl1->next;
		}
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}		
	}
	while(duilie!=NULL)//h==NULL但是duilie不等于NULL 
	{
		k=duilie;
		k->start.hour = now.hour;
		k->start.min = now.min;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;memory.size +=k->size;
		
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}
	}
	output(KK);
}
//函数名:Elevator参数:无
void HRRF(){
//函数功能:调用电梯调度算法
	printf("\n***********HRRF多道批处理作业调度***********\n");
	node *head = in();
	node *h,*q;
	h = NULL;
	q = head;
	while(q!=NULL)
	{
		node *p = (node *)malloc(sizeof(node));
		*p = *q;
		p->next = NULL;
		if(h == NULL)
		{
			h = p;
		}
		else
		{
			node *a = h,*b;
			while(a!=NULL)
			{
				if(p->arrive.hour*60+p->arrive.min<a->arrive.hour*60+a->arrive.min)
				break;
				b = a;
				a = a->next;
			}
			if(a == h)
			{
				p->next = h;
				h = p;
			}
			else
			{
				p->next = a;
				b->next = p;
			}
		}
		q = q->next; 
	}
	
	time now;
	now.hour = 0;
	now.min = 0;
	int qaq = 0,available = resource,flag=0;
	q = h;
	node *duilie = NULL,*flow,*flagh;
	node *k,*flowk,*KK=NULL; 
	link_node *wfp,*yfp,*bl1,*bl2,*bl1pre,*bl2pre;
	wfp = NULL;
	yfp = NULL;
	
	link_node* i = (link_node*)malloc(sizeof(link_node));
	i->address = memory.address;
	i->flag[0] = '\0';
	i->size = memory.size;
	i->next = NULL;
	wfp = i;
	
	bl1 = wfp;
	while(h!=NULL)
	{
		flagh = h;
		//每一次大循环都找在自己执行完的时间内并且符合条件的 摘下来 
		//大循环每次末尾都回收第一个 			
		q = h;
		if(flag==0)
		{	
			flag=1;
			node *first = h,*b = h;
			while(b!=NULL)
			{
				if(b->arrive.hour*60+b->arrive.min<first->arrive.hour*60+first->arrive.min)
				first = b;
				b = b->next; 
			} 
			b = h;
			while(b!=NULL)
			{
				if(b->next == first)
				break;
				b = b->next;
			}
			if(first == h)
			{
				h = first->next;
				first->next = NULL;
				duilie = first;
			}
			else
			{
				b->next = first->next;
				first->next = NULL;
				duilie = first;
			}
			flow = duilie;
			now.hour = duilie->arrive.hour;
			now.min = duilie->arrive.min;
			
			duilie->start.hour = now.hour;
			duilie->start.min = now.min;
			duilie->zhuangru.hour = now.hour;
			duilie->zhuangru.min = now.min;
			now.hour = duilie->start.hour + (duilie->start.min + duilie->zx)/60;
			now.min =  (duilie->start.min + duilie->zx)%60;
			duilie->finish.hour = now.hour;
			duilie->finish.min = now.min;
			
			duilie->zz = duilie->finish.hour*60 + duilie->finish.min - (duilie->arrive.hour*60 + duilie->arrive.min);
			duilie->zzxs = duilie->zz*1.0/duilie->zx; 
			
			duilie  = duilie->next;
			KK = flow;
			flowk = KK;	
		}
		 q = h;
		 node *xyb=NULL;
	while(q!=NULL)
	{
		node *p = (node *)malloc(sizeof(node));
		*p = *q;
		p->next = NULL;
		if(xyb == NULL)
		{
			xyb = p;
		}
		else
		{
			node *a = xyb,*b;
			while(a!=NULL)
			{
				if((now.hour*60+now.min-q->arrive.hour*60-q->arrive.min+q->zx)*1.0/q->zx>(now.hour*60+now.min-a->arrive.hour*60-a->arrive.min+a->zx)*1.0/a->zx)
				break;
				b = a;
				a = a->next;
			}
			if(a == xyb)
			{
				p->next = xyb;
				xyb = p;
			}
			else
			{
				p->next = a;
				b->next = p;
			}
		}
		q = q->next; 
	}
	//如果不是第一个,则遍历去找符合条件的
		node *hh = xyb,*hhpre;
		bl1 = wfp;
		while(hh!=NULL)
		{
			bl1 = wfp;
			while(bl1!=NULL)
			{	
				if(hh->size<=bl1->size)
				break;
				bl1pre = bl1;
				bl1=bl1->next;
			}
			if(bl1!=NULL&&available-hh->tai>=0)
			{
				//满足条件 
				available -=hh->tai;
				link_node *i = (link_node *)malloc(sizeof(link_node));//分配给作业的结点 
				i->address = bl1->address;
				strcpy(i->flag,hh->name);
				i->size = hh->size;
				i->next = NULL;
				
				if(bl1 == wfp)//修改未分配表 
				{
					if(bl1->size==hh->size)
					{
						wfp = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				else
				{
					if(bl1->size==hh->size)
					{
						bl1pre->next = bl1->next;
						free(bl1);
					}
					else
					{
						bl1->address = bl1->address + hh->size;
						bl1->size = bl1->size - hh->size;
					}
				}
				if(yfp == NULL)
				{
					yfp = i;
				}
				else
				{
					i->next = yfp;
					yfp = i;
				}

				node *xybh = h,*xybhpre;
				while(xybh!=NULL)
				{
					if(strcmp(xybh->name,hh->name) == 0)
					{
						xybh->zhuangru.hour = now.hour;
						xybh->zhuangru.min = now.min;
						break;
					}
					xybhpre = xybh;
					xybh = xybh->next;
				}
				if(xybh == h)
				{
					h = h->next;
					xybh->next = NULL;
					if(duilie == NULL)
					{ 
						duilie = xybh;
						flow = xybh; 
					} 
					else
					{
						flow->next = xybh;
						flow = xybh;
					}
				}
				else
				{
					xybhpre->next = xybh->next;
					xybh->next = NULL;
					if(duilie == NULL)
					{ 
						duilie = xybh;
						flow = xybh; 
					} 
					else
					{
						flow->next = xybh;
						flow = xybh;
					} 
				}
			}
				//否则不满足条件——————> 空间不满足 or  磁带机不满足
			hh = hh->next;
			
		}
		//回收资源
		k=duilie;
		k->start.hour = now.hour;
		k->start.min = now.min;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;
		bl2 = yfp;
		while(bl2!=NULL)
		{
			if(strcmp(bl2->flag,duilie->name)==0)
			break;
			bl2pre = bl2;
			bl2 = bl2->next; 
		}
		if(bl2 == yfp)
		{
			yfp = bl2->next;
		}
		else
		{
			bl2pre->next = bl2->next;
		}
		bl2->flag[0] = '\0';
		bl2->next = NULL;
		bl1 = wfp;
		if(wfp==NULL)
		{
			wfp = bl2;
		}
		else
		while(bl1!=NULL)
		{
			if((bl2->address+bl2->size)<=wfp->address)//插头 
			{ 
				if(bl2->address+bl2->size<wfp->address)
				{
					bl2->flag[0] = '\0';
					bl2->next = wfp;
					wfp = bl2;
				}
				else
				{
					wfp->address = bl2->address;
					wfp->size = wfp->size + bl2->size;
					free(bl2);
				}
				break;
			}
			else if(bl1->next == NULL&&bl2->address>=bl1->address+bl1->size)//插尾
			{
				if(bl2->address>bl1->address+bl1->size)
				{
					bl2->flag[0] = '\0';
					bl2->next = NULL;
					bl1->next = bl2;
				}
				else
				{
					bl1->size = bl1->size + bl2->size;
					free(bl2);
				}
				break;
			} 
			//else if()
			else if(bl2->address+bl2->size<=bl1->address)//插中间 
			{
				if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size<bl1->address)//没有上邻,没有下邻
				{
					bl2->next = bl1;
					bl1->next = bl2;
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size<bl1->address)//有上邻,没有下邻
				{
					bl1pre->size = bl1pre->size + bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size<bl2->address&&bl2->address+bl2->size==bl1->address)//没有上邻,有下邻
				{
					bl1->address = bl2->address;
					bl1->size = bl1->size +bl2->size;
					free(bl2);
				}
				else if(bl1pre->address+bl1pre->size==bl2->address&&bl2->address+bl2->size==bl1->address)//有下邻,有下邻 
				{
					bl1pre->size = bl1pre->size + bl1->size + bl2->size;
					bl1pre->next = bl1->next;
					free(bl1);
					free(bl2);
				}
				break;
			} 
			bl1pre = bl1;
			bl1 = bl1->next;
		}
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}		
	}
	while(duilie!=NULL)//h==NULL但是duilie不等于NULL 
	{
		k=duilie;
		k->start.hour = now.hour;
		k->start.min = now.min;
		k->finish.hour = k->start.hour+(k->start.min+k->zx)/60;
		k->finish.min = (k->start.min + k->zx)%60;
		
		k->zz = k->finish.hour*60 + k->finish.min - (k->arrive.hour*60 + k->arrive.min);
		k->zzxs = k->zz*1.0/k->zx; 
		now.hour = k->finish.hour;
		now.min = k->finish.min;
		available +=k->tai;memory.size +=k->size;
		
		duilie = duilie->next;
		if(KK==NULL)
		{
			KK=k;
			flowk = k;
			k->next = NULL;
		}
		else
		{
			flowk->next = k;
			flowk = k;
			k->next = NULL;
		}
	}
	output(KK);
}

//函数名:Out参数:无
void Out(){
//函数功能:输出选项菜单
	printf("***************多道批处理作业调度***************\n");
	printf("  *        1.先来先服务调度算法(FCFS)          *\n");
	printf("  *        2.最短作业优先算法(SPF)             *\n");
	printf("  *        3.响应比最高者优先算法(HRRF)        *\n");
	printf("  *        0.退出                              *\n");
	printf("           请输入选项[ ]\b\b");
}
int main()
{
    while (1)
    {
        Out();//给出选项
        int c;
        scanf("%d", &c);
        switch (c){//选择算法
        case 1:FCFS();
            break;
        case 2:SPF();
            break;
        case 3:HRRF();
            break;
        default:printf("\n调度结束!");
            exit(1);
        }
    }
    return 0;
}

实验结果

实验总结

①处理单链表还是不熟练;

②对操作哪个单链表模糊,导致结果错误;

③C语言没有return不报错;

④p=q不等价与*p=*q;

⑤宏观上并行,微观上串行;先释放在申请。

tips:本题思路


网站公告

今日签到

点亮在社区的每一天
去签到