-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSCode.c
201 lines (175 loc) · 5.8 KB
/
PSCode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<stdio.h>
#include<stdlib.h>
//Symbol for complexity: n = Total no. of process
// m = Turn Around Time
int main(){
int noOfProcess; //-- 1
int i, at, pr, bt; //-- 1
int t=0; //-- 1
int complete=0; //-- 1
int minPriIndexC; //-- 1
int minPri, minPriIndex; //-- 1
//Taking User Input
printf("No of process: "); //-- 1
scanf("%d",&noOfProcess); //-- 1
int arr[8][noOfProcess]; //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("\nFor Process P%d \n", i); //-- n
printf("Priority: "); //-- n
scanf("%d", &pr); //-- n
printf("Arrival Time: "); //-- n
scanf("%d", &at); //-- n
printf("Burst Time: "); //-- n
scanf("%d", &bt); //-- n
arr[0][i]=i; //-- n
arr[1][i]=at; //Arrival Time //-- n
arr[2][i]=pr; //Priority //-- n
arr[3][i]=bt; //Burst Time //-- n
arr[4][i]=0; //Wait Time //-- n
arr[5][i]=0; //State //-- n
arr[6][i]=0; //Turn Around Time //-- n
arr[7][i]=0; //WaitTime to incriment Priority //-- n
}
printf("\n-----------------------------------\n\n");
//Saving the burst time of each process.
int burstTime[noOfProcess]; //-- 1
for(i=0; i<noOfProcess;i++){ //-- 1
burstTime[i]=arr[3][i]; //-- n
}
int initialPri[noOfProcess]; //-- 1
for(i=0; i<noOfProcess;i++){ //-- 1
initialPri[i]=arr[2][i]; //-- n
}
//Checking initial minimum priority.
for(i=0; i<noOfProcess; i++){ //-- 1
if(arr[1][i]==0){ //-- n
minPriIndexC = i; //-- log(n)
break; //-- log(n)
}
}
while(complete==0){ //-- 1
//Checking for the arrival time
for(i=0;i<noOfProcess; i++){ //-- m
if(arr[1][i]==t){ //-- nm
arr[5][i]=2; //-- mlog(n)
}
}
minPri = 100000; //-- m
minPriIndex = 0; //-- m
//Finding the minimum priority
for(i=0; i<noOfProcess; i++){ //-- m
if(arr[2][i]<minPri&&arr[5][i]==2){ //-- mn
minPriIndex = i; //-- mlog(n)
minPri = arr[2][i]; //-- mlog(n)
}
}
t++; //-- m
if(minPriIndexC!=minPriIndex){ //-- m
int time=2; //-- m
while(time!=0){ //-- m
//Checking for the arrival time
for(i=0;i<noOfProcess; i++){ //-- 3m
if(arr[1][i]==t){ //-- 3mn
arr[5][i]=2; //-- 3mlog(n)
}
}
t++; //-- 3m
for(i=0; i<noOfProcess;i++){ //-- m
if(arr[5][i]==2){ //-- m
arr[4][i] +=1; //-- m/3
arr[7][i] +=1; //-- m/3
if(arr[7][i]==2){ //-- m/3
arr[7][i]=0; //-- m/9
arr[2][i] -=1; //-- m/9
}
}
}
time--; //-- 3m
}
}
//Execution
minPriIndexC = minPriIndex; //-- m
arr[3][minPriIndex] -=1; //-- m
arr[4][minPriIndex] -=1; //-- m
arr[7][minPriIndex] -=1; //-- m
if(arr[3][minPriIndex]==0){ //-- m
arr[6][minPriIndex] = t; //-- log(m)
arr[5][minPriIndex] = -1; //-- log(m)
arr[7][minPriIndex] = 0; //-- log(m)
}
//Checking for the completed process.
for(i=0; i<noOfProcess;i++){ //-- m
if(arr[5][i]==2){ //-- m
arr[4][i] +=1; //-- m/3
arr[7][i] +=1; //-- m/3
if(arr[7][i]==2){ //-- m/3
arr[7][i]=0; //-- m/9
arr[2][i] -=1; //-- m/9
}
}
}
//Checking the completion
int temp=0;
for(i=0; i<noOfProcess;i++){ //-- m
temp=temp+arr[5][i]; //-- mn
}
if((temp*(-1))==noOfProcess){ //-- m
complete=-1; //-- mn
}
}
/*End of the logic*/
//Output
printf("Priority Scheduling Table: \n"); //-- 1
printf("\nProcNo: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("P%d\t", arr[0][i]); //-- n
}
printf("\nArrT: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", arr[1][i]); //-- n
}
printf("\nInitPri: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", initialPri[i]); //-- n
}
printf("\nBurstT: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", burstTime[i]); //-- n
}
printf("\nWaitT: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", (arr[6][i]-arr[1][i])-burstTime[i]); //-- n
}
printf("\nCompT: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", arr[6][i]); //-- n
}
printf("\nTAT: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", arr[6][i]-arr[1][i]); //-- n
}
printf("\nFinalPri: \t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%d\t", arr[2][i]); //-- n
}
printf("\n\n\nRequired Answers: "); //-- 1
printf("\n\nPNo: |\t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf(" P%d", arr[0][i]); //-- n
printf(" | "); //-- n
}
printf("\n\nTAT: |\t"); //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%5d", arr[6][i]-arr[1][i]); //-- n
printf(" | "); //-- n
}
printf("\n\nWT: |\t"); //-- 1
int totalWaitTime=0; //-- 1
for(i=0; i<noOfProcess; i++){ //-- 1
printf("%5d", (arr[6][i]-arr[1][i])-burstTime[i]); //-- n
totalWaitTime += (arr[6][i]-arr[1][i])-burstTime[i]; //-- n
printf(" | "); //-- n
}
printf("\n\nTurn Around Time: %d second", t); //-- 1
printf("\n\nAverage Waiting Time : %.2f second\n\n", totalWaitTime/(float)noOfProcess ); //-- 1
}