⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zuoyediaodu.c

📁 这是一个操作系统的作业调度的算法,是用C语言来编程的
💻 C
字号:
#define NULL 0
#define false 0
#define ture 1
#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;

#define getJCB(type)(type *)malloc(sizeof(type))
#define NULL 0
int type;//3种作业调度算法
int num;//作业数
int startTime=0;//开始执行时间
int endTime=0;//完成时间
double averageTime=0;//平均周转时间
double averageValTime=0;//带权平均周转时间
int tempTime;

struct JCB{
	char name[10];//作业名称
	int ttime;//提交时间
	int ntime;//所需的运行时间
	int nres;//所需的资源
	char state;//作业状态
	int waitTime;//等待时间
	struct JCB*link;//链指针
}*head=NULL,*p,*q;

void FCFS()//先来先服务算法
{if(head==NULL){
		head=p;q=p;q->link=NULL;}
	else{q->link=p;q=p;q->link=NULL;}}

void SJF()//短作业优先算法
{if(head==NULL){
		head=p;p->link=NULL;}
	else{q=head->link;
		if(q==NULL)//第二项为空
		{head->link=p;
			p->link=NULL;}
		else{
			if(q->ntime>p->ntime)
			{head->link=p;
				p->link=q;
			}
			else{
				JCB *fir,*sec;
				fir=q; 
				sec=head;
				int insert=false;
				while(fir!=NULL&&!insert)
				{
					if(fir->ntime<q->ntime)
					{
						sec=fir;
						fir=fir->link;
					}
					else{
						sec->link=p;
						p->link=fir;
						insert=true;
					}
				}//while
				if(insert==false)//没有插入,运行时间最长
				{
					sec->link=p;
					p->link=NULL;
				}
			}//else
		}//else
	}//else
}

void HRN()//响应比高者优先算法
{
	int ok=false;
	double waitTime1,waitTime2;
	JCB *newp1,*newp2,*oldp;
	q=head->link;
	while(q!=NULL){
		oldp=q->link;
		waitTime1=(double)(head->waitTime+head->ntime)/head->ntime;
		waitTime2=(double)(q->waitTime+q->ntime)/q->ntime;
		if(waitTime1<waitTime2)
		{
			if(head->link->link==NULL)head->link=NULL;//只有两个元素,后一个元素的响应比高
			q->link=head;
			head=q;
		}
		else{
			newp1=head;
			newp2=head->link;
			while(newp2!=NULL&&!ok)
			{waitTime1=(newp2->waitTime+newp2->ntime)/newp2->ntime;
				waitTime2=(q->waitTime+q->ntime)/q->ntime;
				if(waitTime2>waitTime1)
				{newp1->link=q;
					ok=true;
					q->link=newp2;
				}
				else{newp1=newp2;
					newp2=newp2->link;}
			}
			if(!ok)
			{newp2=q;
				newp2->link=NULL;}
		}//else
		q=oldp;
	}//while
}

void addWaitTime()
{
	q=head;
	while(q!=NULL)
	{
		q->waitTime++;
		q=q->link;
	}
}

void input()//输入作业信息
{
	do{
		printf("********************************\n");
		printf("please select the diaodusuanfa:\n");
		printf("1-FCFS.\n");
		printf("2-SJF.\n");
		printf("3-HRN.\n");
		printf("********************************\n");
		scanf('%d",&type);
	}while(type!=1&&type!=2&&type!=3);
	printf("input homework:");
scanf("%d",&num);
printf("\n");
	for(int i=0;i<num;i++)
	{  printf("input homework No. %d message\n ",i+1);
		p=getJCB(JCB);
		printf("homework name: ");
		scanf("%c",p->name);
		printf("homework need time: ");
		scanf("%d",p->ntime);
		printf("homework need resouse: ");
		scanf("%d",p->nres);
		p->ttime=i;
		p->state='W';
		p->waitTime=0;
		if(type!=2)FCFS();
		else 
			SJF();
		printf("\n");}}
void output1(JCB *pr)
{
	printf("name\tstate\tneedtime\n");
	printf("%c",pr->name);printf("\t");
	printf("%d",pr->state);printf("\t");
	printf("%d",pr->ntime);printf("\t");

void check()//输出运行跟就绪队列
{
	JCB *pr;
	printf(" %c is running ",p->name);printf("\n");
	output1(head);
	pr=head->link;
	printf("waiting rol:\n");
	while(pr!=NULL)
	{
		output1(pr);
		pr=pr->link;
	}printf("\n");
}
void output()
{   
	printf("homework %c has finishde\n",p->name);
	printf("finishde moment: ");
	printf("%d\n",startTime+(tempTime));
	int i=endTime-(p->ttime);
	averageTime+=i;
	printf("zhou zhuan time: ");
	printf("%lf\n",i);
	double j=i/(tempTime);
	averageValTime+=j;
	printf("dai quan zhou zhuan time: ");
	printf("%lf\n",j);
	printf("press any key to contiue...........\n");
	getch();
}
void running()//作业调度
{while(head!=NULL)
	{   p=head;
		p->state='R';
		startTime=endTime;
		tempTime=p->ntime;
		while(p->ntime!=0)
		{   p->ntime--;
			endTime++;
			check();}
		output();
		printf("\n");
		head=head->link;
		free(p);
		if(type==3)
		{ if(head!=NULL)
			{  addWaitTime();
				HRN();}}
	}
         printf("pingjun zhou zhuan shijian:%lf\n",averageTime);
	 printf("daiquan zhou zhuan shijian:%lf\n",averageValTime);
	getch();
}
main()
{
	input();
	running();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -