欢迎来到天天文库
浏览记录
ID:9858321
大小:99.00 KB
页数:5页
时间:2018-05-12
《操作系统进程调度算法模拟》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验一进程调度算法模拟一.实验题目:设计一个简单的进程调度算法,模拟OS中的进程调度过程二.要求:①进程数不少于5个;②进程调度算法任选;可以用动态优先数加时间片轮转法实现进程调度,每运行一个时间片优先数减3;③用C语言编程;④程序运行时显示进程调度过程。三.程序中所用数据结构及说明:进程控制块结构体:typedefstructnode1{intID;//进程标识数intPRIORITY;//进程优先数intCPUTIME;//进程已占用时间片intALLTIME;//进程还需占用时间片}Block,pcb;就绪进程链表节点:typedefstructnode2{pcbdat
2、a;structnode2*next;}process;四.清单程序及描述:Procelink.h:typedefstructnode1{intID;//进程标识数intPRIORITY;//进程优先数intCPUTIME;//进程已占用时间片intALLTIME;//进程还需占用时间片//charSTATE;//进程状态//structnode*next;//进程队列指针}Block,pcb;typedefstructnode2{pcbdata;structnode2*next;}process;voidInitlink(process**PQ){/*如果有内存空间,申请头
3、结点空间并使头指针head指向头结点*/if((*PQ=(process*)malloc(sizeof(process)))==NULL)exit(1);(*PQ)->next=NULL;/*置链尾标记NULL*/}intIsEmpty(process*PQ){if(PQ->next==NULL)return1;elsereturn0;}voidEnInitlink(process*PQ,pcbp){while(PQ->next!=NULL)PQ=PQ->next;process*temp=(process*)malloc(sizeof(Block));temp->data.
4、PRIORITY=p.PRIORITY;temp->data.ID=p.ID;temp->data.CPUTIME=p.CPUTIME;temp->data.ALLTIME=p.ALLTIME;temp->next=PQ->next;PQ->next=temp;}//插入pcbDeInitlink(process*PQ)//选择优先数最小的出列{if(IsEmpty(PQ)){printf("所有进程已运行完!");exit(0);//退出}process*temp=(process*)malloc(sizeof(Block));temp=PQ->next;process
5、*te=(process*)malloc(sizeof(Block));process*t=(process*)malloc(sizeof(Block));te=PQ->next;//优先数最小的进程while(temp->next!=NULL){if(te->data.PRIORITYnext->data.PRIORITY){te=temp->next;t=temp->next->next;PQ=temp;}temp=temp->next;}PQ->next=PQ->next->next;pcbpp=te->data;//free(temp);//free(t
6、);//free(te);returnpp;}//出队列voidoutid(process*PQ)//输出就绪队列函数{printf("当前就绪队列为:");while(!IsEmpty(PQ)){printf("%d",PQ->next->data.ID);PQ=PQ->next;}printf("");}voiddispatch(pcbp,process*PQ)//进程运行函数{if((p.ALLTIME)!=0){p.PRIORITY-=3;p.CPUTIME+=1;p.ALLTIME-=1;printf("进程%d运行",p.ID);//printf("进程优
7、先数:%d进程已占用时间片:%d进程还需占用时间片:%d",p.PRIORITY,p.CPUTIME,p.ALLTIME);outid(PQ);//输出就绪队列}if((p.ALLTIME)==0){printf("进程%d运行完成!",p.ID);return;//完成则不加入链表}EnInitlink(PQ,p);return;//运行之后再加入链表}os_1.cpp:#include#include#include"procelink.h"voidma
此文档下载收益归作者所有