You are on page 1of 5

Name: Abhijeet Bhushari

Assignment 3

FCFS

#include<bits/stdc++.h>
#include<iostream>
using namespace std;

int main(){
int n;
cout<<"Enter the number of processes: ";
cin>>n;
cout<<"\nEnter the sequence: ";
int pid[n];
int burst[n];
for(int i=0;i<n;i++){
cout<<"PID: ";
cin>>pid[i];
cout<<"Burst time: ";
cin>>burst[i];
}
cout<<"\nPID Waiting time"<<endl;
int sum=0,total_wait=0;
for(int i=0;i<n;i++){
cout<<pid[i]<<" "<<sum<<endl;
if(i<n-1){
sum+=burst[i];
total_wait+=sum;
}
}
cout<<"Average Waiting time: "<<(double)total_wait/(double)n<<endl;
return 0;
}

OUTPUT:
Non Preemptive SJF:

CODE:
#include<bits/stdc++.h>
using namespace std;
struct process{
int pid;
int bt;
int at;
process(){
pid=0;
bt=0;
at=0;
}
};

bool comp(process a, process b){


if(a.at<b.at) return true;
else if(a.at==b.at && a.bt<b.bt) return true;
return false;
}
int main(){
int n;
cout<<"Enter the number of processes: ";
cin>>n;
int pid[n],bt[n],at[n];
process p[n];
for(int i=0;i<n;i++){
p[i].pid=i+1;
cout<<"Burst time of "<<p[i].pid<<": ";
cin>>p[i].bt;
cout<<"Arrival time of "<<p[i].pid<<": ";
cin>>p[i].at;
}
sort(p,p+n,comp);
int ct[n],tat[n],wt[n];
ct[0]=p[0].bt-p[0].at;
tat[0]=p[0].bt;
wt[0]=0;
int tot_wait=0,tot_tat=tat[0];
for(int i=1;i<n;i++){
wt[i]=max(p[i].at,ct[i-1])-p[i].at;
tot_wait+=wt[i];
ct[i]=max(p[i].at,ct[i-1])+p[i].bt;
tat[i]=ct[i]-p[i].at;
tot_tat+=tat[i];
}
cout<<"PID WT TAT"<<endl;
for(int i=0;i<n;i++){
cout<<p[i].pid<<" "<<wt[i]<<" "<<tat[i]<<endl;
}
cout<<"Avg. Waiting time: "<<(double)tot_wait/(double)n<<endl;
cout<<"Avg. Turnaround time: "<<(double)tot_tat/(double)n<<endl;
return 0;
}

OUTPUT:

Preemptive SJF:

CODE:
#include<bits/stdc++.h>
#define int long long
using namespace std;
int current=0;
struct process{
int pid,bt,at,ct,rt;
process(){
pid=0;bt=0;at=0;ct=0;rt=0;
}
};

bool comp(process a, process b){


if(a.at<b.at) return true;
else if(a.at==b.at && a.bt<b.bt) return true;
return false;
}

bool comp1(process a,process b){


if(b.at>current || b.rt==0) return true;
if(a.at>current || a.rt==0) return false;
if(a.rt<b.rt) return true;
return false;
}

bool comp2(process a,process b){


if(a.pid<b.pid) return true;
return false;
}

void main(){
int n;
// cout<<"Enter the number of processes: ";
// cin>>n;
n=3;
int pid[n],bt[n],at[n],tat[n],wt[n],count=0,tot_wait=0,tot_tat=0;
process p[n];
// for(int i=0;i<n;i++){
// p[i].pid=i+1;
// cout<<"Burst time of "<<p[i].pid<<": ";
// cin>>p[i].bt;
// p[i].bt=p[i].rt;
// cout<<"Arrival time of "<<p[i].pid<<": ";
// cin>>p[i].at;
// }
p[0].pid=1;p[0].bt=5;p[0].at=1;p[0].rt=5;
p[1].pid=2;p[1].bt=2;p[1].at=2;p[1].rt=2;
p[2].pid=3;p[2].bt=5;p[2].at=3;p[2].rt=5;
sort(p,p+n,comp);
current=p[0].at;
while(true){
sort(p,p+n,comp1);
if(current>=p[0].at) p[0].rt--;
if(p[0].rt==0){
count++;
p[0].ct=current+1;
}
if(count==n) break;
current++;
}
sort(p,p+n,comp2);
for(int i=0;i<n;i++){
tat[i]=p[i].ct-p[i].at;
tot_tat+=tat[i];
wt[i]=tat[i]-p[i].bt;
tot_wait+=wt[i];
}
cout<<"PID AT BT CT TAT WT"<<endl;
for(int i=0;i<n;i++){
cout<<p[i].pid<<" "<<p[i].at<<" "<<p[i].bt<<" "<<p[i].ct<<" "<<tat[i]<<"
"<<wt[i]<<endl;
}
cout<<"Avg. Waiting time: "<<(double)tot_wait/(double)n<<endl;
cout<<"Avg. Turnaround time: "<<(double)tot_tat/(double)n<<endl;
return;
}

OUTPUT:

You might also like