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

📄 chuliqi.cpp

📁 操作系统中模拟处理器调度.可以自由控制进程数,非常人性化!
💻 CPP
字号:
//操作系统实验:实验二 处理器调度。
//实验目的:本实验模拟在单处理器情况下的处理器调度。
//实验内容:设计一个按时间片轮转法实现处理器调度的程序。
//结果存储在result.txt文件中。


# include <stdio.h>
# include <string.h>
# define FORMAT "%5s%10d%13d%13c\n"
# define N 5
//N为进程的数目。
output (struct tenor *head);
inc (struct tenor *head);
compare (struct tenor *head);
attemper (struct tenor *head);


//定义结构体
struct tenor
{
	char name[5];
	//name为进程名
	int runtime;
	//runtime为进程要求运行时间
	int alrdtime;	
	//alrdtime为进程已运行时间
	char estate;
	//estate为进程状态标志
	int key;
	//key为定义的标志 用来控制语句输出
	struct tenor *next;
	//next为指针
};
struct tenor ten[N];
//定义结构体数组

int s;
//程序INPUT
//功能:用来输入结构体数组数据
input (struct tenor ten[])
{
	FILE *fp;	
	int i;
	printf("输入进程个数s:");
	scanf("%d",&s);
    struct tenor *head,*p;
	
	for (i=0;i<s;i++)
	{
		printf ("输入进程名%d:\n",i+1);
		scanf ("%s",ten[i].name);
		//输入进程名称
		ten[i].alrdtime=0;
		ten[i].estate='R';
		ten[i].key=0;
		//结构体数组初始化 已运行的单位时间数为“0”
		//状态:初始状态为“就绪”,用“R”表示
		//输出标志:初始状态为“0”
		printf ("输入进程%d 运行时间:\n",i+1);
		scanf ("%d",&ten[i].runtime);
		//输入进程要求运行时间
	}
	head=&ten[0];
	p=head;
	for (i=0;i<s-1;i++)//形成循环指针链
		ten[i].next=&ten[i+1];
		ten[s-1].next=head;
    fp=fopen("result.txt","a+");
	fprintf (fp,"初始状态:\n");
	fclose (fp);
    printf ("\n初始状态:\n"); 
	output (p);//输出初始状态
	return 1;
}


//程序OUTPUT
//功能:输出
output (struct tenor * head)
{
	FILE *fp;	
	struct tenor *p;
	p=head;
	fp=fopen("result.txt","a+");
	fprintf (fp,"  name     runtime     alrdtime       estate\n");
    printf ("  name     runtime     alrdtime       estate\n");
	if (head!=NULL)
		do
		{
			fprintf(fp,FORMAT,p->name,p->runtime,p->alrdtime,p->estate);
			printf(FORMAT,p->name,p->runtime,p->alrdtime,p->estate);
	        p=p->next;
		}
		while (p!=head);
		fprintf (fp,"\n\n");
		fclose (fp);
		printf ("\n\n");
	return 1;
}


//程序INC
//功能:模拟进程的运行
inc (struct tenor *head,int j)
{
	struct tenor *p;
	//定义结构体指针 用来控制循环
	int i,t;
	p=head;
	t=p->runtime;
	for (p=head+1;p<head+N;p++)
	{
		if (t<p->runtime)
			t=p->runtime;
		//将所有进程中要求运行时间中最大的值用t保存 用来控制整个循环的次数
	}
	p=head;
	for (i=0;i<t;i++)//t用来控制整体的循环次数
	{
		for (p=head;p<head+N;p++)
		{
			if (p->alrdtime<p->runtime)
			//如果已运行时间小于要求运行时间 则已运行时间加一
			{
				p->alrdtime=p->alrdtime+j;
                printf ("一次循环后:\n");
				output (p);//调用输出函数
			    compare (ten);//加一后调用比较函数
				output (p);//调用输出函数

			}
		}
	}
	return 1;
}


//程序COMPARE
//功能:比较函数 用来比较已运行时间是否等于要求运行时间
compare (struct tenor ten[])
{
	FILE *fp;
	struct tenor *head,*p;
	head=&ten[0];
	for (p=head;p<head+s;p++)
	{
		if (p->alrdtime>=p->runtime)
		//比较已运行时间与要求运行时间 若相等则将p->estate置‘E’
		{
		    p->estate='E';
		    if (p->key==0)
			//检查p->key标志位是否为‘0’若为0标志没有输出过提示信息 则可以输出提示信息
			{
				fp=fopen("result.txt","a+");
				fprintf (fp,"进程 %s完成\n",p->name);
				fclose (fp);
				printf ("进程%s完成\n",p->name);
				//输出提示信息 提示该进程已结束
				p->key=1;
				//若某进程结束 则将标志p->key置为1 表示输出了提示信息
			}
		}
	}
	return 1;
}  
    

//主函数
main ()
{   int j;
    printf("\n");
    printf("输入时间片j:");
    scanf("%d",&j);
    input (ten);
    inc (ten,j);
	return 1;
}

⌨️ 快捷键说明

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