Skip to content

Commit a2a9f13

Browse files
Added working SJF files
1 parent 1d696c8 commit a2a9f13

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

OS Lab/CPU Scheduling/non-pre-SJF.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<stdio.h>
2+
int main()
3+
{
4+
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
5+
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
6+
float wavg=0,tavg=0,tsum=0,wsum=0;
7+
printf(" -------Shortest Job First Scheduling ( NP )-------\n");
8+
printf("\nEnter the No. of processes :");
9+
scanf("%d",&n);
10+
11+
for(i=0;i<n;i++)
12+
{
13+
printf("\tEnter the burst time of %d process :",i+1);
14+
scanf(" %d",&bt[i]);
15+
printf("\tEnter the arrival time of %d process :",i+1);
16+
scanf(" %d",&at[i]);
17+
}
18+
19+
/*Sorting According to Arrival Time*/
20+
21+
for(i=0;i<n;i++)
22+
{
23+
for(j=0;j<n;j++)
24+
{
25+
if(at[i]<at[j])
26+
{
27+
temp=p[j];
28+
p[j]=p[i];
29+
p[i]=temp;
30+
temp=at[j];
31+
at[j]=at[i];
32+
at[i]=temp;
33+
temp=bt[j];
34+
bt[j]=bt[i];
35+
bt[i]=temp;
36+
}
37+
}
38+
}
39+
40+
/*Arranging the table according to Burst time,
41+
Execution time and Arrival Time
42+
Arrival time <= Execution time
43+
*/
44+
45+
for(j=0;j<n;j++)
46+
{
47+
btime=btime+bt[j];
48+
min=bt[k];
49+
for(i=k;i<n;i++)
50+
{
51+
if (btime>=at[i] && bt[i]<min)
52+
{
53+
temp=p[k];
54+
p[k]=p[i];
55+
p[i]=temp;
56+
temp=at[k];
57+
at[k]=at[i];
58+
at[i]=temp;
59+
temp=bt[k];
60+
bt[k]=bt[i];
61+
bt[i]=temp;
62+
}
63+
}
64+
k++;
65+
}
66+
wt[0]=0;
67+
for(i=1;i<n;i++)
68+
{
69+
sum=sum+bt[i-1];
70+
wt[i]=sum-at[i];
71+
wsum=wsum+wt[i];
72+
}
73+
74+
wavg=(wsum/n);
75+
for(i=0;i<n;i++)
76+
{
77+
ta=ta+bt[i];
78+
tt[i]=ta-at[i];
79+
tsum=tsum+tt[i];
80+
}
81+
82+
tavg=(tsum/n);
83+
84+
printf("************************");
85+
printf("\n RESULT:-");
86+
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around" );
87+
for(i=0;i<n;i++)
88+
{
89+
printf("\n p%d\t %d\t %d\t\t %d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
90+
}
91+
92+
printf("\n\nAVERAGE WAITING TIME : %f",wavg);
93+
printf("\nAVERAGE TURN AROUND TIME : %f",tavg);
94+
return 0;
95+
}

OS Lab/CPU Scheduling/preemSJF.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int arrival_time[10], burst_time[10], temp[10];
6+
int i, smallest, count = 0, time, limit;
7+
double wait_time = 0, turnaround_time = 0, end;
8+
float average_waiting_time, average_turnaround_time;
9+
printf("\nEnter the Total Number of Processes:\t");
10+
scanf("%d", &limit);
11+
printf("\nEnter Details of %d Processes\n", limit);
12+
for(i = 0; i < limit; i++)
13+
{
14+
printf("\nEnter Arrival Time:\t");
15+
scanf("%d", &arrival_time[i]);
16+
printf("Enter Burst Time:\t");
17+
scanf("%d", &burst_time[i]);
18+
temp[i] = burst_time[i];
19+
}
20+
burst_time[9] = 9999;
21+
for(time = 0; count != limit; time++)
22+
{
23+
smallest = 9;
24+
for(i = 0; i < limit; i++)
25+
{
26+
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
27+
{
28+
smallest = i;
29+
}
30+
}
31+
burst_time[smallest]--;
32+
if(burst_time[smallest] == 0)
33+
{
34+
count++;
35+
end = time + 1;
36+
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
37+
turnaround_time = turnaround_time + end - arrival_time[smallest];
38+
}
39+
}
40+
average_waiting_time = wait_time / limit;
41+
average_turnaround_time = turnaround_time / limit;
42+
printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
43+
printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
44+
return 0;
45+
}

0 commit comments

Comments
 (0)