1、进程调度1.实验目的进程调度是处理机管理的核心内容,通过本实验加深理解有关进程控制块,进程队列的概念,并体会和了解先来先服务算法、优先数优先算法、时间片轮转算法的实现。2.实验内容1掌握先来先服务的调度基本思想。2掌握优先数优先的调度基本思想。3. 掌握时间轮转算法的调度基本思想。3会对以上三种调度算法程序进行编写及调试3.设计思路 先来先服务算法通过用户输入的Secquence来作为进程队列排序的依据,优先数有限算法通过用户输入的Periority来作为进程队列排序的依据,时间轮转算法先通过进程的优先级,等一个时间片用完后,由下一个优先级的进程开始运行,依次执行,直到各个进程都执行完为止。4
2、.关键代码#include #include #include #define NULL 0typedef struct pcb /* 定义进程控制块PCB */ char name10; int Id; int Sequence; char state; int super; int ntime; int rtime; struct pcb *next;node;node *p;node *ready=NULL; /*FCFS先进先出*/*/定义函数,建立进程列表node *Create_FCFS() node *head,*p1,*p2; int n=0; head=NULL; p1=p2
3、=(node*)malloc(sizeof(node); printf(n请创建进程控制表(0表示结束):n); printf(Id Sequencen); 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; return head;/模拟当前就绪进程队列中最先进入进程出队并输出的调度过程node
4、 *FSFC(node *head) node *p=head; printf(Id=%d,Sequence=%d,p-Id,p-Sequence); p=p-next; return(p);void Print(node *head) node *p,*q; int count=0; p=head; while(p) count+; printf(n第%d次被调度的就绪进程为:n,count); q=FSFC(p); p=q; /打印进程列表void Print_FCFS(node *head) node *p=head; printf(nThe table is(原始进程控制表为):n);
5、 while(p) printf(%d %dn,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; else if(p=NULL) pre-next=a;pre=a; else a-next=pre-next; pre-next=a; return head; /定义函数,建立进程链表no
6、de *Create_Priority() node *head; node *p1; int n=0; p1=(node *)malloc(sizeof(node); printf(n请创建进程控制表(0表示结束):n); printf(Id Priorityn); 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
7、%d,&p1-Id,&p1-super); p1-next=NULL; return(head);/模拟按优先数大小进程分级出队的过程void Print_Priority(node *head) int count=1; node *p; p=head; while(p) printf(第%d个出队进程为:%dn,p-super); printf(Id=%d,Priority=%dn,p-Id,p-super); printf(n); p=p-next; count+; /*时间轮转算法*/ void sort() /* 建立对进程进行优先级排列函数*/ node *first, *seco
8、nd; int insert=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 /* 插入进程优先
9、数最低,则插入到队尾*/ first=first-next; second=second-next; if(insert=0) first-next=p; void Create() int i,num; printf(n请输入进程的个数:n); scanf(%d,&num); for(i=0;iname); 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函
10、数*/ int num()/*计算进程控制块个数*/ int count=0; node *q=ready; while(q!=NULL) count+; q=q-next; return count;void display(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)
11、;void check() /* 进程查看 */ node *q; printf(n 当前正在运行的进程是:%s,p-name); /*显示当前运行进程*/ display(p); q=ready; printf(n 当前就绪队列状态为:n); /*显示就绪队列状态*/ while(q!=NULL) display(q); q=q-next; void destroy() /*进程运行结束,撤消进程*/ printf(n 进程 %s 已完成.n,p-name); free(p);void running() /* 进程运行时间到,置就绪状态*/ (p-rtime)+; if(p-rtime=p
12、-ntime) destroy(); /* 调用destroy函数*/ else (p-super)-; p-state=w; sort(); /*调用sort函数*/ void Time() int Num,i=0; char ch; 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
13、(n 按任一键继续.); ch=getchar(); printf(nn 进程已经完成.n); ch=getchar();main() int choice; node *head; printf( 进程调度n); printf(1、先进先出算法n); printf(2、按优先级算法(从大到小)n); printf(3、时间轮转算法n); printf(0、退出); printf(n请输入您的选择:); scanf(%d,&choice); do switch(choice) case 1: head=Create_FCFS(); Print_FCFS(head); Print(head); break; case 2: head=Create_Priority(); Print_Priority(head); break; case 3: Time(); break; case 0: break; default: break; printf(n请输入您的选择:); scanf(%d,&choice); while(choice!=0); 5.运行结果5.实验总结(含心得体会)先来先服务算法实现简单,但响应不及时,适用于交互性要求不高的系统。时间轮转算法可以保证不会有进程长期得不到响应,缺点是无法照顾到一些特殊进程。优先级优先算法每次需要计算进程优先级,占用较多资源。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1