//4.
a FCFS CPU Scheduling
#include<stdio.h>
int main(){
int n,i;
int wt[20],bt[20],tat[20],Twt=0,Ttat=0;
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=wt[i-1]+bt[i-1];
Twt+=wt[i];
}
for(i=0;i<n;i++){
tat[i]=wt[i]+bt[i];
Ttat+=tat[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}
OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter the number of processes
4
Enter the burst time for each process
1
3
4
5
P no B.T W.T T.A.T
1 1 0 1
2 3 1 4
3 4 4 8
4 5 8 13
13 26
3.25 6.50
//4.b sjf
#include<stdio.h>
#include<stdlib.h>
int main(){
int n,i=0,temp=0,j=0;
system("clear");
int wt[20],bt[20],tat[20],pno[20],Twt=0,Ttat=0;
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the burst time for each process\n");
for(i=0;i<n;i++){
scanf("%d",&bt[i]);
pno[i]=i;
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(bt[j]<bt[i]){
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
temp=0;
temp=pno[j];
pno[j]=pno[i];
pno[i]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++){
wt[i]=wt[i-1]+bt[i-1];
Twt+=wt[i];
}
for(i=0;i<n;i++){
tat[i]=wt[i]+bt[i];
Ttat+=tat[i];
}
Avgwt=(float)Twt/n;
Avgtat=(float)Ttat/n;
printf("P no\tB.T\tW.T\tT.A.T\n");
for(i=0;i<n;i++){
printf("%d\t%d\t%d\t%d\n",pno[i],bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}
OUTPUT:-
[cse22151@cseserver os]$./a.out
Enter the number of processes
3
Enter the burst time for each process
3
2
6
P no B.T W.T T.A.T
1 2 0 2
0 3 2 5
2 6 5 11
__ __ 7 18
__ __ 2.33 6.00
//4.c Roundrobin
#include<stdio.h>
#include<sys/stat.h>
int main(){
int n,i,count=0,t=0,ts, wt[20],bt[20],tat[20],Twt=0,Ttat=0,RT[15];
float Avgwt=0,Avgtat=0;
system("clear");
printf("Enter the number of processes\n");
scanf("%d",&n);
printf("Enter the time slice\n");
scanf("%d",&ts);
printf("Enter the burst time for each process\n");
for(i=1;i<=n;i++){
scanf("%d",&bt[i]);
RT[i]=bt[i];
}
while(1){
for(i=1;i<=n;i++){
if(RT[i]>0){
if(RT[i]>ts){
RT[i]-=ts;
t=t+ts;
}
else{
t=t+RT[i];
RT[i]=0;
tat[i]=t;
count++;
}
}
}
if(count==n){
break;
}
}
for(i=1;i<=n;i++){
wt[i]=tat[i]-bt[i];
Twt+=wt[i];
}
for(i=1;i<=n;i++){
Ttat+=tat[i];
}
Avgwt=(float)T
wt/n;
Avgtat=(float)Tt
at/n;
printf("P no\tB.T\tW.T\tT.A.T\
n"); for(i=1;i<=n;i++){
printf("%d\t%d\t%d\t%d\n",i,bt[i],wt[i],tat[i]);
}
printf(" \t \t%d\t%d\n",Twt,Ttat);
printf(" \t \t%.2f\t%.2f\t\n",Avgwt,Avgtat);
}
OUTPUT:-
[cse22151@cseserver
os]$./a.out Enter the number
of processes
4
Enter the time
slice 2
Enter the burst time for each
process
3
4
5
6
P no B.T W.T T.A.T
1 3 6 9
2 4 7 11
3 5 11 16
4 6 12 18
36 54
9.00 13.50