#include<bits/stdc++.h> #include<pthread.h> using namespace std; #define threadnum 20 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); } int main() { init(); FCFS(PCBS); init(); SJF(PCBS); init(); HRRN(PCBS); }