c语言课程设计处理机低级调度模拟系统
- 格式:doc
- 大小:586.50 KB
- 文档页数:24
C语言模拟CPU调度C语言模拟CPU调度CPU在处理多个进程时,要根据各种情况对处理的进程进行调度。
这其中就包括对各个进程优先级的处理,和调度算法的处理。
下面这个C语言程序,是我在大学期间学习《操作系统》课程的CPU调度时编写的,模拟了CPU的两种调度模式和各种模式所对应的多种调度算法。
为了模拟得更为形象,采用了图形屏幕输出。
#include<stdlib.h>#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<dos.h>#define NULL 0/*-----------------------------------------------------------------*/ struct event /*事件结点结构*/{int evtype; /*1:进程产生。
2:进程执行。
3:激活阻塞进程。
4:进程执行完5:进程阻塞* 6:返回事件*/int pnum; /*执行该事件的进程号*/int t; /*事件发生的时间*/int ifblock; /*如果是执行事件标准其有无阻塞,其它事件时无定义*/struct event *next;};struct process /*进程结点结构*/{int pnum; /*进程号*/int plong; /*进程长度*/int prior; /*进程优先级*/int blocknum; /*进程当前的阻塞数*/int runtime; /*执行次数*/struct process *next;};struct headnod /*队列头结点结构*/{struct process *head; /*队列头*/int totalpro; /*队列中的进程数*/struct process *tail; /*队列尾*/};/*============================================ =====================*/int Mode,Algorithm; /*选择的调度模式和算法*/ struct event *evhead; /*事件链表的头结点*/struct headnod *ready,*block; /*就绪队列、阻塞队列的头结点*/main(){int gdriver,gmode;struct process *runpro=NULL; /*当前正在执行的进程*/ struct process *wakepro; /*当前被唤醒的进程*/ struct process *newpro; /*新建进程*/int busy=0; /*标志cpu状态*/menu(); /*选择调度模式和算法的菜单*/gdriver=DETECT;initgraph(&gdriver, &gmode, "");setbkcolor(LIGHTBLUE);cleardevice();welcome(); /*显示欢迎屏幕*/start(); /*初始化*/while(evhead!=NULL){switch(evhead->evtype){case 1: /*产生新进程并在就绪队列中排队*/{randomize();newpro=(struct process *)malloc(sizeof(struct process));newpro->pnum=evhead->pnum;newpro->plong=rand()%3+1;if((Mode==2)&&(Algorithm==2))newpro->prior=rand()%3+1;elsenewpro->prior=0; /*优先级相等*/if((Mode==2)&&(Algorithm==1)){newpro->runtime=newpro->plong+1;newpro->blocknum=rand()%2;}else{newpro->blocknum=rand()%3;newpro->runtime=1;}newpro->next=NULL;drawcreate(newpro); /*画出产生一个新进程*/Orderp(1,newpro);drawQ(1);sleep(1);if(ready->head==newpro){if(busy==0)Runevent();elseif((Mode==2)&&(Algorithm==2)&&(newp ro->prior>runpro->prior)){Interupt(runpro);returnev(runpro);}}}break;case 2: /*执行事件*/ {runpro=ready->head;ready->head=ready->head->next; runpro->next=NULL;ready->totalpro--;busy=1;Run(runpro); /*画进入cpu*/ drawQ(1);sleep(1);if(evhead->ifblock==1)blockev(runpro);else{runpro->runtime--;if(runpro->runtime==0)overev(runpro); /*产生结束事件*/elsereturnev(runpro);}}break;case 3: /*激活事件*/ {wakepro=block->head;block->head=block->head->next;wakepro->next=NULL;block->totalpro--;wakeup(wakepro);/*移动激活进程*/if(block->totalpro!=0){drawQ(2);sleep(1);}Orderp(1,wakepro);drawQ(1);sleep(1);if(ready->head==wakepro){if(busy==0)Runevent();elseif((Mode==2)&&(Algorithm==2)&&(wake pro->prior>runpro->prior)){Interupt(runpro);returnev(runpro);}}wakepro=NULL;if(block->totalpro!=0)wakeupev();}break;case 4: /*结束事件*/{over(runpro);runpro=NULL;busy=0;if(ready->totalpro!=0)Runevent();}break;case 5: /*阻塞事件*/ {blocked(runpro);busy=0;if(ready->totalpro!=0)Runevent();Orderp(2,runpro);drawQ(2);sleep(1);if(block->head==runpro)wakeupev();runpro=NULL;}break;case 6: /*返回事件*/ {returnwait(runpro); /*画出返回*/ busy=0;Orderp(1,runpro);runpro=NULL;drawQ(1);sleep(1);Runevent();}break;}evhead=evhead->next;}setcolor(LIGHTRED);settextstyle(1,0,0);setusercharsize(1, 1, 2, 1);outtextxy(290,340,"END!");setcolor(15);settextstyle(1,0,1);outtextxy(105,420,"Author : ZHOUWUBO CLASS : 01-1 NO.17");getch();getch();closegraph();}/*============================================ =====================*/menu() /*选择菜单*/{char wrong;textbackground(LIGHTBLUE);clrscr();window(20,3,60,23);textbackground(LIGHTRED);textcolor(YELLOW);clrscr();gotoy(2,2);cprintf("Please select the Scheduling Mode:");gotoxy(5,3);cprintf("1.Non-Preemptive");gotoxy(5,4);cprintf("2.Preemptive");gotoxy(7,5);cprintf(">> ");cscanf("%d",&Mode);switch(Mode){case 1:{gotoxy(2,7);cprintf("Please select the Scheduling Algorithm:");gotoxy(5,8);cprintf("1.FCFS");gotoxy(5,9);cprintf("2.SPF");gotoxy(7,10);cprintf(">> ");cscanf("%d",&Algorithm);if((Algorithm!=1)&&(Algorithm!=2)){gotoxy(2,12);cprintf("Your select is wrong! Run once again!");scanf("%c",&wrong);}break;}case 2:{gotoxy(2,7);cprintf("Please select the Scheduling Algorithm:");gotoxy(5,8);cprintf("1.Round Robin");gotoxy(5,9);cprintf("2.Priority");gotoxy(7,10);cprintf(">> ");cscanf("%d",&Algorithm);if((Algorithm!=1)&&(Algorithm!=2)){gotoxy(2,12);cprintf("Your select is wrong! Run once again!");scanf("%c",&wrong);}break;}default:{gotoxy(2,7);cprintf("Your select is wrong! Run once again!"); scanf("%c",&wrong);}}/*-----------------------------------------------------------------*/ welcome() /*显示欢迎屏幕*/{int i;setcolor(14);outtextxy(45,178,"create");outtextxy(40,188,"process");outtextxy(555,210,"Finish");line(100,204,170,204);line(170,204,160,199);line(160,199,165,204);line(100,226,170,226);line(170,226,160,231);line(160,231,165,226);rectangle(370,40,420,60);rectangle(425,40,475,60);rectangle(480,40,530,60);rectangle(535,40,588,60);setcolor(LIGHTRED);if((Mode==1)&&(Algorithm==1))int arw[8]={395,62,385,77,405,77,395,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==1)&&(Algorithm==2)) {int arw[8]={450,62,440,77,460,77,450,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==2)&&(Algorithm==1)) {int arw[8]={505,62,495,77,515,77,505,62};setlinestyle(0, 0, 3);drawpoly(4,arw);}if((Mode==2)&&(Algorithm==2)) {int arw[8]={561,62,551,77,571,77,561,62};setlinestyle(0, 0, 3);drawpoly(4,arw);setlinestyle(0, 0, 1);settextstyle(2,0,7);outtextxy(373,40,"FCFS");outtextxy(434,40,"SPF");outtextxy(495,40,"RR");settextstyle(2,0,5);outtextxy(537,42,"Priorty"); settextstyle(1,0,0);setusercharsize(1, 1, 2, 1);outtextxy(82,18, "CPU SCHEDULING"); outtextxy(280,360,"start!");setcolor(14);line(220,204,230,204);line(230,204,230,200);line(230,200,380,200);line(380,200,380,204);line(380,204,390,204);line(390,204,424,180);line(424,180,424,170);line(456,180,456,100);line(456,100,180,100);line(180,204,175,194);line(175,194,180,199);line(456,180,490,204);line(490,204,510,204); settextstyle(1,0,1);outtextxy(275,180,"READY"); line(220,226,230,226);line(230,226,230,230);line(230,230,380,230);line(380,230,380,226);line(380,226,390,226);line(390,226,424,250);line(424,250,424,260);line(456,250,456,330);line(456,330,410,330);line(410,330,410,334);line(410,334,260,334);line(260,334,260,330);line(260,330,180,330);line(180,330,180,226);line(180,226,175,236);line(420,308,410,308);line(410,308,410,304);line(410,304,260,304);line(260,304,260,308);line(260,308,250,308);line(456,250,490,226);line(490,226,510,226);outtextxy(305,285,"BLOCK");circle(440,215,30);sleep(2);setcolor(LIGHTBLUE);for(i=0;i<=43;i++){line(280+i,360,280+i,420);line(365-i,360,365-i,420);delay(5000);}}/*-----------------------------------------------------------------*/ start() /*初始化*/{int i;int t;struct event *p1,*p2;p1=(struct event *)malloc(sizeof(struct event));p1->evtype=1;p1->pnum=1;p1->t=0;p1->ifblock=0; /*在进程产生事件中无意义*/ p1->next=NULL;t=p1->t;evhead=p1;randomize();for(i=2;i<=5;i++){p2=(struct event *)malloc(sizeof(struct event)); p2->evtype=1;p2->pnum=i;p2->t=t+rand()%2+1;p2->ifblock=0;p2->next=NULL;t=p2->t;p1->next=p2;p1=p1->next;}ready=(struct headnod*)malloc(sizeof(struct headnod));ready->head=NULL;ready->totalpro=0;ready->tail=NULL;block=(struct headnod*)malloc(sizeof(struct headnod));block->head=NULL;block->totalpro=0;block->tail=NULL;}/*-----------------------------------------------------------------*/ drawcreate(struct process *p) /*画出进程的产生过程*/ {void *buf;int i,size;setfillstyle(1,p->pnum);bar(56,206,55+10*p->plong,224);size=imagesize(55,205,56+10*p->plong,225);buf=malloc(size);getimage(55,205,56+10*p->plong,225,buf);for(i=0; i<134; i++){putimage(55+i,205,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ Orderp(int flag,struct process *p)/*1:就绪队列排队2:阻塞队列排队*/{struct process *q;if(flag==1) /*就绪队列*/{if(ready->totalpro==0){ready->head=p;ready->tail=p;ready->totalpro++;}else{if((Mode==1)&&(Algorithm==1)) /*FCFS*/{ready->tail->next=p;ready->tail=ready->tail->next;ready->totalpro++;}if((Mode==1)&&(Algorithm==2)) /*短进程优先*/{if(p->plong<ready->head->plong) /*长度小于当前队头*/{p->next=ready->head;ready->head=p;ready->totalpro++;}else /*长度大于当前队头*/{q=ready->head;while(q->next!=NULL){if(p->plong<q->next->plong) {p->next=q->next;q->next=p;ready->totalpro++;break;}elseq=q->next;}if(q->next==NULL){q->next=p;ready->totalpro++;ready->tail=p;}}}if((Mode==2)&&(Algorithm==1)) /*时间片轮转*/{ready->tail->next=p;ready->tail=ready->tail->next;ready->totalpro++;if((Mode==2)&&(Algorithm==2)) /*抢占优先级*/{if(p->prior>ready->head->prior) /*优先级高于当前队头*/{p->next=ready->head;ready->head=p;ready->totalpro++;}else /*优先级低于当前队头*/{q=ready->head;while(q->next!=NULL){if(p->prior>q->next->prior) {p->next=q->next;q->next=p;ready->totalpro++;break;elseq=q->next;}if(q->next==NULL) {q->next=p;ready->totalpro++;ready->tail=p;}}}}}if(flag==2) /*阻塞队列*/{if(block->totalpro==0){block->head=p;block->totalpro++;block->tail=p;}else{block->tail->next=p;block->tail=block->tail->next;block->totalpro++;}}}/*-----------------------------------------------------------------*/ drawQ(int flag) /*画出队列,flag=1:就绪队列,flag=2:阻塞队列*/{int i,j;struct process *p;if(flag==1){setcolor(LIGHTBLUE);for(i=0;i<=150;i++)line(230+i,205,230+i,225);for(i=0;i<=30;i++)line(189+i,203,189+i,227);p=ready->head;j=379;for(i=1;i<=ready->totalpro;i++) {setfillstyle(1,p->pnum);bar(j,206,j-10*p->plong+1,224);j=j-10*p->plong-1;p=p->next;}}if(flag==2){setcolor(LIGHTBLUE);for(i=0;i<=150;i++)line(260+i,309,260+i,39);for(i=0;i<=30;i++)line(420+i,308,420+i,329);p=block->head;j=261;for(i=1;i<=block->totalpro;i++) {setfillstyle(1,p->pnum);bar(j,309,j+10*p->plong-1,328);j=j+10*p->plong+1;p=p->next;}}}/*-----------------------------------------------------------------*/ Runevent() /*产生执行事件*/{struct event *p1;struct process *p;p=ready->head;randomize();p1=(struct event *)malloc(sizeof(struct event));p1->evtype=2;p1->pnum=p->pnum;p1->t=evhead->t;if(p->blocknum!=0){p1->ifblock=rand()%2;if(p1->ifblock==1)p->blocknum--;}elsep1->ifblock=0;p1->next=NULL;Orderev(p1);}/*-----------------------------------------------------------------*/ Interupt(struct process *p) /*中断当前正在执行的进程*/ {struct event *q1,*q2;if(evhead->next==NULL){if(evhead->evtype==4)p->runtime++;evhead=NULL;}else{q1=evhead->next;q2=evhead;}while(q1!=NULL){if(q1->pnum==p->pnum){if(q1->evtype==4)p->runtime++;q2->next=q1->next;break;}else{q2=q1;q1=q1->next;}}}/*-----------------------------------------------------------------*/ Orderev(struct event *q) /*在事件链表中插入结点*/ {struct event *p;p=evhead;while(p->next!=NULL){if(q->t<p->next->t){q->next=p->next;p->next=q;break;}elsep=p->next;}if(p->next==NULL)p->next=q;}/*----------------------------------------------------------------*/ blockev(struct process *p) /*产生阻塞事件*/{struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q->evtype=5;q->pnum=p->pnum;q->t=evhead->t+rand()%4+2;q->ifblock=0;q->next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ blocked(struct process *p) /*画出阻塞过程*/{int i,size;void *buf;setcolor(LIGHTBLUE);for(i=1;i<=28;i++)circle(440,215,i);setfillstyle(1,p->pnum);bar(426,251,426+10*p->plong-2,269);size=imagesize(425,250,426+10*p->plong-1,270);buf=malloc(size);getimage(425,250,426+10*p->plong-1,270,buf);for(i=0; i<59; i++){putimage(425,250+i,buf,COPY_PUT);delay(3500);}for(i=0;i<5;i++){putimage(425-i,308,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ wakeupev() /*产生唤醒事件*/{struct event *q;struct process *p;p=block->head;randomize();q=(struct event *)malloc(sizeof(struct event));q->evtype=3;q->pnum=p->pnum;q->t=evhead->t+rand()%3+4;q->ifblock=0; /*此时无意义*/q->next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ Run(struct process *p) /*画出执行*/int i,size;void *buf;size=imagesize(380,205,380-10*p->plong-1,225);buf=malloc(size);getimage(380,205,380-10*p->plong-1,225,buf);for(i=0; i<29; i++){putimage(380-10*p->plong-1+i,205,buf,COPY_PUT);delay(3500);}setcolor(LIGHTBLUE);for(i=0;i<29;i++)line(380+i,205,380+i,225);setcolor(p->pnum);for(i=1;i<=28;i++)circle(440,215,i);free(buf);}/*-----------------------------------------------------------------*/ overev(struct process *p) /*产生结束事件*/struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q->evtype=4;q->pnum=p->pnum;if((Mode==2)&&(Algorithm==1))q->t=evhead->t+2; /*时间片为2*/elseq->t=evhead->t+3*(p->plong);/*时间由进程长度确定*/q->ifblock=0;q->next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ returnev(struct process *p) /*产生返回事件*/{struct event *q;randomize();q=(struct event *)malloc(sizeof(struct event));q->evtype=6;q->pnum=p->pnum;if((Mode==2)&&(Algorithm==2))q->t=evhead->t;elseq->t=evhead->t+2; /*时间片为2*/q->ifblock=0;q->next=NULL;Orderev(q);}/*-----------------------------------------------------------------*/ wakeup(struct process *p)/*画出唤醒过程*/{int i,size;void *buf;size=imagesize(260,309,261+10*p->plong,329);buf=malloc(size);getimage(260,309,261+10*p->plong,329,buf);for(i=0; i<72; i++){putimage(260-i,309,buf,COPY_PUT);delay(3500);}for(i=0;i<104;i++){putimage(189,309-i,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ returnwait(struct process *p)/*画出返回过程*/{int i,size;void *buf;setcolor(LIGHTBLUE);for(i=1;i<=28;i++)circle(440,215,i);setfillstyle(1,p->pnum);bar(426,161,426+10*p->plong-2,179);size=imagesize(425,160,426+10*p->plong-1,180);buf=malloc(size);getimage(425,160,426+10*p->plong-1,180,buf);for(i=0; i<59; i++){putimage(425,160-i,buf,COPY_PUT);delay(3500);}for(i=0;i<237;i++){putimage(425-i,101,buf,COPY_PUT);delay(3500);}for(i=0;i<105;i++){putimage(189,101+i,buf,COPY_PUT);delay(3500);}free(buf);}/*-----------------------------------------------------------------*/ over(struct process *p) /*画出结束过程*/{int i,size;void *buf;int k=evhead->pnum;setcolor(LIGHTBLUE);for(i=1;i<=28;i++)circle(440,215,i);setfillstyle(1,k);bar(476,206,476+10*p->plong,224);size=imagesize(475,205,476+10*p->plong+1,225);buf=malloc(size);getimage(475,205,476+10*p->plong+1,225,buf);for(i=0; i<=45; i++){putimage(475+i,205,buf,COPY_PUT);delay(4000);}setcolor(LIGHTBLUE);for(i=0;i<=31;i++){line(520+i,205,520+i,225);delay(4000);}free(buf);}。
#include<stdio.h>#include<stdlib.h>#include <conio.h>#include<math.h>#define N 20#define MAX 100typedef struct PCB //pcb进程控制块定义{int num[N]; //进程序号char name[10]; //进程名char state; //进程状态int tijiaotime; //进程到达时间int runtime; //进程开始时间int finishtime; //进程结束时间int needtime; //服务时间int pro;//进程优先级struct PCB *next; //链接指针指向下个作业的}pcb;struct PCB *head_input;struct PCB *head_run;struct PCB *head_run_pre;unsigned long current; //记录系统当前时间的变量int time=10000,n; //计时器pcb *head=NULL,*p,*q;void getInfo() //创建进程{int num;printf("\n请输入要建立的进程个数:");scanf("%d",&n);for(num=0;num<n;num++){p=(pcb *)malloc(sizeof(pcb));if(head==NULL) {head=p;q=p;}printf("依次输入:\n进程号进程名到达时间服务时间 \n");scanf("%s\t%s\t%d\t%d",&p->num,&p->name,&p->tijiaotime,&p->needtime);if(p->tijiaotime < time) time=p->tijiaotime;q->next=p;p->runtime=0;p->finishtime=0;p->next=NULL;p->state='W';q=p;}}// *********************1.先来先服务调度算法******************************* void run_fcfo(pcb *p1)//定义先来先到服务的算法{time = p1->tijiaotime > time? p1->tijiaotime:time;p1->runtime=time;printf("\n现在时间是%d,开始运行进程%s\n",time,p1->name);time+=p1->needtime;p1->state='F';p1->finishtime=time;printf("进程名开始时间所需时间结束时间\n");printf("%s %d %d %d ",p1->name,p1->runtime,p1->needtime,p1->finishtime); }void fcfo()//定义运行进程函数{int i,j,t;for(j=0;j<n;j++){p=head;t=10000;for(i=0;i<n;i++) //找到当前未完成的进程{if(p->tijiaotime<t && p->state=='W'){t=p->tijiaotime;q=p; //标记当前未完成的进程}p=p->next;}run_fcfo(q);}}// ************************2.优先级调度服务算法************************************int readydata(){ //建立就绪队列if(head_input->next==NULL){return 0;}struct PCB *p1=head_input->next,*pmax,*p2;int maxpro=0xffff;pmax=p1;p2=head_input;while(p1!=NULL){if(p1->pro<maxpro){maxpro=p1->pro;head_run_pre=p2;pmax=p1;}p2=p1;p1=p1->next;}head_run=pmax;head_run_pre->next=head_run->next;return 1;}void runprocess() //运行进程函数{head_run->runtime-=10;head_run->pro++;struct PCB *p1,*p2;printf("时间片的大小 %d",current);current+=10;printf(" %s 开始\n",head_run->name);printf("时间片的大小 %d",current);printf(" %s 结束\n",head_run->name);if(head_run->runtime<=0){//判断进程是否运行结束}else{p1=head_input;p2=head_input->next;p1->next=head_run;head_run->next=p2;}}int readyprocess(){while(1){if(readydata()==0)return 0;else runprocess();}}void Init(){head_input=new PCB;head_input->next=NULL;current=0;int numpro;printf("请重新输入要建立的进程个数:");scanf("%d",&numpro);printf("请依次输入进程名运行时间优先级\n");for(int i=0;i<numpro;i++){struct PCB *p1=new PCB;scanf("%s",p1->name);scanf("%d",&p1->runtime);scanf("%d",&p1->pro);p1->state='C';p1->next=NULL;struct PCB *p2=head_input->next;head_input->next=p1;p1->next=p2;}}// ************************3.时间片轮转调度服务算法************************************ void shijianpian(){ int b,i,X,t,k;int a[MAX];//存放进程的剩余时间int cnt[MAX];//存放进程调度次数printf("请输入进程数:");scanf("%d",&X);printf("\n请输入时间片t大小:");scanf("%d",&t);printf("\n请依次输入各个进程的服务时间");for(i=0;i<X;i++){scanf("%d",&a[i]);cnt[i]=0;}printf("被调度进程\t进程调度次数 \t本次运行时间结果\t剩余时间\n");k=1;while(k){for(i=0;i<X;i++){if(a[i]!=0)if(a[i]>=t){a[i]-=t;b+=t;cnt[i]=cnt[i]+1;printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);}else{b=b+a[i];cnt[i]=cnt[i]+1;a[i]=0;printf("\n\t%d\t\t%d\t\t%d\t\t%d",i+1,cnt[i],b,a[i]);}else continue;}for(i=0;i<X;i++)if(a[i]!=0){ k=1;break;}else continue;if(i>=X)k=0;}}void main(){printf(" *******************************");printf("\n 1. 按先来先到服务调度的算法模拟\n"); printf(" *******************************");getInfo();fcfo();printf("\n *******************************");printf("\n 2. 按优先级调度的算法模拟\n");printf("\n *******************************\n"); Init();readyprocess();printf("\n *******************************");printf("\n 3. 按时间片轮转调度的算法模拟\n");printf(" *******************************\n"); shijianpian();printf(" \n");}。
沈阳航空航天大学课程设计报告课程设计名称:C语言课程设计课程设计题目:车辆调度调度程序设计院(系):计算机学院专业:计算机科学与技术班级:04010101学号:**************名:***指导教师:丛丽晖完成日期:2010年3月17日目录第1章概要设计 (2)1.1题目的内容与要求 (2)1.2总体结构 (2)第2章详细设计 (4)2.1主程序模块 (4)2.2车辆调用模块 (5)2.3车辆归还模块 (6)2.4车辆总览模块 (7)2.5车辆查询模块 (8)2.6新车登记模块 (9)2.7车辆注销模块 (10)第3章调试分析 (11)3.1调试前期 (11)3.2调试中期 (12)3.3调试后期 (12)第4章使用说明与执行结果 (13)4.1车辆调度: (13)4.2车辆归还: (14)4.3车况一览: (16)4.4车辆查询: (16)4.5新车登陆: (17)4.6车辆注销: (18)4.7退出系统: (18)参考文献 (19)附录(程序清单) (20)第1章概要设计1.1题目的内容与要求内容:用文件系统设计实现一个简单的车辆调度系统。
实现用车要求的登记、车辆的管理等功能。
建立车辆基本的情况表,内容包含车辆所有信息,包括车辆目前状态,认为司机和车辆是一一对应的,在用户要求时进行信息动态匹配,并且可以智能选择最合理的车辆资源满足用户要求,为简单起见,可以不考虑司机的情况即仅对车辆进行管理,程序最终输出结果是:本次调出的车辆或者司机。
要求:1)系统利用C语言实现;2)车辆调用或归还结束后,需要把新的车辆状况保存到文本文件中;3)采用VC环境进行调试运行。
1.2总体结构本程序主要分为七个模块(功能模块图见图1.1):主程序模块,车辆调用模块,车辆归还模块,车辆总览模块,车辆查询模块,新车登记模块,车辆注销模块。
主程序模块:用于实现整个程序功能引导。
车辆调用模块:实现车辆调用功能。
车辆归还模块:实现车辆归还功能。
处理机模拟调度课程设计一、课程目标知识目标:1. 让学生理解处理机调度的基本概念,掌握常见的调度算法及其原理。
2. 使学生掌握模拟调度过程中的各个阶段,了解其相互关系和影响。
3. 帮助学生掌握分析调度算法性能的方法,能够从理论上评估算法的优劣。
技能目标:1. 培养学生运用所学调度算法进行模拟调度的实际操作能力。
2. 培养学生运用编程语言实现调度算法,解决实际调度问题的能力。
3. 提高学生分析调度算法性能,提出改进措施的能力。
情感态度价值观目标:1. 培养学生对处理机调度研究的兴趣,激发学生的学习热情。
2. 培养学生的团队合作意识,提高学生在团队中的沟通与协作能力。
3. 培养学生严谨的科学态度,树立正确的价值观,认识到技术发展对社会进步的重要性。
本课程针对高中年级学生,课程性质为理论与实践相结合。
在分析课程性质、学生特点和教学要求的基础上,将课程目标分解为具体的学习成果,以便后续的教学设计和评估。
通过本课程的学习,学生将能够掌握处理机调度相关知识,具备解决实际调度问题的能力,并形成积极的情感态度价值观。
二、教学内容本课程教学内容主要包括以下几部分:1. 处理机调度基本概念:介绍处理机调度的定义、作用和分类,以及调度过程中涉及的关键术语。
2. 常见调度算法:讲解先来先服务(FCFS)、短作业优先(SJF)、优先级调度、时间片轮转等调度算法的原理和实现。
3. 调度算法性能分析:分析各种调度算法的性能指标,如平均等待时间、平均周转时间、响应时间等。
4. 模拟调度实验:设计实验,使学生通过实际操作了解调度算法的执行过程,学会使用编程语言实现调度算法。
5. 教学案例分析与讨论:分析实际调度案例,引导学生运用所学知识解决实际问题,并提出改进措施。
教学内容安排如下:第一课时:处理机调度基本概念第二课时:先来先服务(FCFS)调度算法第三课时:短作业优先(SJF)调度算法第四课时:优先级调度和时间片轮转调度算法第五课时:调度算法性能分析第六课时:模拟调度实验第七课时:教学案例分析与讨论教学内容与课本紧密关联,按照教学大纲逐步推进,确保学生能够系统地掌握处理机调度的相关知识。
操作系统课程设计---作业调度模拟《操作系统课程设计作业调度模拟》在计算机科学领域,操作系统是管理计算机硬件与软件资源的核心系统,而作业调度则是操作系统中的一个关键环节。
作业调度的主要任务是根据一定的策略,将系统中的作业合理地分配到处理机上执行,以提高系统的资源利用率和作业的执行效率。
本次课程设计的目标就是模拟实现一个简单的作业调度系统,通过对作业调度算法的设计和实现,深入理解操作系统中作业调度的原理和机制。
首先,让我们来了解一下作业调度的基本概念和常见的调度算法。
作业调度的主要目标是在多个作业之间合理地分配处理机资源,以满足不同作业的执行需求。
常见的作业调度算法包括先来先服务(FCFS)、短作业优先(SJF)、优先级调度、高响应比优先调度等。
先来先服务算法是最简单的调度算法,它按照作业到达系统的先后顺序进行调度。
这种算法的优点是实现简单、公平,但缺点是对于长作业可能会导致短作业长时间等待,从而影响系统的整体性能。
短作业优先算法则优先调度执行时间短的作业。
这种算法能够有效地减少作业的平均等待时间,提高系统的吞吐量,但可能会导致长作业长时间得不到调度,出现“饥饿”现象。
优先级调度算法为每个作业赋予一个优先级,系统根据优先级的高低来进行调度。
优先级可以根据作业的紧急程度、重要性等因素来确定。
这种算法能够灵活地满足不同作业的需求,但需要合理地设置优先级,否则可能会导致调度的不公平。
高响应比优先调度算法则综合考虑了作业的等待时间和执行时间,通过计算响应比来决定调度顺序。
响应比的计算公式为:响应比=(等待时间+执行时间)/执行时间。
这种算法在一定程度上避免了短作业优先算法中长作业的“饥饿”问题,同时也能保证短作业的优先调度。
在本次课程设计中,我们选择使用 C 或 C++语言来实现作业调度模拟系统。
系统的主要功能包括作业的创建、作业队列的管理、调度算法的实现以及作业执行情况的输出。
为了实现作业的创建,我们定义了一个作业结构体,其中包含作业的编号、到达时间、执行时间、优先级等信息。
操作系统课程设计报告学校:广州大学学院:计算机科学与教育软件学院班级:计算机127班课题:处理机调度程序任课老师:陶文正、陈文彬姓名:黄俊鹏学号:1200002111班内序号:27成绩:日期:2015年1月6日一、设计目的在多道程序和多任务系统中,系统内同时处于就绪状态的进程可能有若干个。
也就是说能运行的进程数大于处理机个数。
为了使系统中的进程能有条不紊地工作,必须选用某种调度策略,选择一进程占用处理机。
要求学生设计一个模拟处理机调度算法,以巩固和加深处理机调度的概念。
二、设计要求1)进程调度算法包括:时间片轮转法,短作业优先算法,动态优先级算法。
2)可选择进程数量3)本程序包括三种算法,用C语言实现,执行时在主界面选择算法(可用函数实现)(进程数,运行时间,优先数由随机函数产生)执行,显示结果。
三、设计思路及算法思想1.界面菜单选项一级菜单提供2个选项:①自动生成进程数量②手动输入所需进程数量一级菜单选择完毕后进入二级菜单:①重新生成进程②时间片轮转法③短作业优先算法④动态优先级算法⑤退出程序2.调度算法程序所用PCB结构体需要用到的进程结构体如上图所示1)时间片轮转法主要是设置一个当前时间变量,curTime和时间片roundTime。
遍历进程组的时候,每运行一个进程,就把curTime += roundTime。
进程已运行时间加roundTime2)短作业优先算法遍历进程组,找到未运行完成并且运行时间最短的进程,让它一次运行完成,如此往复,直到所有进程都运行完成为止。
3)动态优先级算法做法跟短作业优先算法类似,此处主要是比较进程的优先数,优先级高者,先执行。
直到全部执行完毕。
当一个进程运行完毕后,适当增减其余进程的优先数,以达到动态调成优先级的效果。
3.程序流程图四、运行截图1)启动后输入5,生成5个进程2)输入1,选择时间片轮转法。
自动输出结果,分别是时间片为1和4的结果3)输入2,选择短作业优先算法4)输入3,选择动态优先级算法5)输入0,重新生成进程,再输入3,生成3个进程,选择2.短作业优先算法6)输入q,退出五、心得体会通过这次实验,让我对操作系统的进程调度有了更进一步的了解。
目录1、设计目的意义 (2)1.1、目的意义 (2)1.2、实现目标 (2)2、设计方案 (3)2.1、软硬件环境 (3)2.2、开发工具 (3)2.3、思路 (3)3、程序功能模块设计 (4)3.1、总体模块 (4)3.2、部分模块 (4)3.3、详细功能描述 (6)4、程序总控流程图 (6)5、数据结构设计 (8)5.1、PCB结构 (8)5.2、进程状态结构 (8)5.3、控件结构 (9)6、程序代码结构 (9)7、程序主要代码解析 (10)8、测试数据及测试结果 (15)8.1、运行时部分界面 (15)8.2、数据测试记录 (17)9、设计过程中遇到的问题及解决方法 (18)10、结论 (18)10.1、系统实现情况 (18)10.2、系统特点 (18)10.3、设计体会及收获 (18)11、参考资料 (19)模拟进程调度功能的设计与实现1、设计目的意义1.1、目的意义●通过课程设计理解进程调度的概念,深入了解进程控制的功能、进程的创建、删除以及进程各个状态间的转换过程;实现先来先服务、时间片轮转、最短作业优先、优先级调度算法对进程进行的调度过程;通过观察有关的队列结构的内容的动态变化过程深入体会各个调度算法的特点;从而能够更好的巩固从书本上学到的知识。
●编程过程中需要建立队列等结构进行各种操作,通过该次课程设计,我们更加从实用的角度对《数据结构》课程内容进行更深入理解和更熟练的应用。
●使用C++语言进行编程,通过对调度功能的编程实现,不但能有效训练我们对编程语言的熟练使用,还能促进我们独立思考解决问题、以及独立查新获取知识的能力。
1.2、实现目标●一个进程的生命期可以划分为一组状态,这些状态刻画了整个进程。
系统根据PCB结构中的状态值控制过程。
在进程的生命期内,一个进程至少具有5种基本状态,它们是:初始态、执行状态、等待状态、就绪状态和终止状态。
通过系统设计,实现进程相关数据结构的创建和查看功能;实现多种进程调度算法:先来先服务算法、优先级调度算法、时间片轮转法等;实现对执行进程的阻塞,对等待进程的唤醒等功能。
南通大学计算机学院操作系统课程设计报告书设计题目处理器调度算法模拟专业班级软嵌161学生姓名候玉权学号**********指导教师丁红日期2018.07.05实验报告正文内容1、课程设计的目的在采用多道程序设计的系统中,往往有若干个进程同时处于就绪状态。
当就绪状态进程个数大于处理器数时,就必须依照某种策略来决定哪些进程优先占用处理器。
本实验模拟在单处理器情况下处理器调度,帮助学生加深了解处理器调度的工作。
2、课程设计的要求用C++语言编写程序完成单处理机的进程调度3、课程设计的环境在机房完成,运行环境vs20174、问题描述对cpu调度算法进行模拟实现,包含先来先服务,短作业优先,响应比最高优先,时间片轮转等算法。
5、算法分析数据结构struct ef{char name[10]; //进程名称int number; //进程编号float daodatime; //到达时间float kaishitime; //开始运行时间float yunxingtime; //运行时间float jieshutime; //运行结束时间int shengyutime; // 剩余时间int fuwutime; //服务时间int order; //运行次序int run_flag; //调度标志int priority; //优先级struct ef *next;}先来先服务算法int fcfs() //先来先服务{float tp = 0;int i;int zx;tp = ef[0].daodatime;for (i = 0; i<c; i++){ef[i].kaishitime = tp;ef[i].jieshutime = ef[i].kaishitime + ef[i].yunxingtime;ef[i].run_flag = 1;tp = ef[i].jieshutime;zx = i;ef[zx].order = i + 1;}return 0;}短作业优先int sjf() //短作业优先{float temp_time = 0;int i = 0, j;int zx, tc;float yunxingtime;yunxingtime = ef[i].yunxingtime;j = 1;while ((j<c) && (ef[i].daodatime == ef[j].daodatime)){if (ef[j].yunxingtime<ef[i].yunxingtime){yunxingtime = ef[j].yunxingtime;i = j;}j++;} //查找第一个被调度的进程//对第一个被调度的进程求相应的参数zx = i;ef[zx].kaishitime = ef[zx].daodatime;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].jieshutime;ef[zx].order = 1;tc = 1;while (tc<c){for (j = 0; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag)){yunxingtime = ef[j].yunxingtime; zx = j; break;}}for (j = 0; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag))if (ef[j].yunxingtime<yunxingtime){yunxingtime = ef[j].yunxingtime;zx= j;}}//查找下一个被调度的进程//对找到的下一个被调度的进程求相应的参数ef[zx].kaishitime = temp_time;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].yunxingtime;tc++;ef[zx].order = tc;}return 0;}响应比高优先int hrrf() //响应比高优先{int j, zx, tc;float temp_time, respond_rate, max_respond_rate;//第一个进程被调度ef[0].kaishitime = ef[0].daodatime;ef[0].jieshutime = ef[0].kaishitime + ef[0].yunxingtime;temp_time = ef[0].jieshutime;ef[0].run_flag = 1;ef[0].order = 1;tc = 1;//调度其他进程while (tc<c){max_respond_rate = 0;for (j = 1; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag)){respond_rate = (temp_time - ef[j].daodatime) / ef[j].yunxingtime;if (respond_rate>max_respond_rate){max_respond_rate = respond_rate;zx = j;}}}//找响应比高的进程ef[zx].kaishitime = temp_time;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;temp_time = ef[zx].jieshutime;ef[zx].run_flag = 1;tc+= 1;ef[zx].order = tc;}return 0;}时间片轮struct ef *input(){int N, i;// 定义队首、队尾struct ef *head, *rear;// p是队尾指针,q是队首指针,t是执行时间struct ef *p, *q, *t;// 初始化队首和队尾为空head = rear = NULL;cout <<"请输入进程数目:"<<endl;cin>> N;for (i = 0; i < N; i++){// 初始化一个空间给进程p = (struct ef *)malloc(sizeof(struct ef));cout <<"请输入第"<< i+1 <<"个进程的名字\n"<<endl;cin >> p->name;cout <<"请输入第"<< i + 1 <<"个进程的编号:\n"<< endl;cin >>p->number;cout <<"请输入第"<< i + 1 <<"个进程的到达时间:\n"<< endl;cin >>p->daodatime;cout <<"请输入第"<< i + 1 <<"个进程的服务时间:\n"<< endl;cin >> p->fuwutime;p->shengyutime = p->fuwutime;p->next = NULL;// 当输入结束时,把p的数据放到队首,以便下一步执行if (rear == NULL){head = p;p->next = NULL;rear = p;}// 否则执行时间为空,队首变成qelse{t = NULL;q = head;// 当q和q的到达时间小于p的到达时间时,把执行时间给qwhile (q && q->daodatime < p->daodatime){t = q;q = q->next;}// 当q是队首时,则下一个队首变成p,以便每个进程都能够得到时间片if (q == head){p->next = head;head = p;}// 当执行时间片到达队尾时(执行完成),返回给队首pelse if (t == rear){rear->next = p;p->next = NULL;rear = p;}// 否则给队首p占用执行时间,p执行完后到qelse{t->next = p;p->next = q;}}}// 返回队首return head;}void run(struct ef *head){struct ef *p, *t, *r;int num;// 运行过程vector<string> vec_out;cout<<"请输入时间片:"<<endl;cin>>num;// 当队首不为空时,把p给队首while (head != NULL){r = p = head;// 把执行时间给队首while (p != NULL){t = head;// p的剩余时间 = 剩余时间 - 时间片p->shengyutime = p->shengyutime - num;p->jieshutime = p->kaishitime + p->fuwutime;string s = p->name;vec_out.push_back(s);// 当p运行完,即剩余时间小于0时,仍然把它当做0处理if (p->shengyutime < 0)p->shengyutime = 0;cout<<"进程编号到达时间服务时间剩余时间完成时间\n"<<endl;//时间不为空时,输出当前进程的信息,并把时间片交给下一个进程while (t != NULL){cout <<" "<< t->name <<" "<<t->number <<" "<<t->daodatime <<" "<< t->fuwutime <<" "<< t->shengyutime <<" "<<t->jieshutime <<endl;t = t->next;}//当队首的剩余时间为0时,先把队首改成p的下一个,然后释放内存,删除队首节点if (p->shengyutime == 0){if (p == head){head = p->next;free(p);p = head;}//否则返回执行,把队尾的下一个指针变成p的下一个指针,队尾的位置移动到队首else{r->next = p->next;p = r->next;r = p;}}//否则把队首的位置给队尾,把队首的状态显示为“就绪”状态else{r = p;p = p->next;}}}cout<<"执行顺序:\n"<<endl;cout << vec_out[0].c_str()<<endl;for (int i = 1; i<vec_out.size(); i++){cout << vec_out[i].c_str()<<endl;}}void yunxing(){//定义时间片的队首结构体struct ef *head;// 队首执行的时间head =input();run(head);}7、程序源代码#include"stdafx.h"#include<iostream>#include<vector>using namespace std;#define MAX 10int c; //实际进程个数int fcfs(); //先来先服务int sjf(); //短作业优先int hrrf(); //响应比高优先int yxj(); //优先级调度int in(); //进程参数输入int out(); //调度结果输出struct ef{char name[10]; //进程名称int number; //进程编号float daodatime; //到达时间float kaishitime; //开始运行时间float yunxingtime; //运行时间float jieshutime; //运行结束时间int shengyutime; // 剩余时间int fuwutime; //服务时间int order; //运行次序int run_flag; //调度标志int priority; //优先级struct ef *next;}ef[MAX];int fcfs() //先来先服务{float tp = 0;int i;int zx;tp = ef[0].daodatime;for (i = 0; i<c; i++){ef[i].kaishitime = tp;ef[i].jieshutime = ef[i].kaishitime + ef[i].yunxingtime;ef[i].run_flag = 1;tp = ef[i].jieshutime;zx = i;ef[zx].order = i + 1;}return 0;}int yxj() //优先级调度{float temp_time = 0;int i = 0, j;int zx, tc;int max_priority;max_priority = ef[i].priority;j = 1;while ((j<c) && (ef[i].daodatime == ef[j].daodatime)){if (ef[j].priority>ef[i].priority){max_priority = ef[j].priority;i = j;}j++;} //查找第一个被调度的进程//对第一个被调度的进程求相应的参数zx = i;ef[zx].kaishitime = ef[zx].kaishitime;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].jieshutime;ef[zx].order = 1;tc = 1;while (tc<c){max_priority = 0;for (j = 0; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag))if (ef[j].priority>max_priority){max_priority = ef[j].priority;zx = j;}}//查找下一个被调度的进程////对找到的下一个被调度的进程求相应的参数ef[zx].kaishitime = temp_time;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].jieshutime;tc++;ef[zx].order = tc;}return 0;}int sjf() //短作业优先{float temp_time = 0;int i = 0, j;int zx, tc;float yunxingtime;yunxingtime = ef[i].yunxingtime;j = 1;while ((j<c) && (ef[i].daodatime == ef[j].daodatime)){if (ef[j].yunxingtime<ef[i].yunxingtime){yunxingtime = ef[j].yunxingtime;i = j;}j++;} //查找第一个被调度的进程//对第一个被调度的进程求相应的参数zx = i;ef[zx].kaishitime = ef[zx].daodatime;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].jieshutime;ef[zx].order = 1;tc = 1;while (tc<c){for (j = 0; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag)){yunxingtime = ef[j].yunxingtime; zx = j; break;}}for (j = 0; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag))if (ef[j].yunxingtime<yunxingtime){yunxingtime = ef[j].yunxingtime;zx= j;}}//查找下一个被调度的进程//对找到的下一个被调度的进程求相应的参数ef[zx].kaishitime = temp_time;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;ef[zx].run_flag = 1;temp_time = ef[zx].yunxingtime;tc++;ef[zx].order = tc;}return 0;}int hrrf() //响应比高优先{int j, zx, tc;float temp_time, respond_rate, max_respond_rate;//第一个进程被调度ef[0].kaishitime = ef[0].daodatime;ef[0].jieshutime = ef[0].kaishitime + ef[0].yunxingtime;temp_time = ef[0].jieshutime;ef[0].run_flag = 1;ef[0].order = 1;tc = 1;//调度其他进程while (tc<c){max_respond_rate = 0;for (j = 1; j<c; j++){if ((ef[j].daodatime <= temp_time) && (!ef[j].run_flag)){respond_rate = (temp_time - ef[j].daodatime) / ef[j].yunxingtime;if (respond_rate>max_respond_rate){max_respond_rate = respond_rate;zx = j;}}}//找响应比高的进程ef[zx].kaishitime = temp_time;ef[zx].jieshutime = ef[zx].kaishitime + ef[zx].yunxingtime;temp_time = ef[zx].jieshutime;ef[zx].run_flag = 1;tc+= 1;ef[zx].order = tc;}return 0;}int in() //进程参数输入{int i;cout <<"请输入进程数目:\n"<<endl;cin>>c;for (i = 0; i<c; i++){cout <<"请输入第"<< i+1 <<"个进程的名字\n"<<endl;cin>> ef[i].name;cout <<"请输入第"<< i + 1 <<"个进程的编号:\n"<< endl;cin>> ef[i].number;cout <<"请输入第"<< i + 1 <<"个进程的到达时间:\n"<< endl;cin >> ef[i].daodatime;cout <<"请输入第"<< i + 1 <<"个进程的运行时间:\n"<< endl;cin >> ef[i].yunxingtime;ef[i].kaishitime = 0;ef[i].jieshutime = 0;ef[i].order = 0;ef[i].run_flag = 0;}return 0;}int out() /*调度结果输出*/{int i;float turn_round_time = 0, f1, w = 0;cout<<"进程名编号到达时间运行时间开始时间结束时间平均周转时间平均带权周转时间\n"<<endl;for (i = 0; i<c; i++){f1 = ef[i].jieshutime - ef[i].daodatime;turn_round_time += f1;w += (f1 / ef[i].yunxingtime);cout <<" "<< ef[i].name <<" "<< ef[i].number <<" "<<ef[i].daodatime <<" "<< ef[i].yunxingtime <<" "<< ef[i].kaishitime <<" "<< ef[i].jieshutime <<" "<< turn_round_time / c<<" "<< w / c<<endl;}//cout << "平均周转时间=\n"<<turn_round_time / c<<endl;//cout << "带权周转时间=\n"<< w / c<<endl;return 0;}struct ef *input(){int N, i;// 定义队首、队尾struct ef *head, *rear;// p是队尾指针,q是队首指针,t是执行时间struct ef *p, *q, *t;// 初始化队首和队尾为空head = rear = NULL;cout <<"请输入进程数目:"<<endl;cin>> N;for (i = 0; i < N; i++){// 初始化一个空间给进程p = (struct ef *)malloc(sizeof(struct ef));cout <<"请输入第"<< i+1 <<"个进程的名字\n"<<endl;cin >> p->name;cout <<"请输入第"<< i + 1 <<"个进程的编号:\n"<< endl;cin >>p->number;cout <<"请输入第"<< i + 1 <<"个进程的到达时间:\n"<< endl;cin >>p->daodatime;cout <<"请输入第"<< i + 1 <<"个进程的服务时间:\n"<< endl;cin >> p->fuwutime;p->shengyutime = p->fuwutime;p->next = NULL;// 当输入结束时,把p的数据放到队首,以便下一步执行if (rear == NULL){head = p;p->next = NULL;rear = p;}// 否则执行时间为空,队首变成qelse{t = NULL;q = head;// 当q和q的到达时间小于p的到达时间时,把执行时间给qwhile (q && q->daodatime < p->daodatime){t = q;q = q->next;}// 当q是队首时,则下一个队首变成p,以便每个进程都能够得到时间片if (q == head){p->next = head;head = p;}// 当执行时间片到达队尾时(执行完成),返回给队首pelse if (t == rear){rear->next = p;p->next = NULL;rear = p;}// 否则给队首p占用执行时间,p执行完后到qelse{t->next = p;p->next = q;}}}// 返回队首return head;}void run(struct ef *head){struct ef *p, *t, *r;int num;// 运行过程vector<string> vec_out;cout<<"请输入时间片:"<<endl;cin>>num;// 当队首不为空时,把p给队首while (head != NULL){r = p = head;// 把执行时间给队首while (p != NULL){t = head;// p的剩余时间 = 剩余时间 - 时间片p->shengyutime = p->shengyutime - num;p->jieshutime = p->kaishitime + p->fuwutime;string s = p->name;vec_out.push_back(s);// 当p运行完,即剩余时间小于0时,仍然把它当做0处理if (p->shengyutime < 0)p->shengyutime = 0;cout<<"进程编号到达时间服务时间剩余时间完成时间\n"<<endl;//时间不为空时,输出当前进程的信息,并把时间片交给下一个进程while (t != NULL){cout <<" "<< t->name <<" "<<t->number <<" "<<t->daodatime <<" "<< t->fuwutime <<" "<< t->shengyutime <<" "<<t->jieshutime <<endl;t = t->next;}//当队首的剩余时间为0时,先把队首改成p的下一个,然后释放内存,删除队首节点if (p->shengyutime == 0){if (p == head){head = p->next;free(p);p = head;}//否则返回执行,把队尾的下一个指针变成p的下一个指针,队尾的位置移动到队首else{r->next = p->next;p = r->next;r = p;}}//否则把队首的位置给队尾,把队首的状态显示为“就绪”状态else{r = p;p = p->next;}}}cout<<"执行顺序:\n"<<endl;cout << vec_out[0].c_str()<<endl;for (int i = 1; i<vec_out.size(); i++){cout << vec_out[i].c_str()<<endl;}}void yunxing(){//定义时间片的队首结构体struct ef *head;// 队首执行的时间head =input();run(head);}int main(){int p;cout <<"请选择调度算法:"<< endl;cout <<"1.先来先服务"<< endl;cout <<"2.时间片轮调度"<< endl;cout <<"3.短作业优先"<< endl;cout <<"4.响应比高优先"<< endl;cout <<"5.优先级"<< endl;cout <<"6.退出"<< endl;cin >> p;switch (p){case 6:cout <<"运行结束。
《高级程序设计语言》课程设计报告题目:处理机低级调度模拟系统专业:网络工程班级: 10….学号: 00000000000姓名: *********指导教师: *******完成日期: 2013年 3月 30 日一、课程设计的目的1、掌握C语言数组、函数、指针、结构体的综合应用。
2、掌握使用C语言,进行应用性的开发。
3、掌握系统数据结构与算法的设计。
二、课程设计的内容课程设计题目:处理机低级调度模拟系统课程设计内容:根据操作系统处理机不同的调度算法,使用C语言模拟实现处理机调度过程。
1、系统数据结构(1)进程控制块(pcb):进程名称、到达时间、进程要求运行时间、进程已运行时间、指针、进程状态等等(要根据不同算法的需要定义全面的数据结构)(2)进程队列(PQueue):链表……2、调度算法(1)先来先服务调度(FCFS):按照进程提交给系统的先后顺序来挑选进程,先提交的先被挑选。
(2)多级反馈队列调度(FB,第i级队列的时间片=2i-1):(a)应设置多个就绪队列,并为各个队列赋予不同的优先级。
(b)当一个新进程进入内存后,首先将它放入第一队列的末尾,按FCFS的原则排队等待调度。
当轮到该进程执行时,如他能在该时间片内完成,便可准备撤离系统;如果它在一个时间片结束时尚未完成,调度程序便将该进程转入第二队列的末尾,再同样地按FCFS原则等待调度执行;如果它在第二队列中运行一个时间片后仍未完成,再依次将它放入第三队列……,如此下去,当一个长作业进程从第一队列依次降到第N队列后,在第N队列中便采取时间片轮转的方式运行。
(c)仅当第一队列空闲时,调度程序才调度第二队列中的进程运行。
三、课程设计的要求1、按照给出的题目内容(1)完成系统数据结构设计与实现、系统算法设计与实现、系统模块设计与实现、系统总体的设计与实现。
(2)系统需要一个简单操作界面,例如:===========================1. 先来先服务调度2. 多级反馈队列调度3. 退出(按数字1、2、3、,选择操作)===========================(3)对每种调度算法都要求输出每个进程(进程数不少于5)开始运行时刻、完成时刻、周转时间,以及这组进程的平均周转时间。
(4)画出每种调度算法流程图。
1.先来先服务调度:2.多级反馈队列调度:四:课程设计过程:1.系统中所使用的数据结构及说明……..数据结构的定义…………..定义进程控制块PCBstruct PCB{char name[10]; //进程名字float arrivetime; //进程到达时间float servicetime;//进程服务时间float super; //响应比float starttime; //开始运行时间float finishtime; //完成时间float TurnaroundTime; //周转时间char state; //进程的状态,W就绪态,R执行态,F完成态int prio; //优先级int round; //时间片int cputime; //cpu时间int needtime; //进程运行时间int count; //计数器struct PCB *next;}*ready=NULL,*p,*q;定义就绪队列:typedef struct Queue //多级就绪队列节点信息{PCB *LinkPCB; //就绪队列中的进程队列指针int prio; //本就绪队列的优先级int round; //本就绪队列所分配的时间片struct Queue *next; //指向下一个就绪队列的链表指针}ReadyQueue;2.系统功能结构本系统是处理机低级调度模拟系统,主要通过模拟来实现处理机调度,调度方式有先来先服务调度(FCFS)、短进程优先调度(SJF)、高响应比优先调度(HRN)、多级反馈队列调度(FB)四种调度方式。
系统运行过程如下:输入进程个数,输入进程详细信息,通过简单操作界面来选择调度方式,调度的过程和结果,重新选择调度方式或者选择结束。
3.程序清单及描述#define NULL 0#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <string.h>#include <windows.h>struct PCB{char name[10]; //进程名字float arrivetime; //进程到达时间float servicetime;//进程服务时间float super; //响应比float starttime; //开始运行时间float finishtime; //完成时间float TurnaroundTime; //周转时间char state; //进程的状态,W就绪态,R执行态,F完成态int prio; //优先级int round; //时间片int cputime; //cpu时间int needtime; //进程运行时间int count; //计数器struct PCB *next;}*ready=NULL,*p,*q;typedef struct Queue //多级就绪队列节点信息{PCB *LinkPCB; //就绪队列中的进程队列指针int prio; //本就绪队列的优先级int round; //本就绪队列所分配的时间片struct Queue *next; //指向下一个就绪队列的链表指针}ReadyQueue;PCB a[100];int N;void createProcess(PCB *p)//创建进程函数int i;printf("输入进程名& 到达时间& 服务时间:\n例如: a 0 100\n");for(i=0;i<=N-1;i++){printf("输入第%d个进程的信息:\n",i+1);scanf("%s%f%f",&p[i].name,&p[i].arrivetime,&p[i].servicetime);}}void sort(PCB *p,int N)//到达时间排序{for(int i=0;i<=N-1;i++)for(int j=0;j<=i;j++)if(p[i].arrivetime<p[j].arrivetime){PCB temp;temp=p[i];p[i]=p[j];p[j]=temp;}}void running(PCB *p, float arrivetime,float servicetime,float starttime,float finishtime,float &TurnaroundTime,int N)//计算进程时间{int k;for(k=0;k<=N-1;k++){if(k==0){p[k].starttime=p[k].arrivetime;p[k].finishtime=p[k].arrivetime+p[k].servicetime;}else{p[k].starttime=p[k-1].finishtime;p[k].finishtime=p[k-1].finishtime+p[k].servicetime;}}for(k=0;k<=N-1;k++){p[k].TurnaroundTime=p[k].finishtime-p[k].arrivetime;}}void print(PCB *p,float arrivetime,float servicetime,float starttime,float finishtime,float TurnaroundTime,int N)//进程输出各种时间{ int k;printf("运行次序:");printf("%s",p[0].name);for(k=1;k<N;k++){printf("-->%s",p[k].name);}printf("\n进程的信息:\n");printf("\n进程名\t到达\t服务\t开始\t完成\t周转\n");for(k=0;k<=N-1;k++){printf("%s\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t%-.2f\t\n",p[k].name,p[k].arrivetime,p[k].servicetime,p[k].starttime,p[ k].finishtime,p[k].TurnaroundTime);}}void fcfs(PCB *p)//先来先服务调度算法{system("cls");printf("请输入作业数:");scanf("%d",&N);while( N < 5) //进程小于5的话,重新选择输入{system("cls");printf("\n\7\7作业数小于5,重新输入:\n");printf("请输入作业数:");scanf("%d",&N);}createProcess(a);float arrivetime=0,servicetime=0,starttime=0,finishtime=0,TurnaroundTime=0;sort(p,N); //排序running(p,arrivetime,servicetime,starttime,finishtime,TurnaroundTime,N);//模拟运行print(p,arrivetime,servicetime,starttime,finishtime,TurnaroundTime,N);//打印输入结果}PCB *run=NULL,*finish=NULL; //定义三个队列,就绪队列,执行队列和完成队列ReadyQueue *Head = NULL; //定义第一个就绪队列int num; //进程个数int ReadyNum; //就绪队列个数void Output(); //进程信息输出函数void InsertFinish(PCB *in); //将进程插入到完成队列尾部void InsertPrio(ReadyQueue *in); //创建就绪队列,规定优先数越小,优先级越低void PrioCreate(); //创建就绪队列输入函数void GetFirst(ReadyQueue *queue); //取得某一个就绪队列中的队头进程void InsertLast(PCB *in,ReadyQueue *queue); //将进程插入到就绪队列尾部void ProcessCreate(); //进程创建函数void RoundRun(ReadyQueue *timechip); //时间片轮转调度算法void MultiDispatch(); //多级调度算法,每次执行一个时间片void Output() //进程信息输出函数{ReadyQueue *print = Head;PCB *p;printf("进程名\t优先级\t轮数\tcpu时间\t需要时间\t进程状态\t计数器\n");while(print){if(print ->LinkPCB != NULL){p=print ->LinkPCB;while(p){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->sta te,p->count);p = p->next;}}print = print->next;}p = finish;while(p!=NULL){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->sta te,p->count);p = p->next;}p = run;while(p!=NULL){printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);p = p->next;}}void InsertFinish(PCB *in) //将进程插入到完成队列尾部{PCB *fst;fst = finish;if(finish == NULL){in->next = finish;finish = in;}else{while(fst->next != NULL){fst = fst->next;}in ->next = fst ->next;fst ->next = in;}}void InsertPrio(ReadyQueue *in) //创建就绪队列,规定优先数越小,优先级越低{ReadyQueue *fst,*nxt;fst = nxt = Head;if(Head == NULL) //没有队列,则为第一个元素{in->next = Head;Head = in;}else //查到合适的位置进行插入{if(in ->prio >= fst ->prio) //比第一个还要大,则插入到队头{in->next = Head;Head = in;}else{while(fst->next != NULL) //移动指针查找第一个别它小的元素的位置进行插入{nxt = fst;fst = fst->next;}if(fst ->next == NULL) //已经搜索到队尾,则其优先级数最小,将其插入到队尾即可{in ->next = fst ->next;fst ->next = in;}else //入到队列中{nxt = in;in ->next = fst;}}}}void PrioCreate() //创建就绪队列输入函数{ReadyQueue *tmp;int i;printf("输入就绪队列的个数:");scanf("%d",&ReadyNum);printf("输入每个就绪队列的CPU时间片:(一次行输完所有,再按回车结束,如:1 2 3 )\n");for(i = 0;i < ReadyNum; i++){if((tmp = (ReadyQueue *)malloc(sizeof(ReadyQueue)))==NULL){perror("malloc");exit(1);}scanf("%d",&(tmp->round)); //输入此就绪队列中给每个进程所分配的CPU时间片tmp ->prio = 50 - tmp->round; //置其优先级,时间片越高,其优先级越低tmp ->LinkPCB = NULL; //初始化其连接的进程队列为空tmp ->next = NULL;InsertPrio(tmp); //照优先级从高到低,建立多个就绪队列}}void GetFirst(ReadyQueue *queue) //取得某一个就绪队列中的队头进程{run = queue ->LinkPCB;if(queue ->LinkPCB != NULL){run ->state = 'R';queue ->LinkPCB = queue ->LinkPCB ->next;run ->next = NULL;}}void InsertLast(PCB *in,ReadyQueue *queue) //将进程插入到就绪队列尾部{PCB *fst;fst = queue->LinkPCB;if( queue->LinkPCB == NULL){in->next = queue->LinkPCB;queue->LinkPCB = in;else{while(fst->next != NULL){fst = fst->next;}in ->next = fst ->next;fst ->next = in;}}void ProcessCreate() //针对多级反馈队列算法的进程创建函数{PCB *tmp;int i;printf("请输入作业数:");scanf("%d",&num);if( num < 5){system("cls");printf("\n\7\7作业数小于5,重新输入:\n");ProcessCreate();}printf("输入进程名字和进程所需时间:(一次性输完,再按回车结束如:a 1 b 2 c 3 d 4 e 5)\n");for(i = 0;i < num; i++){if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL){perror("malloc");exit(1);}scanf("%s",tmp->name);getchar();scanf("%d",&(tmp->needtime));tmp ->cputime = 0;tmp ->state ='W';tmp ->prio = 50 - tmp->needtime; //置其优先级,需要的时间越多,优先级越低tmp ->round = Head ->round;tmp ->count = 0;InsertLast(tmp,Head); //照优先级从高到低,插入到就绪队列}void RoundRun(ReadyQueue *timechip) //时间片轮转调度算法{int flag = 1;GetFirst(timechip);while(run != NULL){while(flag){run->count++;run->cputime++;run->needtime--;if(run->needtime == 0){run ->state = 'F';InsertFinish(run);flag = 0;}else if(run->count == timechip ->round)//间片用完{run->state = 'W';run->count = 0; //数器清零,为下次做准备InsertLast(run,timechip);flag = 0;}}flag = 1;GetFirst(timechip);}}void MultiDispatch() //多级调度算法,每次执行一个时间片{int flag = 1;int k = 0;system("cls");printf("您选择的是多级反馈队列算法\n");PrioCreate(); //建就绪队列ProcessCreate();//创建就绪进程队列ReadyQueue *point;point = Head;GetFirst(point);while(run != NULL){Output();if(Head ->LinkPCB!=NULL)point = Head;while(flag){run->count++;run->cputime++;run->needtime--;if(run->needtime == 0) //程执行完毕{run ->state = 'F';InsertFinish(run);flag = 0;}else if(run->count == run->round)//间片用完{run->state = 'W';run->count = 0; //数器清零,为下次做准备if(point ->next!=NULL){run ->round = point->next ->round;//置其时间片是下一个就绪队列的时间片InsertLast(run,point->next); //进程插入到下一个就绪队列中flag = 0;}else{RoundRun(point); //果为最后一个就绪队列就调用时间片轮转算法break;}}++k;if(k == 3){ProcessCreate();}}flag = 1;if(point ->LinkPCB == NULL)//绪队列指针下移point =point->next;if(point ->next ==NULL){RoundRun(point);break;}GetFirst(point);}Output();}void opration(){int select;system("cls");printf("\n\n\n\t\t\t请选择调度方式:\n");printf("\t\t\t===========================\n");printf("\t\t\t1.先来先服务调度");printf("\n\t\t\t2.多级反馈队列调度");printf("\n\t\t\t3.退出\n");printf("\t\t\t===========================\n");printf("按数字1、2、3 选择操作:");scanf("%d",&select);switch(select){case 1:fcfs(a);printf("请按任意键继续。