进程调度.docx
- 文档编号:27045428
- 上传时间:2023-06-26
- 格式:DOCX
- 页数:17
- 大小:117.63KB
进程调度.docx
《进程调度.docx》由会员分享,可在线阅读,更多相关《进程调度.docx(17页珍藏版)》请在冰豆网上搜索。
进程调度
1.实验目的
进程调度是处理机管理的核心内容,通过本实验加深理解有关进程控制块,进程队列的概念,并体会和了解先来先服务算法、优先数优先算法、时间片轮转算法的实现。
2.实验内容
1.掌握先来先服务的调度基本思想。
2.掌握优先数优先的调度基本思想。
3.掌握时间轮转算法的调度基本思想。
3.会对以上三种调度算法程序进行编写及调试
3.设计思路
先来先服务算法通过用户输入的Secquence来作为进程队列排序的依据,优先数有限算法通过用户输入的Periority来作为进程队列排序的依据,时间轮转算法先通过进程的优先级,等一个时间片用完后,由下一个优先级的进程开始运行,依次执行,直到各个进程都执行完为止。
4.关键代码
#include
#include
#include
#defineNULL0
typedefstructpcb{/*定义进程控制块PCB*/
charname[10];
intId;
intSequence;
charstate;
intsuper;
intntime;
intrtime;
structpcb*next;
}node;
node*p;
node*ready=NULL;
/****************FCFS先进先出**************************/
/******************************************************/
//定义函数,建立进程列表
node*Create_FCFS()
{
node*head,*p1,*p2;
intn=0;
head=NULL;
p1=p2=(node*)malloc(sizeof(node));
printf("\n请创建进程控制表('0'表示结束):
\n");
printf("IdSequence\n");
scanf("%d%d",&p1->Id,&p1->Sequence);
while(p1->Id!
=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(node*)malloc(sizeof(node));
scanf("%d%d",&p1->Id,&p1->Sequence);
}
p2->next=NULL;
returnhead;
}
//模拟当前就绪进程队列中最先进入进程出队并输出的调度过程
node*FSFC(node*head)
{
node*p=head;
printf("Id=%d,Sequence=%d",p->Id,p->Sequence);
p=p->next;
return(p);
}
voidPrint(node*head)
{
node*p,*q;
intcount=0;
p=head;
while(p)
{
count++;
printf("\n第%d次被调度的就绪进程为:
\n",count);
q=FSFC(p);
p=q;
}
}
//打印进程列表
voidPrint_FCFS(node*head)
{
node*p=head;
printf("\nThetableis(原始进程控制表为):
\n");
while(p)
{
printf("%d%d\n",p->Id,p->Sequence);
p=p->next;
}
}
/*******************按优先级大小***********************/
/******************************************************/
//对进程表按优先数从大到小排序
node*insert(node*head,node*a)
{node*p,*pre;
p=head;
while(p&&p->super>=a->super)
{
pre=p;p=p->next;
}
if(p==head)
{
a->next=head;head=a;
}
elseif(p==NULL)
{
pre->next=a;pre=a;
}
else
{
a->next=pre->next;
pre->next=a;
}
returnhead;
}
//定义函数,建立进程链表
node*Create_Priority()
{
node*head;
node*p1;
intn=0;
p1=(node*)malloc(sizeof(node));
printf("\n请创建进程控制表('0'表示结束):
\n");
printf("IdPriority\n");
scanf("%d%d",&p1->Id,&p1->super);
p1->next=NULL;
head=NULL;
while(p1->Id!
=0)
{
n=n+1;
if(n==1)
head=p1;
else
head=insert(head,p1);
p1=(node*)malloc(sizeof(node));
scanf("%d%d",&p1->Id,&p1->super);
p1->next=NULL;
}
return(head);
}
//模拟按优先数大小进程分级出队的过程
voidPrint_Priority(node*head)
{
intcount=1;
node*p;
p=head;
while(p)
{
printf("第%d个出队进程为:
%d\n",p->super);
printf("Id=%d,Priority=%d\n",p->Id,p->super);
printf("\n");
p=p->next;
count++;
}
}
/***********************时间轮转算法***********************/
voidsort()/*建立对进程进行优先级排列函数*/
{
node*first,*second;
intinsert=0;
if((ready==NULL)||((p->super)>(ready->super)))/*优先级最大者,插入队首*/
{
p->next=ready;
ready=p;
}
else/*进程比较优先级,插入适当的位置中*/
{
first=ready;
second=first->next;
while(second!
=NULL)
{
if((p->super)>(second->super))/*若插入进程比当前进程优先数大,*/
{/*插入到当前进程前面*/
p->next=second;
first->next=p;
second=NULL;
insert=1;
}
else/*插入进程优先数最低,则插入到队尾*/
{
first=first->next;
second=second->next;
}
}
if(insert==0)first->next=p;
}
}
voidCreate()
{
inti,num;
printf("\n请输入进程的个数:
\n");
scanf("%d",&num);
for(i=0;i { printf("\n进程号No.%d: \n",i); p=(node*)malloc(sizeof(node)); printf("\n输入进程名: "); scanf("%s",p->name); printf("\n输入进程优先数: "); scanf("%d",&p->super); printf("\n输入进程运行时间: "); scanf("%d",&p->ntime); printf("\n"); p->rtime=0;p->state='w'; p->next=NULL; sort();/*调用sort函数*/ } } intnum()/*计算进程控制块个数*/ { intcount=0; node*q=ready; while(q! =NULL) { count++; q=q->next; } returncount; } voiddisplay(node*q)/*建立进程显示函数,用于显示当前进程*/ { printf("\n进程名状态优先级需要运行时间已经运行时间\n"); printf("%5s",q->name); printf("%10c",q->state); printf("%10d",q->super); printf("%10d",q->ntime); printf("%15d",q->rtime); printf("\n"); } voidcheck()/*进程查看*/ { node*q; printf("\n当前正在运行的进程是: %s",p->name);/*显示当前运行进程*/ display(p); q=ready; printf("\n当前就绪队列状态为: \n");/*显示就绪队列状态*/ while(q! =NULL) { display(q); q=q->next; } } voiddestroy()/*进程运行结束,撤消进程*/ { printf("\n进程[%s]已完成.\n",p->name); free(p); } voidrunning()/*进程运行时间到,置就绪状态*/ { (p->rtime)++; if(p->rtime==p->ntime) destroy();/*调用destroy函数*/ else { (p->super)--; p->state='w'; sort();/*调用sort函数*/ } } voidTime() { intNum,i=0; charch; Create();//创建进程 Num=num();//计算就绪队列中进程个数 while((Num! =0)&&(ready! =NULL)) { ch=getchar(); i++; printf("\n进程运行次数: %d\n",i); p=ready; ready=p->next; p->next=NULL; p->state='R'; check(); running(); printf("\n按任一键继续......"); ch=getchar(); } printf("\n\n进程已经完成.\n"); ch=getchar(); } main() { intchoice; node*head; printf("进程调度\n"); printf("1、先进先出算法\n"); printf("2、按优先级算法(从大到小)\n"); printf("3、时间轮转算法\n"); printf("0、退出"); printf("\n请输入您的选择: "); scanf("%d",&choice); do{ switch(choice) { case1: head=Create_FCFS(); Print_FCFS(head); Print(head); break; case2: head=Create_Priority(); Print_Priority(head); break; case3: Time(); break; case0: break; default: break; } printf("\n请输入您的选择: "); scanf("%d",&choice); }while(choice! =0); } 5.运行结果 5.实验总结(含心得体会) 先来先服务算法实现简单,但响应不及时,适用于交互性要求不高的系统。 时间轮转算法可以保证不会有进程长期得不到响应,缺点是无法照顾到一些特殊进程。 优先级优先算法每次需要计算进程优先级,占用较多资源。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 进程 调度
![提示](https://static.bdocx.com/images/bang_tan.gif)