当前位置:文档之家› 《操作系统原理》课程设计

《操作系统原理》课程设计

《操作系统原理》课程设计课题名称:进程调度算法姓名:班级:学号:课程设计起止时间:2005年1月2日——2005年1月7日指导教师:成绩:课程设计任务书进程调度算法一、设计说明该程序实现了进程的创建,且对该进程队列进行动态优先权抢占式和时间片轮转算法的调度。

二、详细设计1. 流程图2. 程序运行环境Turbo C 2.03. 变量的名称、作用及含义说明链表结构process,整型变量name表示进程名称,整型变量prior表示优先数,整型变量needtime表示需要执行时间,整型变量CPUtime表示CPU执行时间,整型变量runtime表示进程执行时间,整型变量state表示运行状态(1:ready, 2:execute, 3:finish)。

4. 各主要模块的功能表述[ 子函数]①Createp() 用来创建新进程,其中的整型变量n表示需要创建的进程个数;②PrintP1(h) 用来打印输出时间片轮转算法的创建进程和运行进程的各变量值;③Finish(h) 用来判断某个进程是否执行完;④Find(h) 用来查找该进程队列中优先数最大的进程;⑤PrintP2(h) 用来打印输出动态优先权抢占式算法的创建进程和运行进程的各变量值;⑥ExecuteP_prio(h) 执行动态优先权抢占式进程调度算法;⑦ExecuteP_time(h) 执行时间片轮转进程调度算法;[ 主函数]main()中使用字符变量select表示a和b中的一个字母,整型变量num表示1至3中的一个数,用双层switch语句实现各模块功能。

5.程序源代码#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#define NULL 0#define Max_Pri 100struct Process{int name;int prio;int needtime;int piecetime;int CPUtime;int runtime;int state;struct Process *next;};typedef struct Process process;/**************************************/process *CreateP(){process *h,*t,*p;int i,a;int ct=0;for(i=0;i<5;i++){t=(process *)malloc(sizeof(process));printf("\n Enter the name of process:");scanf("%d",&a);printf("\nEnter the CPUtime of process:");scanf("%d",&ct);t->name=a;t->CPUtime=ct;t->runtime=0;t->prio=100-t->CPUtime;t->needtime=t->CPUtime;t->piecetime=t->CPUtime/2+t->CPUtime%2;t->state=1;if(i==0){h=t;p=t;}else{p->next=t;p=p->next;}}p->next=NULL;return h;}/**************************************/void PrintP1(process*h){process*t;t=h;printf(" 1:ready 2:execute 3:finish ");printf("\n name priority needtime runtime state\n");{while(t){printf("\n%4d%8d%10d%10d%10d",t->name,t->prio,t->needtime,t->runtime, t->state);t=t->next;}}putchar('\n');}/**************************************/void PrintP2(process *h){process *t;t=h;printf(" 1:ready 2:execute 3:finish ");printf("\n name piecetime needtime runtime CPUtime state\n");while(t){printf("\n%4d%8d%10d%10d%10d%8d",t->name,t->piecetime,t->needtime,t->runtime, t->CPUtime,t->state);t=t->next;}putchar('\n');}/**********************************************/int Finish(process *h){int i=1;process *p;p=h;if (h!=NULL){do{if (p->state!=3){i=0;break;}else p=p->next;} while (p!=NULL);}return i;}/*********************************************/ process*Find(process *h){int max;process *p,*t;p=t=h;if (h!=NULL){max=p->prio;while(p){p=p->next;if (max<p->prio) max=p->prio;}while(t){if (max==t->prio){return t;}else t=t->next;}}}/***************************************/process *ExecuteP_prio(process *h){process *t;while (Finish(h)==0){if (h!=NULL){clrscr();t=Find(h);if (t->needtime<=1){t->needtime=0;t->state=3;}else{t->needtime-=1;t->state=2;}if (t->runtime<t->CPUtime) t->runtime+=1;t->prio-=3;PrintP1(h);if (t->state!=3){sleep(1);t->state=1;}}}sleep(3);}/*************************************/process *ExecuteP_time(process *h){process *t;while (Finish(h)==0){t=h;if (h!=NULL){while(t){clrscr();if (t->needtime<=2){t->state=3;t->needtime=0;}else{t->needtime-=2;t->state=2;}if (t->piecetime>0) t->piecetime-=1;if (t->runtime<t->CPUtime) t->runtime+=2;PrintP2(h);if (t->state!=3) sleep(1);if (t->state!=3) t->state=1;t=t->next;}}}sleep(3);}/************************************/main(){int num,count;char select;process *p=NULL;while (1){clrscr();printf("\n\t------------------------------------------\n");printf("\t a.Priority. \n"); printf("\t b.Piece of time. \n"); printf("\t------------------------------------------\n");printf("Enter a letter a or b:");scanf("%s",&select);switch (select){case 'a':{clrscr();while(1){printf("\n\t--------------------------\n");printf("\t 1.Create a new process. \n");printf("\t 2.Execute process. \n");printf("\t 3.Exit. \n");printf("\t--------------------------\n");printf("Enter a number between 1 to 3:");scanf("%d",&num);switch (num){case 1:{printf("This step is create a process.\n");p=CreateP();PrintP1(p);break;}case 2:{printf("This step is execute a process by priority.\n");ExecuteP_prio(p);PrintP1(p);break;}case 3:exit(0);break;default : printf("\nPlease input right select!\n"); break;}}}case 'b':{clrscr();while(1){printf("\n\t--------------------------\n");printf("\t 1.Create a new process. \n");printf("\t 2.Execute process. \n");printf("\t 3.Exit. \n");printf("\t--------------------------\n");printf("Enter a number between 1 to 3:");scanf("%d",&num);switch (num){case 1:{printf("This step is create a process.\n");p=CreateP();PrintP2(p);break;}case 2:{printf("This step is execute a process by piece of time.\n");ExecuteP_time(p);PrintP2(p);break;}case 3:exit(0);break;default : printf("\n\tPlease input right select!\n"); break;}}}default : printf("\n\tPlease input right select!\n"); break;}}}三、运行结果四、分析结果进程控制块采用链表结构体,进程控制块中进程标示符、进程优先数、需要执行时间、时间片数、CPU执行时间、进程运行时间、进程状态均采用整型。

相关主题