#include using namespace std; void fcfs(vector arrival,vector burst,bool flag) { int start_time[105]; int finish_time[105]; int arri[105]; int burst_time[105]; int turn_time[105]; int wait_time[105]; int response_time[105]; int n=arrival.size(); vector > > v; for(int i=1;i<=n;i++) { arri[i]=arrival[i-1]; burst_time[i]=burst[i-1]; v.push_back({arri[i],{i,burst_time[i]}}); } sort(v.begin(),v.end()); start_time[0]=v[0].first; finish_time[0]=v[0].first+v[0].second.second; int tot=0,idle=0; if(v[0].first>tot) { idle+=(v[0].first-tot); tot=v[0].first; } tot+=v[0].second.second; for(int i=1;itot) { idle+=(arrival-tot); tot=arrival; } int ex=v[i].second.second; finish_time[i]=max(finish_time[i-1],v[i].first)+ex; start_time[i]=max(finish_time[i-1],v[i].first); tot+=ex; } double tot_wait_time=0,tot_turn_time=0; for(int i=0;i > ans; for(int i=0;iarrival,vectorburst,bool flag) { long long int n=arrival.size(); vector, long long int> > v; long long int endTime[105]; long long int response_time[105]; for (long long int i = 0; i < n; i++) { long long int ar=arrival[i]; long long int bt=burst[i]; v.push_back({{ar, bt}, i+1}); } sort(v.begin(), v.end()); set s; long long int start_time[105]; start_time[0]=v[0].first.first; long long int idle=0; for (long long int i = 0; i < n; i++) { if (i == 0) { endTime[i] = v[i].first.first + v[i].first.second; s.insert(v[i].second); } else { long long int mn = LLONG_MAX; long long int id = -1; for (long long int j = 0; j < n; j++) { if (v[j].first.first <= endTime[i - 1] && s.count(v[j].second) == 0 && v[j].first.second < mn) { id = j; mn = v[j].first.second; } } if (id != -1) { swap(v[id], v[i]); s.insert(v[i].second); endTime[i] = endTime[i - 1] + v[i].first.second; sort(v.begin()+ i+1, v.end()); } else { endTime[i] = v[i].first.first + v[i].first.second; s.insert(v[i].second); } start_time[i]=max(endTime[i-1],v[i].first.first); } } long long int turnAround[105]; long long int waitingTime[105]; double tot_turn_time = 0; double tot_wait_time = 0; for (long long int i = 0; i < n; i++) { turnAround[i] = endTime[i] - v[i].first.first; waitingTime[i] = turnAround[i] - v[i].first.second; tot_turn_time += turnAround[i]; tot_wait_time += waitingTime[i]; } for(int i=0;i > ans; for(long long int i=0;imp; typedef pair>pi; ///vector>>v; void preemptive_sjf(vectorarrival,vectorburst,bool f) { vector>>v; mapmp; int n=arrival.size(); int finish_t[100]; int flag[100]; int resp_t[100]; int burst_time[102]; int arri_time[102]; vector>gantchart; memset(flag,0,sizeof(flag)); memset(resp_t,0,sizeof(resp_t)); priority_queue,greater>q; for(int i=1;i<=n;i++) { arri_time[i]=arrival[i-1]; burst_time[i]=burst[i-1]; v.push_back({arri_time[i],{burst_time[i],i}}); ///flag[i-1]=0; } sort(v.begin(),v.end()); q.push({v[0].second.first,{v[0].first,v[0].second.second}}); int cur=0; cur=v[0].first; flag[0]=1; int total_idle=cur; if(cur!=0){ mp[0]=cur; } int cnt=1; while(!q.empty()) { int y=q.top().second.second; int x=q.top().first; int ar=q.top().second.first; q.pop(); if(x>=1){ if(!resp_t[y]){ resp_t[y]=cur; } if(cnt0){ q.push({x,{ar,y}}); } if(q.empty() and cntCT) return -1; int i=0; int tmp = -1; while(b[i].ART<=CT) { if(b[i].RT != 0 && tmp == -1) { tmp = i; } else if(b[i].PRT < b[tmp].PRT && b[i].RT!=0) //Higher number higher priority(>)//Higher number lower priority (<) tmp = i; i++; if(i>n) break; } return tmp; } int nonpreemp_next_job(int CT,int n, struct job (& a)[100]) { if(a[0].ART>CT) return -1; int i=0; int tmp = -1; while(a[i].ART<=CT) { if(a[i].RT != 0 && tmp == -1) { tmp = i; } else if(a[i].PRT < a[tmp].PRT && a[i].RT!=0) //Higher number higher priority(>)//Higher number lower priority (<) tmp = i; i++; if(i>n) break; } return tmp; } void non_preemptive_priority(vector arrival,vector burst,vector priority,bool flag) { job a[100]; vector GT; int n=arrival.size(), i, j, T=0; for(i=0; i=0; i--) { if(GT[i]!=-1 && a[GT[i]].ENT==-2) a[GT[i]].ENT = i+1; } // other values using gantt chart for(i=0; i arrival,vector burst,vector priority,bool flag) { job b[100]; vector GT; int n=arrival.size(), i, j, T=0; for(i=0; i=0; i--) { if(GT[i]!=-1 && b[GT[i]].ENT==-2) b[GT[i]].ENT = i+1; } // other values using gantt chart for(i=0; iar,vectorbt,int tq,bool f) { int arrival[5000],burst[5000],end_time[5000],id[5000],flag[5000],res_time[5000]; int n=ar.size(); double turn_time[5000],wait_time[5000]; vector< pair> >v; int context=0; for(int i=1;i<=n;i++) { int x,y; x=ar[i-1]; arrival[i]=x; y=bt[i-1]; burst[i]=y; res_time[i]=-1; flag[i-1]=0; v.push_back({x,{i,y}}); } int TQ=tq; sort(v.begin(),v.end()); map gap; ///saves { idle time start, idle itme end } vector > g; ///saves the { pid, end time } queue< pair > q; q.push({v[0].second.first,v[0].second.second}); ///id burst time flag[0]=1; int cur_time=0,tot_idle=0; cur_time=v[0].first; ///idle in the start if(cur_time!=0) { tot_idle+=cur_time; gap[0]=cur_time; } int completed=0; while(!q.empty() and completed!=n) { int id=q.front().first; int bur=q.front().second; q.pop(); ///cout<<"ID "<=bur) { cur_time+=bur; /// burst2[pt]=0; end_time[id]=cur_time; rem=0; completed++; } else { cur_time+=TQ; /// burst2[pt]-=TQ; rem=bur-TQ; } g.push_back({id,cur_time}); for(int i=0;i readyq; int finding_time_quantum() { double sm=0,avg,sz=readyq.size(); vectorrem_time; while(!readyq.empty()) { int x=readyq.front(); readyq.pop(); rem_time.push_back(x); sm=(double)sm+x; } avg=sm/(sz); int t=ceil(avg); for(auto it:rem_time) { readyq.push(it); } return t; } void proposed_algo(vectorar,vectorbt,vectorpriority,bool f) { while(!readyq.empty()) readyq.pop(); int arrival[5000],burst[5000],end_time[5000],id[5000],flag[5000],res_time[5000],prio[5000]; int n=ar.size(); double turn_time[5000],wait_time[5000]; vector< pair< int,pair > > >v; int completed=0; int context=0; for(int i=1;i<=n;i++) { int x,y,p; x=ar[i-1]; arrival[i]=x; y=bt[i-1]; burst[i]=y; p=priority[i-1]; prio[i]=p; res_time[i]=-1; flag[i-1]=0; v.push_back({arrival[i],{prio[i],{burst[i],i}}}); } int TQ; sort(v.begin(),v.end()); ///for(auto it:v) cout< gap; ///saves { idle time start, idle itme end } vector > g; ///saves the { pid, end time } queue< pair > q; q.push({v[0].second.second.second,v[0].second.second.first}); ///id burst time flag[0]=1; readyq.push(v[0].second.second.first); int cur_time=0,tot_idle=0; cur_time=v[0].first; ///idle in the start if(cur_time!=0) { tot_idle+=cur_time; gap[0]=cur_time; } while(!q.empty() and completed!=n) { int id=q.front().first; int bur=q.front().second; q.pop(); context++; ///cout<<"ID "<=bur) { cur_time+=bur; end_time[id]=cur_time; rem=0; completed++; } else { cur_time+=TQ; rem=bur-TQ; } g.push_back({id,cur_time}); vector< pair< int,pair > > >rv; /// p, burst , arri ,id for(int i=0;i arrival,vector burst,vector priority,int tq) { fcfs(arrival,burst,0); non_preemptive_sjf(arrival,burst,0); preemptive_sjf(arrival,burst,0); non_preemptive_priority(arrival,burst,priority,0); preemptive_priority(arrival,burst,priority,0); round_robin(arrival,burst,tq,0); proposed_algo(arrival,burst,priority,0); } int main() { while(1) { cout<<"1: FCFS"<>choice; if(choice==9) return 0; cout<<"Number of Process,n: "; int n;cin>>n; cout< arrival,burst,priority; if(choice>=1 and choice<=3) { for(int i=1;i<=n;i++) { int a,b; cout<<"Enter the arrival time of P"<>a; arrival.push_back(a); cout<<"Enter the burst time of P" <>b; burst.push_back(b); cout << endl; } } else if(choice>=4 and choice<=5) { for(int i=1;i<=n;i++) { int a,b,p; cout<<"Enter the arrival time of P"<>a; arrival.push_back(a); cout<<"Enter the burst time of P" <>b; burst.push_back(b); cout << "Enter Priority of p" << i << ": " ; cin>>p; priority.push_back(p); cout << endl; } } else if(choice==6) { for(int i=1;i<=n;i++) { int a,b; cout<<"Enter the arrival time of P"<>a; arrival.push_back(a); cout<<"Enter the burst time of P" <>b; burst.push_back(b); cout << endl; } cout<<"Enter the time Quantum : "; cin>>tq; cout<>a; arrival.push_back(a); cout<<"Enter the burst time of P" <>b; burst.push_back(b); cout << "Enter Priority of p" << i << ": " ; cin>>p; priority.push_back(p); cout << endl; } } else if(choice==8) { for(int i=1;i<=n;i++) { int a,b,p; cout<<"Enter the arrival time of P"<>a; arrival.push_back(a); cout<<"Enter the burst time of P" <>b; burst.push_back(b); cout << "Enter Priority of p" << i << ": " ; cin>>p; priority.push_back(p); cout << endl; } cout<<"Enter the time Quantum : "; cin>>tq; cout<>ch; } } /* 3 3 5 1 6 2 1 12 3 1 //sp 3 3 5 1 5 20 1 6 2 1 9 */