-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcpuScheduling.c
154 lines (154 loc) · 3.37 KB
/
cpuScheduling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include<stdio.h>
struct cpuScheduling{
int no;
int burst;
int burstCopy;
int priority;
int turnAround;
int waiting;
}p[10],temp;
void print(int n){
int i;
printf("\nProc\tbur\tturn\twait\n");
for(i=0;i<n;i++){
printf("p[%d]\t%d\t%d\t%d\n",p[i].no,p[i].burst,p[i].turnAround,p[i].waiting);
}
}
void avgTWCalc(int n,int flag){
if(flag){
printf("\nGantt Chart\n");
printf("0");
for(int i=0;i<n;i++){
printf("[%d] %d ",p[i].no,p[i].turnAround);
}
printf("\n");
print(n);
}
float totalTurn=0,totalWait=0,avgT,avgW;
for(int i=0;i<n;i++){
totalTurn+=p[i].turnAround;
totalWait+=p[i].waiting;
}
avgT=totalTurn/n;
avgW=totalWait/n;
printf("Average Turnaround Time = %.2f \n",avgT);
printf("Average Waiting Time = %.2f \n",avgW);
}
void fcfs(int n){
int i,com=0;
float totalTurn=0,totalWait=0,avgT,avgW;
for(i=0;i<n;i++){
com=com+p[i].burst;
p[i].turnAround=com;
p[i].waiting=com-p[i].burst;
}
avgTWCalc(n,1);
}
void sjf(int n){
int i,j,com=0;
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(p[j].burst>p[j+1].burst){
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
for(i=0;i<n;i++){
com=com+p[i].burst;
p[i].turnAround=com;
p[i].waiting=com-p[i].burst;
}
avgTWCalc(n,1);
}
void priority(int n){
int i,j,com=0;
printf("Enter the Priority\n");
for(i=0;i<n;i++){
printf("p[%d] :- ",i);
scanf("%d",&p[i].priority);
}
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(p[j].priority>p[j+1].priority){
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
for(i=0;i<n;i++){
com=com+p[i].burst;
p[i].turnAround=com;
p[i].waiting=com-p[i].burst;
}
avgTWCalc(n,1);
}
void roundRobin(int n){
int i,quanta,totalBurst=0,com;
printf("Enter the Time Quanta :- ");
scanf("%d",&quanta);
com=0;
for(i=0;i<n;i++){
totalBurst+=p[i].burst;
}
printf("Gatt chart :- \n 0 ");
while(com<totalBurst){
for(i=0;i<n;i++){
if(p[i].burstCopy>0){
if(p[i].burstCopy<=quanta){
com+=p[i].burstCopy;
p[i].burstCopy=0;
p[i].turnAround=com;
p[i].waiting=com-p[i].burst;
}
else{
com+=quanta;
p[i].burstCopy-=quanta;
}
printf("p[%d] %d ",i,com);
}
}
}
print(n);
avgTWCalc(n,0);
}
int main(){
int i,j,n,option;
do{
printf("*************** MENU *************");
printf("\nPlease choose your option\n");
printf("1.FCFS\n2.SJF\n3.PRIORITY\n4.RoundRobin\n5.Exit\n");
scanf("%d",&option);
if(option!=5){
printf("Enter no. of process :- ");
scanf("%d",&n);
printf("Enter the burst Time\n");
for(i=0;i<n;i++){
printf("p[%d] :- ",i);
scanf("%d",&p[i].burst);
p[i].burstCopy=p[i].burst;
p[i].no=i;
}
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++){
if(p[j].no>p[j+1].no){
temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
}
}
}
}
switch(option){
case 1:fcfs(n);break;
case 2:sjf(n);break;
case 3:priority(n);break;
case 4:roundRobin(n);break;
case 5:break;
default:printf("Enter a valid option");
}
}while(option!=5);
return 0;
}