操作系统-进程调度之模拟SJF
2014-11-22 16:23
423 查看
大致介绍:
SJF像它的名字一样,短进程优先。
该调度策略大致思路,在已存在的进程中,以进程的预计服务时间作为决定执行顺序的参数。且预计服务时间越短,优先级越高。
分析:
在开始进程调度过程中模拟SJF调度策略结果时,主要有两个因素影响调度最终该结果。到达时间和进程预计运行时间。在调度过程中有两种要解决的问题:
1、CPU空闲,就绪队列没有进程,当下一个进程到达时就开始调度,也就是说这个进程到达的时刻也是一个调度时刻。
2、由于是模拟系统,刚开始就已经初始化了好多PCB模块信息,然后在调度时候,输入一个以前的时间点,就可以模拟出系统的调度进程和创建进程好像是在并发,得到合理这调度顺序,这样的话,就会出现第二个问题:如果一个进程的预计运行时间短,但是在调度的时刻并未到达,所以仅按预计运行时间长短来来排序是不行的。
设计:
采用链表组织PCB就绪队列,应对上面出现的情况,我认为在创建过程中,先按照进程的预计运行时间进行插入排序,因为插入排序利于链表,再加上在创建进程过程中有一定顺序的组织就绪队列有力与以后的工作,综合分析这个过程插入排序也局有明显优势。
在调度过程中,因为开始调度按钮可以对应不同的开始调度时刻,那么对应的调度结果也是不一样的,所以没有在创建进程的过程中直接按到达时间和预计服务时间两个参数进行控制排序。因为那样得到的结果并不满足实际情况的多样性。
虚拟一个时钟,在每一个调度时刻去队列中从队首到队尾寻找第一个到达时间早于或者等于该时刻的进程(因为创建的时候按照预计运行时间排过序)。如果在一个调度时刻就绪队列里面没有进程,那么等到下一个进程到达的时刻进行调度,并更新调度时刻。
实现:
调度算法代码实现:
附录:一个完整进程的创建、撤销以及FCFS和SJF的调度算法的模拟程序。
SJF像它的名字一样,短进程优先。
该调度策略大致思路,在已存在的进程中,以进程的预计服务时间作为决定执行顺序的参数。且预计服务时间越短,优先级越高。
算法设计
SJF算法分析、设计与实现分析:
在开始进程调度过程中模拟SJF调度策略结果时,主要有两个因素影响调度最终该结果。到达时间和进程预计运行时间。在调度过程中有两种要解决的问题:
1、CPU空闲,就绪队列没有进程,当下一个进程到达时就开始调度,也就是说这个进程到达的时刻也是一个调度时刻。
2、由于是模拟系统,刚开始就已经初始化了好多PCB模块信息,然后在调度时候,输入一个以前的时间点,就可以模拟出系统的调度进程和创建进程好像是在并发,得到合理这调度顺序,这样的话,就会出现第二个问题:如果一个进程的预计运行时间短,但是在调度的时刻并未到达,所以仅按预计运行时间长短来来排序是不行的。
设计:
采用链表组织PCB就绪队列,应对上面出现的情况,我认为在创建过程中,先按照进程的预计运行时间进行插入排序,因为插入排序利于链表,再加上在创建进程过程中有一定顺序的组织就绪队列有力与以后的工作,综合分析这个过程插入排序也局有明显优势。
在调度过程中,因为开始调度按钮可以对应不同的开始调度时刻,那么对应的调度结果也是不一样的,所以没有在创建进程的过程中直接按到达时间和预计服务时间两个参数进行控制排序。因为那样得到的结果并不满足实际情况的多样性。
虚拟一个时钟,在每一个调度时刻去队列中从队首到队尾寻找第一个到达时间早于或者等于该时刻的进程(因为创建的时候按照预计运行时间排过序)。如果在一个调度时刻就绪队列里面没有进程,那么等到下一个进程到达的时刻进行调度,并更新调度时刻。
实现:
调度算法代码实现:
void SJF() { int tempTime; system("cls"); bool flag=false; bool mark; Process *temp1,*temp2; temp1=NULL; temp2=NULL; char choose; cout<<"是否开始进行进程调度,请选择:[y/n]"<<endl; cin>>choose; cout<<"请输入开始调度进程的时间:"<<endl; cin>>tempTime; if(choose=='y') flag=true; if(flag) { while(ReadyH2->next!=NULL)//控制调出所有就绪队列里面的所有PCB { temp1=ReadyH2; mark=false; while(temp1)//假如该时刻已有创建好的进程,找到应该被调度的PCB。 { if(temp1->next!=NULL&&tempTime>=temp1->next->CreateTime&&temp1->next->CreateTime!=-842150451) { temp2=temp1->next; temp1->next=temp2->next; temp2->next=NULL; temp2->Runtime=tempTime; tempTime+=temp2->ServiceTime; enqueue(RunH2,temp2); mark=true; break; } else temp1=temp1->next; } //假如该调度时刻没有创建好的进程,那么找到下一个创建的进程。 if(mark==false) { int ttime=0xfffffff; temp1=ReadyH2; while(temp1->next)//找到要创建进程的PCB块前驱temp2。 { if(ttime>temp1->next->CreateTime) { temp2=temp1; ttime=temp1->next->CreateTime; } temp1=temp1->next; } temp1=temp2->next; temp2->next=temp1->next; temp1->next=NULL; temp1->Runtime=temp1->CreateTime; tempTime=temp1->CreateTime+temp1->ServiceTime; enqueue(RunH2,temp1); } } RunPrint1(RunH2->next);//打印执行队列,即:执行结果。 } }
附录:一个完整进程的创建、撤销以及FCFS和SJF的调度算法的模拟程序。
#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const int Nalen = 30; const int IDRange = 1000+10; int Hash[IDRange]; struct Process { char PName[Nalen]; int Pid; int CreateTime; //int Priority; int ServiceTime; int Runtime;//被调度的时间。 Process* next; }; Process *ReadyH1,*ReadyH2,*RunH1,*RunH2,*SpareH; void Initial();//初始化函数。 int GetId();//获得ID的函数。 void Create_Process();//创建进程。 void Kill_Process();//撤销进程。 void enqueue(Process * head , Process * node);//进入队尾。 Process *outqueue(Process *head);//队首结点出队列。 void InsertSort(Process* head,Process* node);//SJF算法初始化就绪队列使用的插入排序。 void ReadyPrint();//打印就绪队列里PCB的状态。 void RunPrint(Process *p);//打印采用FCFS调度机制,得到的执行队列调度进程PCB的顺序。 void RunPrint1(Process *p);//打印采用SJF调度机制,得到的执行队列调度进程PCB的顺序。 void FCFS();//先来先服务调度算法。 void SJF();//短进程优先调度算法。 void mainUI();//菜单主函数。 void Initial() { memset(Hash,0,sizeof(Hash)); ReadyH1 = (Process*) malloc(sizeof(Process)); ReadyH1->next = NULL; ReadyH2 = (Process*) malloc(sizeof(Process)); ReadyH2->next = NULL; RunH1 = (Process*) malloc(sizeof(Process)); RunH1->next = NULL; RunH2 = (Process*) malloc(sizeof(Process)); RunH2->next = NULL; SpareH = (Process*) malloc(sizeof(Process)); SpareH->next = NULL; } void enqueue(Process * head , Process * node)//进入队尾。 { if(node==NULL) return; Process *p; p=head; while(p) { if(p->next == NULL) { p->next = node; node->next= NULL; break; } else p = p->next; } } Process *outqueue(Process *head)//队首结点出队列。 { Process * p; p=NULL; if(head->next == NULL) p=NULL; else { p=head->next; head->next=p->next; } return p; } void InsertSort(Process* head,Process* node) { if(node==NULL) return; Process *p; p=head; while(p->next!=NULL&&p->next->ServiceTime<=node->ServiceTime) p=p->next; node->next=p->next; p->next=node; } int GetId() { int id = 1; while(1) { if(Hash[id] == 0) { Hash[id] = 1; break; } id=id++%IDRange; } return id; } void Create_Process() { system("cls"); char Pname[Nalen]; cout<<"请输入要创建的进程的名字:"<<endl; cin>>Pname; Process* temp1,*temp2; temp1=(Process *) malloc(sizeof(Process)); temp1->next=NULL; temp2=(Process *) malloc(sizeof(Process)); temp2->next=NULL; strcpy_s(temp1->PName,Pname); strcpy_s(temp2->PName,Pname); temp1->Pid = GetId(); temp2->Pid = temp1->Pid; cout<<"请输入该进程的创建时刻:"<<endl; cin>>temp1->CreateTime; temp2->CreateTime=temp1->CreateTime; /*cout<<"请输入要创建的进程的优先级:"<<endl; cin>>temp1->Priority; temp2->Priority=temp1->Priority;*/ cout<<"请输入该进程预计被服务时间:"<<endl; cin>>temp1->ServiceTime; temp2->ServiceTime=temp1->ServiceTime; enqueue(ReadyH1,temp1); InsertSort(ReadyH2,temp2); getchar(); cout<<"请输入任意键继续"<<endl; getchar(); system("cls"); } void Kill_Process() { system("cls"); Process* p,*temp; temp=NULL; int KillId; bool flag=true; cout<<"请输入要删除的进程的ID:"; cin>>KillId; p = ReadyH1; while(p->next) { if(p->next->Pid==KillId) { temp=p->next; temp->next=NULL; p->next=temp->next; cout<<"该进程已结束:"<<"\n进程号为:"<<temp->Pid<<"\n进程名为:"<<temp->PName<<endl; Hash[temp->Pid] = 0; enqueue(SpareH,temp); flag=false; } } if(flag) cout<<"你输入的为无效Pid"<<endl; getchar(); cout<<"请输入任意键继续"<<endl; getchar(); system("cls"); } void ReadyPrint(Process *p) { system("cls"); cout<<"*********** 就绪队列显示区 ***********"<<endl; while(p) { cout<<"进程ID: "<<p->Pid<<endl; cout<<"进程名: "<<p->PName<<endl; cout<<"进程创建时刻:"<<p->CreateTime<<endl; cout<<"*******************************************"<<endl; p=p->next; } getchar(); cout<<"请输入任意键结束程序。"<<endl; getchar(); system("cls"); } void RunPrint(Process *p) { int temp; system("cls"); temp=1; cout<<"*********** 进程按调度顺序显示 ***********"<<endl; while(p) { cout<<"第"<<" "<<temp<<" 次调的进程为:"<<endl; cout<<"进程ID: "<<p->Pid<<endl; cout<<"进程名: "<<p->PName<<endl; cout<<"进程创建时刻:"<<p->CreateTime<<endl; cout<<"*******************************************"<<endl; p=p->next; temp++; } getchar(); cout<<"请输入任意键结束程序。"<<endl; getchar(); system("cls"); } void FCFS() { system("cls"); bool flag=false; char choose; Process *temp; temp=NULL; cout<<"是否开始进行进程调度,请选择:[y/n]"<<endl; cin>>choose; if(choose=='y') flag=true; if(flag) { while(ReadyH1->next!=NULL) { temp=outqueue(ReadyH1); enqueue(RunH1,temp); } RunPrint(RunH1->next); } } void RunPrint1(Process *p) { int temp; system("cls"); temp=1; cout<<"*********** 进程按调度顺序显示 ***********"<<endl; while(p) { cout<<"第"<<" "<<temp<<" 次调的进程为:"<<endl; cout<<"进程ID: "<<p->Pid<<endl; cout<<"进程名: "<<p->PName<<endl; cout<<"进程创建时刻:"<<p->CreateTime<<endl; cout<<"进程预计被服务时间: "<<p->ServiceTime<<endl; cout<<"进程的被调度的时间为:"<<p->Runtime<<endl; cout<<"*******************************************"<<endl; p=p->next; temp++; } getchar(); cout<<"请输入任意键结束程序。"<<endl; getchar(); system("cls"); } void SJF() { int tempTime; system("cls"); bool flag=false; bool mark; Process *temp1,*temp2; temp1=NULL; temp2=NULL; char choose; cout<<"是否开始进行进程调度,请选择:[y/n]"<<endl; cin>>choose; cout<<"请输入开始调度进程的时间:"<<endl; cin>>tempTime; if(choose=='y') flag=true; if(flag) { while(ReadyH2->next!=NULL)//控制调出所有就绪队列里面的所有PCB { temp1=ReadyH2; mark=false; while(temp1)//假如该时刻已有创建好的进程,找到应该被调度的PCB。 { if(temp1->next!=NULL&&tempTime>=temp1->next->CreateTime&&temp1->next->CreateTime!=-842150451) { temp2=temp1->next; temp1->next=temp2->next; temp2->next=NULL; temp2->Runtime=tempTime; tempTime+=temp2->ServiceTime; enqueue(RunH2,temp2); mark=true; break; } else temp1=temp1->next; } //假如该调度时刻没有创建好的进程,那么找到下一个创建的进程。 if(mark==false) { int ttime=0xfffffff; temp1=ReadyH2; while(temp1->next)//找到要创建进程的PCB块前驱temp2。 { if(ttime>temp1->next->CreateTime) { temp2=temp1; ttime=temp1->next->CreateTime; } temp1=temp1->next; } temp1=temp2->next; temp2->next=temp1->next; temp1->next=NULL; temp1->Runtime=temp1->CreateTime; tempTime=temp1->CreateTime+temp1->ServiceTime; enqueue(RunH2,temp1); } } RunPrint1(RunH2->next); } } void mainUI() { cout<<"*********** 模拟级任务管理器 ***********"<<endl; cout<<"*********** 1、进程创建 ***********"<<endl; cout<<"*********** 2、进程撤销 ***********"<<endl; cout<<"*********** 3、就绪队列显示(FCFD) **********"<<endl; cout<<"*********** 4、FCFS调度执行结果 ***********"<<endl; cout<<"*********** 5、SJF调度执行结果 ***********"<<endl; cout<<"*********** 6、离开任务管理器 ***********"<<endl; cout<<"*********** 请输入您选择的数字: ***********"<<endl; } int main() { int choose; bool flag=true; Initial(); while(1) { mainUI(); cin>>choose; switch (choose) { case 1: Create_Process(); break; case 2: Kill_Process(); break; case 3: ReadyPrint(ReadyH1->next); break; case 4: FCFS(); break; case 5: SJF(); break; case 6: flag=false; break; default: break; } if(!flag) break; } return 0; }
相关文章推荐
- 实验三 进程调度模拟程序--操作系统
- 【操作系统】实验三 进程调度模拟程序
- 实验三 进程调度模拟程序--操作系统
- 【操作系统】实验三 进程调度模拟程序
- 【操作系统】实验三 进程调度模拟程序
- 操作系统FCFS,SJF进程调度(C++)
- 操作系统---实验三 进程调度模拟程序
- 操作系统用C语言模拟进程基于优先级的调度程序
- 【操作系统】实验三、进程调度模拟程序实验
- 操作系统之实验三 进程调度模拟程序
- 【操作系统】实验三 进程调度模拟程序
- 【操作系统】实验三 进程调度模拟程序
- Java模拟操作系统进程调度算法—先来先服务、短作业优先、高响比优先
- 【操作系统】实验三 进程调度模拟程序
- 操作系统——使用动态优先权的进程调度算法的模拟
- [操作系统]实验三进程调度模拟程序
- 模拟操作系统优先级调度进程
- 【操作系统】实验三 进程调度模拟程序
- 【操作系统】实验三 进程调度模拟程序
- 操作系统实验之一--进程调度算法的模拟实现