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

📄 时间片轮转算法.c

📁 进程调度 课程设计 草好的 可以一看的 哦
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>

typedef struct node     /**/
{
    char name[10];    /*进程标识符*/
    int prio;         /*进程优先数*/
    int round;        /*进程时间轮转时间片*/
    int cputime;      /*进程占用CPU时间*/
    int needtime;     /*进程到完成还需要的时间*/
    int count;        /*计数器*/
    char state;       /*进程的状态*/
    struct node *next;  /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run;  /*队列指针*/
int N;                     /*进程数*/
/*将就绪队列中的第一个进程投入运行*/
firstin()
{
    run=ready;          /*就绪队列头指针赋值给运行头指针*/
    run->state='R';     /*进程状态变为运行态*/
    ready=ready->next;  /*就绪队列头指针后移到下一进程*/
}

/*标题输出函数*/
void prt1( )
{
	/*输出标题*/
	printf("name cputime needtime count round state\n");
}

/*进程PCB的输出*/
void prt2(PCB *q)
{
	/*轮转法的输出*/
	printf("%-8s%-8d%-8d%-6d%-5d %-c\n",q->name,
		q->cputime,q->needtime,q->count,q->round,q->state);
}

/*输出函数*/
void prt()
{
	PCB *p;
	if(run!=NULL) /*如果运行指针不空*/
	prt2(run);    /*输出当前正在运行的PCB*/
	p=ready;      /*输出就绪队列的PCB*/
	while(p!=NULL)
		{
			prt2(p);
			p=p->next;
		}
	p=finish;      /*输出就完成队列的PCB*/
	while(p!=NULL)
		{
			prt2(p);
			p=p->next;
		}
	printf("Press any key to continue...\n");

    _getch();        /*按任意键继续*/
	
	prt1();
}

insert(PCB *p2)   /*轮转法插入函数*/
{
	tail->next=p2;
	tail=p2;
	p2->next=NULL;
}

void creat()      /*轮转法创建进程PCB*/
{
	PCB *p;
	int i,time;
	char na[10];
	ready=NULL;       /*就绪队列头指针*/
	finish=NULL;      /*完成队列头指针*/
	run=NULL;         /*运行队列指针*/
	printf("Enter name and time of round process \n");
	for(i=1;i<=N;i++)
		{
			p=malloc(sizeof(PCB));
			scanf("%s",na);
			scanf("%d",&time);
			strcpy(p->name,na);
			p->cputime=0;
			p->needtime=time;
			p->count=0;         /*计数器*/
			p->state='w';
			p->round=2;         /*时间片*/
			if(ready!=NULL)
				insert(p);
			else
				{
					p->next=ready;
					ready=p;
					tail=p;
				}
			}

	printf("        output of round\n");
	printf("****************************************\n");
	prt1();
	prt();         /*输出进程PCB信息*/
	run=ready;     /*将就绪队列的第一个进程投入运行*/
	ready=ready->next;
	run->state='R';
}

roundrun()      /*时间片轮转法*/
{
	while(run!=NULL)
		{
			run->cputime=run->cputime+1;
			run->needtime=run->needtime-1;
			run->count=run->count+1;
			/*运行完将其变为完成态,插入完成队列*/
			if(run->needtime==0)
				{
					run->next=finish;
					finish=run;
					run->state='F';
					run=NULL;
					if(ready!=NULL)
						/*就绪队列不空,将第一个进程投入进行*/
						firstin();
				}
			else
				if(run->count==run->round)   /*如果时间片到*/
					{
						run->count=0;        /*计数器置0*/
						if(ready!=NULL)      /*如果就绪队列不空*/
							{
								/*将进程插入到就绪队列中等待轮转*/
								run->state='W';
								insert(run);
								/*将就绪队列的第一个进程投入运行*/
								firstin();
							}
					}
			prt();              /*输出进程信息*/
		}
}

/*主函数*/
main()
{

	printf("Enter process number\n");
	scanf("%d",&N);             /*输入进程数*/
	creat();                    /*轮转法*/
	roundrun();
}

⌨️ 快捷键说明

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