Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit a9cc1a5

Browse files
author
Rohit Kumar
authored
Initial Commit
1 parent a7b3428 commit a9cc1a5

12 files changed

+818
-0
lines changed

1.FCFS.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Program to Implement First Come First Serve Algorithm
2+
// Rohit Kumar | 1900320100131
3+
#include<stdio.h>
4+
void main()
5+
{
6+
// STDIN
7+
int n;
8+
printf("No of Process : ");
9+
scanf("%d",&n);
10+
int AT[n],BT[n],CT[n],WT[n],TAT[n];
11+
printf("Enter the AT & BT of Process\n");
12+
for(int i=0;i<n;i++)
13+
{
14+
printf("P[%d]\t",i);
15+
scanf("%d%d",&AT[i],&BT[i]);
16+
}
17+
// PROCESSING
18+
float avgWT = 0.0;
19+
float avgTAT = 0.0;
20+
// Calculation for 1st Process
21+
CT[0] = AT[0] + BT[0];
22+
TAT[0] = CT[0] - AT[0];
23+
WT[0] = TAT[0] - BT[0];
24+
// Loop for Calculation
25+
for(int i=1; i<n; i++)
26+
{
27+
if(CT[i-1]>AT[i])// if CT of 1st Process is more then than Arrival of Second Process
28+
CT[i] = CT[i-1] + BT[i];
29+
else // if CT of 1st Process is less then than Arrival of Second Process
30+
CT[i] = AT[i] + BT[i];
31+
TAT[i] = CT[i] - AT[i];
32+
WT[i] = TAT[i] - BT[i];
33+
avgTAT += TAT[i];
34+
avgWT += WT[i];
35+
}
36+
avgWT = avgWT/n;
37+
avgTAT = avgTAT/n;
38+
// STDOUT
39+
printf("\n\t\tAT\tBT\tWT\tTAT\n");
40+
for(int i=0;i<n;i++)
41+
printf("P[%d]\t%d\t%d\t%d\t%d\n",i,AT[i],BT[i],WT[i],TAT[i]);
42+
printf("\nAverage WT : %f Units\n",avgWT);
43+
printf("Average TAT : %f Units",avgTAT);
44+
}

1.FCFS.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
at1=[6,4,2,8,15]
2+
bt1=[1,3,2,1,3]
3+
at=[]
4+
bt=[]
5+
at=at1.copy()
6+
at. sort()
7+
for i in range(len(at)):
8+
r= at.index(at[i])
9+
bt.append(bt1[r])
10+
cf=[]
11+
tat=[]
12+
wt=[]
13+
cf.append(at[0]+bt[0])
14+
for i in range(1,len(at)):
15+
if at[i]>cf[i-1]:
16+
cf.append(at[i]+bt[i])
17+
else:
18+
cf.append(cf[i-1]+bt[i])
19+
for i in range(len(at)):
20+
tat.append(cf[i]-at[i])
21+
wt.append(tat[i]-bt[i])
22+
print("\tAT\tST\tWT\tTAT")
23+
for i in range(len(at)):
24+
print("P"+str(i+1),at[i],bt[i],wt[i],tat[i],sep="\t")
25+
print("Average Wait time ="+str(round(sum(wt) / len(wt), 2))+" unit")
26+
print("Average Turn Around time ="+str(round(sum(tat) / len(tat), 2))+" unit")

2.SJF.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
at1=[]
2+
bt1=[]
3+
at=[]
4+
bt=[]
5+
atT=[0,1,4,6,8]
6+
btT=[4,2,1,4,2]
7+
ref=atT.copy()
8+
at1.append(min(atT))
9+
bt1.append(btT[atT.index(min(atT))])
10+
atT.remove(min(atT))
11+
btT.remove(btT[atT.index(min(atT))])
12+
while len(atT)!=0:
13+
bt1.append(min(btT))
14+
at1.append( atT[btT.index(min(btT))] )
15+
atT.remove(atT[btT.index(min(btT))])
16+
btT.remove(min(btT))
17+
t=0
18+
i=1
19+
tempS=[]
20+
for i in range(len(at1)):
21+
if at1[i]<=t:
22+
at.append(at1[i])
23+
bt.append(bt1[i])
24+
t+=bt1[i]
25+
else:
26+
tempS.append(i)
27+
for i in tempS:
28+
at.append(at1[i])
29+
bt.append(bt1[i])
30+
31+
cf=[]
32+
tat=[]
33+
wt=[]
34+
35+
cf.append(at[0]+bt[0])
36+
for i in range(1,len(at)):
37+
if at[i]>cf[i-1]:
38+
cf.append(at[i]+bt[i])
39+
else:
40+
cf.append(cf[i-1]+bt[i])
41+
for i in range(len(at)):
42+
tat.append(cf[i]-at[i])
43+
wt.append(tat[i]-bt[i])
44+
45+
print(" AT BT WT TAT")
46+
for i in range(len(at)):
47+
print("P"+str((ref.index(at[i]))+1),at[i],bt[i],wt[i],tat[i],sep=" ")
48+
print("AVERAGE WAIT TIME "+str(sum(wt)/len(wt))+" UNITS")
49+
print("AVERAGE TURN AROUND TIME "+str(sum(tat)/len(tat))+" UNITS")

3.Priority Non Preemptive.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Program to Implement Priority Scheduling (Non - Preemptive)
2+
// Rohit Kumar | 1900320100131
3+
#include<stdio.h>
4+
// Function to Swap Numbers
5+
void swap(int *a, int *b)
6+
{
7+
int temp;
8+
temp = *a;
9+
*a = *b;
10+
*b = temp;
11+
}
12+
// Driver code
13+
void main()
14+
{
15+
// STDIN
16+
int n;
17+
printf("No of Process : ");
18+
scanf("%d",&n);
19+
int AT[n],BT[n],PR[n],CT[n],WT[n],TAT[n],RBT[n];
20+
printf("Enter the AT,BT and Priority of Process\n");
21+
for(int i=0;i<n;i++)
22+
{
23+
printf("P[%d]\t",i);
24+
scanf("%d%d%d",&AT[i],&BT[i],&PR[i]);
25+
}
26+
// PROCESSING
27+
float avgWT = 0.0;
28+
float avgTAT = 0.0;
29+
// Loop to Sort Programs on the Basic of Arrival Time
30+
for(int i=0;i<n;i++)
31+
{
32+
for(int j=0;j<n-1-i;j++)
33+
{
34+
if(*(AT+j)>*(AT+j+1))
35+
{
36+
swap((AT+j),(AT+j+1));//Swap Arrival Time
37+
swap((BT+j),(BT+j+1));//Swap Burst Time
38+
swap((PR+j),(PR+j+1));//Swap Priority
39+
}
40+
}
41+
}
42+
int flag[n];
43+
// Initilise flag of All Process to Zero
44+
for(int i=0;i<n;i++)
45+
{
46+
flag[i]=0;
47+
}
48+
int time=AT[0],min,k;
49+
// Loop to Find Samll Process on the Basic of Arrival Time and Priority
50+
for(int j=0;j<n;j++)
51+
{
52+
min=100;
53+
k=0;
54+
for(int i=0;i<n;i++)
55+
{
56+
if(flag[i]==0)
57+
{
58+
if(AT[i]<=time)
59+
{
60+
if(PR[i]<min)
61+
{
62+
min=PR[i];
63+
k=i;
64+
}
65+
}
66+
}
67+
}
68+
// Main Calculations
69+
CT[k]=BT[k]+time;
70+
time=time+BT[k];
71+
flag[k]=1;
72+
WT[k]=CT[k]-(BT[k]+AT[k]);
73+
TAT[k]=CT[k]-AT[k];
74+
avgTAT += TAT[k];
75+
avgWT += WT[k];
76+
}
77+
78+
avgWT = avgWT/n;
79+
avgTAT = avgTAT/n;
80+
// STDOUT
81+
printf("\n\t\tAT\tBT\tPR\tWT\tTAT\n");
82+
for(int i=0;i<n;i++)
83+
printf("P[%d]\t%d\t%d\t%d\t%d\t%d\n",i,AT[i],BT[i],PR[i],WT[i],TAT[i]);
84+
printf("\nAverage Waiting Time is %f Units\n",avgWT);
85+
printf("Average Turn Around Time is %f Units",avgTAT);
86+
}

3.Priority Preemptive.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Program to Implement Priority Scheduling (Preemptive)
2+
// Rohit Kumar | 1900320100131
3+
#include <stdio.h>
4+
// Function to Swap two Numbers
5+
void swap(int *a, int *b)
6+
{
7+
int temp;
8+
temp = *a;
9+
*a = *b;
10+
*b = temp;
11+
}
12+
// Driver Code
13+
void main()
14+
{
15+
int n;
16+
// STDIN
17+
printf("No of Process : ");
18+
scanf("%d", &n);
19+
int AT[n], BT[n], PR[n], CT[n], WT[n], TAT[n], RBT[n];
20+
printf("Enter the AT,BT and Priority of Process\n");
21+
for (int i = 0; i < n; i++)
22+
{
23+
printf("P[%d]\t", i);
24+
scanf("%d%d%d", &AT[i], &BT[i], &PR[i]);
25+
}
26+
27+
float avgWT = 0.0;
28+
float avgTAT = 0.0;
29+
30+
// Loop for Sorting Process on the Basic of Arrival time
31+
for (int i = 0; i < n; i++)
32+
{
33+
for (int j = 0; j < n - 1 - i; j++)
34+
{
35+
if (*(AT + j) > *(AT + j + 1))
36+
{
37+
swap((AT + j), (AT + j + 1));
38+
swap((BT + j), (BT + j + 1));
39+
swap((PR + j), (PR + j + 1));
40+
}
41+
}
42+
}
43+
44+
int sum = 0;
45+
// Loop to Copy Burst Time to Rough Burst Time & calculating Sum of Burst Time of all Process
46+
for (int i = 0; i < n; i++)
47+
{
48+
RBT[i] = BT[i];
49+
sum += BT[i];
50+
}
51+
52+
int time = AT[0], min, k;
53+
// Loop for Calculation of smallest Process
54+
// Takes Each Process by 1 Unit
55+
for (int i = 0; i < sum; i++)
56+
{
57+
min = 100;
58+
k = 0;
59+
for (int j = 0; j < n; j++)
60+
{
61+
if (RBT[j] != 0)
62+
{
63+
if (AT[j] <= time)
64+
{
65+
if (PR[j] < min)
66+
{
67+
min = PR[j];
68+
k = j;
69+
}
70+
}
71+
}
72+
}
73+
74+
RBT[k] = RBT[k] - 1;
75+
time = time + 1;
76+
CT[k] = time;
77+
}
78+
// Calculation of Burst Time
79+
for (int i = 0; i < n; i++)
80+
{
81+
WT[i] = CT[i] - AT[i] - BT[i];
82+
TAT[i] = CT[i] - AT[i];
83+
avgTAT += TAT[i];
84+
avgWT += WT[i];
85+
}
86+
87+
avgWT = avgWT / n;
88+
avgTAT = avgTAT / n;
89+
// STDOUT
90+
printf("\n\t\tAT\tBT\tPR\tWT\tTAT\n");
91+
for (int i = 0; i < n; i++)
92+
{
93+
printf("P[%d]\t%d\t%d\t%d\t%d\t%d\n", i, AT[i], BT[i], PR[i], WT[i], TAT[i]);
94+
}
95+
printf("\nAverage Waiting Time is %f Units\n", avgWT);
96+
printf("Average Turn Around Time is %f Units", avgTAT);
97+
}

4.Round Robin.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Program to Implement Round Robin
2+
// Rohit Kumar | 1900320100131
3+
#include <stdio.h>
4+
void main()
5+
{
6+
int n, q;
7+
// STDIN
8+
printf("No of Process : ");
9+
scanf("%d", &n);
10+
int AT[n], BT[n], CT[n], WT[n], TAT[n], RBT[n];
11+
printf("Enter the AT & BT of Process\n");
12+
for (int i = 0; i < n; i++)
13+
{
14+
printf("P[%d]\t", i);
15+
scanf("%d%d", &AT[i], &BT[i]);
16+
}
17+
printf("\nEnter Quantum : ");
18+
scanf("%d", &q);
19+
// PROCESSING
20+
float AvgWT = 0.0;
21+
float AvgTAT = 0.0;
22+
int sum = 0;
23+
// Loop for Calculation of Sum of Burst Time and Copy Burst time to Rough Burst Time
24+
for (int i = 0; i < n; i++)
25+
{
26+
RBT[i] = BT[i];
27+
sum = sum + BT[i];
28+
}
29+
int time = 0;
30+
while (time < sum) //To Check if the total time is less than sum
31+
{
32+
for (int i = 0; i < n; i++)
33+
{
34+
if (RBT[i] != 0)
35+
{
36+
if (RBT[i] >= q)//If Busrt Time is More or Equal to Quantum
37+
{
38+
RBT[i] = RBT[i] - q;
39+
time = time + q;
40+
if (RBT[i] == 0)
41+
CT[i] = time;
42+
}
43+
else//If Burst Time is Less than Quantum
44+
{
45+
time = time + RBT[i];
46+
RBT[i] = 0;
47+
CT[i] = time;
48+
}
49+
}
50+
}
51+
}
52+
// Calculations
53+
for (int i = 0; i < n; i++)
54+
{
55+
WT[i] = CT[i] - BT[i];
56+
TAT[i] = CT[i];
57+
AvgTAT += TAT[i];
58+
AvgWT += WT[i];
59+
}
60+
AvgWT = AvgWT / n;
61+
AvgTAT = AvgTAT / n;
62+
// STDOUT
63+
printf("\n\t\tAT\tBT\tWT\tTAT\n");
64+
for (int i = 0; i < n; i++)
65+
printf("P[%d]\t%d\t%d\t%d\t%d\n", i, AT[i], BT[i], WT[i], TAT[i]);
66+
printf("\nAverage Waiting Time is %f Units\n", AvgWT);
67+
printf("Average Turn Around Time is %f Units", AvgTAT);
68+
}

0 commit comments

Comments
 (0)