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

📄 main.cpp

📁 是操作系统的作业调度模拟程序
💻 CPP
字号:
#include "stdio.h" 
#include <stdlib.h> 
#include "conio.h"
#include <time.h>     //用来产生随机函数的
#include "iomanip"
#include <iostream>
#include <dos.h>
using namespace std;

#define getpch(type) (type*)malloc(sizeof(type)) 
#define NULL 0 

struct jcb{ /* 定义作业控制块JCB */ 
	char name[4]; 
	char state; /*作业的状态是就绪W(Wait)、运行R(Run)、或完成F(Finish)三种状态之一*/
	int super; 
	int res[3];
	int ntime;   //need time 
	int rtime;   //run time
	struct jcb* link; 
}*ready=NULL,*runqueue=NULL,*runtail=NULL,*p,*q;   // 把运行的作业放在队列running里,用tail指向它的尾巴。
                                                   //p在产生的时候用,q在插入运行队列的时候用
typedef struct jcb JCB;
 
int sysres[3];//系统总的资源数目
int len;

void printname(JCB*);
void disp();

void sort() /* 建立对作业进行优先级排列函数*/ 
{ 
	JCB *first, *second; 
	int insert=0; 
	if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/ 
	{ 
		p->link=ready; 
		ready=p; 
	} 
	else /* 作业比较优先级,插入适当的位置中*/ 
	{ 
		first=ready; 
		second=first->link; 
		while(second!=NULL) 
		{ 
			if((p->super)>(second->super)) /*若插入作业比当前作业优先数大,*/ 
			{ /*插入到当前作业前面*/ 
				p->link=second; 
				first->link=p; 
				second=NULL; 
				insert=1; 
			}
			else /* 插入作业优先数最低,则插入到队尾*/ 
			{
				first=first->link; 
				second=second->link; 
			}
		}
		if(insert==0) first->link=p; 
	} 
}

void input() /* 建立作业控制块函数*/ 
{ 
	int i,j;
	system("cls"); /*清屏*/ 
	printf("\n 请输入作业块数?"); 
	scanf("%d",&len);
	srand(time(NULL));
	for(i=0;i<len;i++) 
	{
		p=getpch(JCB);
		for(j=0; j < 4; j++)
		{	p->name[j]=65+rand()%26;	}
		p->super=1+rand()%10;//最高优先级为10
		p->res[0]=1+rand()%5;
		p->res[1]=1+rand()%5;
		p->res[2]=1+rand()%5;
		p->ntime=1+rand()%5;
		p->rtime=0;p->state='w';
		p->link=NULL;
		sort(); /* 调用sort函数*/ 
	}
	p=NULL;
} 

void printname(JCB *pr)
{ 
	int j;
	printf("[");
	for(j=0;j<4;j++) printf("%c",pr->name[j]);
	printf("]");
}

void disp(JCB * pr) /*建立作业显示函数,用于显示当前进作业*/ 
{

	printf("\t");	printname(pr); 
    printf("\t| %c\t",pr->state); 
	printf("| %d\t",pr->super);
	printf("| [%d][%d][%d] ",pr->res[0],pr->res[1],pr->res[2]);
	printf("| %d\t",pr->ntime); 
	printf("    | %d\n",pr->rtime);  
	printf("\t____________________________________________________\n");
}

void dispqueue(JCB *pr)  //print the whole queue
{
	printf("\tqname \t state \t super \t  resource  ndtime  runtime \n"); 
	printf("\t____________________________________________________\n");
	while(pr)
	{
		disp(pr);
		pr=pr->link;
	}
}


void insertrunq() /* 建立作业查看函数 */ 
{
	if( runqueue )      // 如果运行队列中已经有作业
	{ 
		runtail->link=q;
		runtail=q;
	}
	else 
	{
		runqueue=runtail=q;
	}
	runtail->link=NULL;
} 


void running() /* 建立作业就绪函数(作业运行时间到,置就绪状态*/ 
{
	JCB *pr,*runned=NULL,*runnedtail;
	pr=runqueue;
	while(pr)
	{		
		if(pr->rtime==pr->ntime)   //作业达到时间运行了,该撤销了
		{
			sysres[0]+=pr->res[0];
			sysres[1]+=pr->res[1];
			sysres[2]+=pr->res[2];
			printf(" 作业 ");
			printname(pr);
			printf(" 已完成.\n"); 
			len--;
			pr=pr->link;
		}         
		else
		{
			(pr->rtime)++; 		
			if(runned)
			{runnedtail->link=pr;runnedtail=pr;}
			else
			{ runned=runnedtail=pr;}
			pr=pr->link;		
			runnedtail->link = NULL; //一定要加,不然出错	
		}

	} 
	runqueue=runned;
}

void sleep(int n)
{
	time_t temp=time(NULL);
	while(time(NULL)<temp+n) ;
}
void main() /*主函数*/ 
{ 
	int h=-1;
	char ch;
	printf("用最高优先级算法进行多道程序调度\n");
	sleep(2);
	input();
	system("cls");
	printf("当前产生的作业:\n");
	dispqueue(ready);
	getchar();
	printf("\n开始运行调度请按任意键。。。\n");
	getchar();

	sysres[0]=sysres[1]=sysres[2]=10;
	while( ready||runqueue ) 
	{ 
		h++;
		system("cls");
		printf("\n The execute number:%d \n",h);

		while(ready && ready->res[0]<= sysres[0] && ready->res[1]<= sysres[1] && ready->res[2] <= sysres[2])
		{
			sysres[0]-=ready->res[0];
			sysres[1]-=ready->res[1];
			sysres[2]-=ready->res[2];
			ready->state='R';
			q=ready;
			ready=ready->link;
			q->link=NULL;
			insertrunq();			
		}
		printf("当前运行的作业是:\n");
		dispqueue(runqueue);
		running();
		printf("当然就绪队列的作业是:\n");
		dispqueue(ready);
		printf("sys[0]=%d  sys[1]=%d  sys[2]=%d \n",sysres[0],sysres[1],sysres[2]); 
		getchar();
		//sleep(1);
	}
	printf("\n\n 全部作业已经完成.\n");
	ch=getchar(); 
}

⌨️ 快捷键说明

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