操作系统--进程调度算法
2018-06-02 17:21
417 查看
#include<bits/stdc++.h> #include<pthread.h> using namespace std; #define threadnum 20 #define timecut 25 pthread_mutex_t Device_mutex ; struct PCB { int id;//process log int priority;//youxianquan int runtime;//use time int waittime;//wait time int arrivetime; bool used;//book this pcb }PCBS[threadnum]; void init() { for(int i=0;i<threadnum;i++) { PCBS[i].id = i+1; PCBS[i].priority = 0; PCBS[i].arrivetime = 1+rand()%60; PCBS[i].runtime = 1+rand()%30; PCBS[i].waittime = 0; PCBS[i].used = false; } } void showinfo(PCB *T) { for(int i=0;i<threadnum;i++) { cout<<"id: "<<T[i].id<<" priority: "<<T[i].priority<<" arrivetime: "<<T[i].arrivetime<<" runtime: "<<T[i].runtime<<" waittime: "<<T[i].waittime<<endl; } } bool cmpbyarrivetime(PCB a,PCB b) { return a.arrivetime<b.arrivetime; } bool cmpbyid(PCB a,PCB b) { return a.id<b.id; } bool cmpbyruntime(PCB a,PCB b) { return a.runtime<b.runtime; } void FCFS(PCB *T) { //int len = sizeof(T)/sizeof(PCB); int sumtime = 0; cout<<"the infomation before fcfs run"<<endl; showinfo(T); sort(T,T+threadnum,cmpbyarrivetime); for(int i=0;i<threadnum;i++) T[i].priority = i+1; T[0].waittime = 0; sumtime+=T[0].runtime; T[0].used = true; for(int i=1;i<threadnum;i++) { if(T[i].arrivetime > sumtime) sumtime = T[i].arrivetime; T[i].waittime = sumtime; sumtime+=T[i].runtime; T[i].used = true; } sort(T,T+threadnum,cmpbyid); cout<<"the infomation after fcfs run"<<endl; showinfo(T); } void SJF(PCB *T)//short first { int sumtime = 0; int sumpcb = 0; sort(T,T+threadnum,cmpbyarrivetime); cout<<"the infomation before sfj run"<<endl; showinfo(T); vector<PCB> sfj; sfj.clear(); sfj.push_back(T[0]); T[0].used = true; T[0].waittime = 0; sumtime += T[0].runtime; sumpcb++; while(1) { if(sumpcb>=threadnum) break; PCB S = sfj[sumpcb-1]; int truntime = 999; int tipos=-1; for(int i=0;i<threadnum;i++) { if(T[i].arrivetime < S.arrivetime+S.runtime&&(!T[i].used)) { if(T[i].runtime < truntime) { truntime = T[i].runtime; tipos = i; } } } if(tipos!=-1) { T[tipos].used = true; T[tipos].waittime=sumtime; sfj.push_back(T[tipos]); sumtime+=T[tipos].runtime; sumpcb++; } else { for(int i=sumpcb;i<threadnum;i++) { if(!T[i].used) { sumtime = T[i].arrivetime; T[i].used = true; break; } } } } for(int i=0;i<threadnum;i++) { T[i].id = sfj[i].id; T[i].priority = i+1; T[i].arrivetime = sfj[i].arrivetime; T[i].runtime = sfj[i].runtime; T[i].waittime = sfj[i].waittime; } sort(T,T+threadnum,cmpbyid); cout<<"the infomation after sfj run"<<endl; showinfo(T); } void HRRN(PCB *T) { int sumtime = 0; sort(T,T+threadnum,cmpbyarrivetime); cout<<"the infomation before hrrn run"<<endl; showinfo(T); //waittime / runtime; vector<PCB> PCBlist; int sumpcb = 0; PCBlist.push_back(T[0]); sumtime+=T[0].runtime; sumpcb++; T[0].waittime = 0; T[0].used = true; while(1) { double rp = 0; int tipos; if(sumpcb>=threadnum) break; for(int i=0;i<threadnum;i++) { if(!T[i].used) { if((sumtime-T[i].arrivetime)*1.0/T[i].runtime > rp){ rp = (sumtime-T[i].arrivetime)*1.0 / T[i].runtime; tipos = i; } } } if(rp==0) { for(int i=0;i<threadnum;i++) { if(!T[i].used) { sumtime = T[i].arrivetime; tipos = i; } } } T[tipos].used = true; T[tipos].waittime = sumtime; sumtime+=T[tipos].runtime; sumpcb++; // T[tipos].waittime = sumtime; PCBlist.push_back(T[tipos]); } for(int i=0;i<threadnum;i++) { T[i].id = PCBlist[i].id; T[i].priority = i+1; T[i].waittime = PCBlist[i].waittime; } sort(T,T+threadnum,cmpbyid); cout<<"the infomation after hrrn run"<<endl; showinfo(T); } bool operator < (PCB a,PCB b) { double ax = a.waittime*1.0/a.runtime; double bx = b.waittime*1.0/b.runtime; return ax<bx; } bool cmpbywaitdivrun(PCB a,PCB b) { double ax = a.waittime*1.0/a.runtime; double bx = b.waittime*1.0/b.runtime; return ax>bx; } int main() { init(); FCFS(PCBS); init(); SJF(PCBS); init(); HRRN(PCBS); }
上述程序
FCFS:先到先服务算法
SJF:短作业优先调度算法
HRRN:响应比优先调度算法
轮盘调度算法:
#include <bits/stdc++.h> using namespace std; const int maxn = 20; #define timecut 25 struct PCB { int id;//process log int priority;//youxianquan int runtime;//use time int waittime;//wait time int arrivetime; bool used;//book this pcb }PCBS[maxn]; void init() { for(int i=0;i<maxn;i++) { PCBS[i].id = i+1; PCBS[i].priority = 0; PCBS[i].arrivetime = 1+rand()%60; PCBS[i].runtime = 1+rand()%30; PCBS[i].waittime = 0; PCBS[i].used = false; } } bool arivecmp(PCB a,PCB b) { return a.arrivetime < b.arrivetime; } void showinfo(PCB *T) { for(int i=0;i<maxn;i++) { cout<<"id: "<<T[i].id<<" priority: "<<T[i].priority<<" arrivetime: "<<T[i].arrivetime<<" runtime: "<<T[i].runtime<<" waittime: "<<T[i].waittime<<endl; } } bool cmpid(PCB a,PCB b) { return a.id < b.id; } void RR(PCB *T) { sort(T,T+maxn,arivecmp); cout<<"the infomation before RR runs"<<endl; showinfo(T); deque<PCB> QP; vector<PCB> ans; int sumpcb = 0; int sumtime = 0; for(int i=0;i<maxn;i++) { QP.push_back(T[i]); } while(!QP.empty()) { PCB X = QP.front(); QP.pop_front(); if(X.runtime<=timecut) { X.waittime = sumtime; X.used = true; ans.push_back(X); sumtime+=X.runtime; } else { X.runtime-=timecut; QP.push_back(X); sumtime+=timecut; } } for(int i=0;i<maxn;i++) { T[i]= ans[i]; T[i].priority = i+1; } cout<<"the infomation after rr run"<<endl; sort(T,T+maxn,cmpid); showinfo(T); } int main() { init(); RR(PCBS); }
相关文章推荐
- 操作系统进程调度算法图解(FCFS、轮转、SPN、SRT、HRRN、反馈)
- 【操作系统 - 2】时间片轮转RR进程调度算法
- 操作系统 进程调度算法 浅析
- 操作系统进程(作业)调度常见算法详解
- 操作系统---OS的进程调度算法
- 【操作系统】进程调度的四种算法
- 操作系统中常用的几种进程调度算法
- 【操作系统】进程调度算法
- 操作系统——使用动态优先权的进程调度算法的模拟
- Java模拟操作系统进程调度算法—先来先服务、短作业优先、高响比优先
- 【操作系统 - 2】时间片轮转RR进程调度算法
- 操作系统的常见进程调度算法
- 【操作系统 - 2】时间片轮转RR进程调度算法
- 操作系统:进程调度算法详解之FCFS和SPF篇
- 操作系统进程调度算法(Java 实现)
- 【操作系统 - 2】时间片轮转RR进程调度算法
- 操作系统:进程调度算法详解之FCFS和SPF篇
- 操作系统常见的几个进程调度算法
- 操作系统进程调度算法 先到先服务 短作业 优先级 时间片轮转
- 计算机操作系统-进程的调度算法