7
7
// Structure to represent a process
8
8
struct Process {
9
9
char pid [32 ]; // Process ID
10
- int arrival_time ; // Arrival time in microseconds
11
- int job_time ; // Burst time in microseconds
12
- int remaining_time ; // Remaining time in microseconds
13
- int turnaround_time ; // Turnaround time in microseconds
14
- int response_time ; // Waiting time in microseconds
10
+ double arrival_time ; // Arrival time in microseconds
11
+ double job_time ; // Burst time in microseconds
12
+ double remaining_time ; // Remaining time in microseconds
13
+ double turnaround_time ; // Turnaround time in microseconds
14
+ double response_time ; // Waiting time in microseconds
15
15
};
16
16
17
17
//Linked list of log struct
18
18
struct log {
19
19
char pid [32 ];
20
- int start ;
21
- int finish ;
20
+ double start ;
21
+ double finish ;
22
22
struct log * next ;
23
23
};
24
24
//Function to create a new node
25
- struct log * createLog (char * pid , int start , int finish ){
25
+ struct log * createLog (char * pid , double start , double finish ){
26
26
struct log * newNode = (struct log * )malloc (sizeof (struct log ));
27
27
strcpy (newNode -> pid ,pid );
28
28
newNode -> start = start ;
@@ -31,7 +31,7 @@ struct log* createLog(char *pid, int start, int finish){
31
31
return newNode ;
32
32
}
33
33
//Function to insert a node at the end of the linked list
34
- void insertLog (struct log * * head ,char * pid , int start , int finish ){
34
+ void insertLog (struct log * * head ,char * pid , double start , double finish ){
35
35
struct log * newNode = createLog (pid ,start ,finish );
36
36
if (* head == NULL ){
37
37
* head = newNode ;
@@ -57,14 +57,14 @@ void combineLogs(struct log*head){
57
57
}
58
58
void printLog (struct log * head ){
59
59
while (head != NULL ){
60
- printf ("%s %d %d " ,head -> pid ,head -> start ,head -> finish );
60
+ printf ("%s %.2lf %.2lf " ,head -> pid ,head -> start ,head -> finish );
61
61
head = head -> next ;
62
62
}
63
63
printf ("\n" );
64
64
}
65
65
66
66
void printAvgTurnaroundResponse (struct Process processes [], int n ){
67
- int sum = 0 ;
67
+ double sum = 0 ;
68
68
for (int i = 0 ;i < n ;i ++ ){
69
69
sum += processes [i ].turnaround_time ;
70
70
}
@@ -78,7 +78,7 @@ void printAvgTurnaroundResponse(struct Process processes[], int n){
78
78
79
79
80
80
struct log * fcfs (struct Process processes [], int n ) {
81
- int current_time = 0 ;
81
+ double current_time = 0 ;
82
82
struct log * head = NULL ;
83
83
84
84
for (int i = 0 ; i < n ; i ++ ) {
@@ -97,16 +97,16 @@ struct log* fcfs(struct Process processes[], int n) {
97
97
}
98
98
99
99
100
- struct log * roundRobin (struct Process processes [], int n , int time_slice ) {
100
+ struct log * roundRobin (struct Process processes [], int n , double time_slice ) {
101
101
int remaining_processes = n ;
102
- int current_time = 0 ;
102
+ double current_time = 0 ;
103
103
int current_process = 0 ;
104
104
105
105
struct log * head = NULL ;
106
106
107
107
while (remaining_processes > 0 ) {
108
108
if (processes [current_process ].remaining_time > 0 ) {
109
- int execution_time = (processes [current_process ].remaining_time < time_slice )
109
+ double execution_time = (processes [current_process ].remaining_time < time_slice )
110
110
? processes [current_process ].remaining_time
111
111
: time_slice ;
112
112
if (processes [current_process ].response_time == -1 ){
@@ -132,7 +132,7 @@ struct log* roundRobin(struct Process processes[], int n, int time_slice) {
132
132
133
133
134
134
struct log * sjf (struct Process processes [], int n ) {
135
- int current_time = 0 ;
135
+ double current_time = 0 ;
136
136
int completed = 0 ;
137
137
bool executed [n ]; // Array to track if a process has been executed
138
138
struct log * head = NULL ;
@@ -144,7 +144,7 @@ struct log* sjf(struct Process processes[], int n) {
144
144
145
145
while (completed < n ) {
146
146
int shortest_job_index = -1 ;
147
- int shortest_job_time = INT_MAX ;
147
+ double shortest_job_time = __DBL_MAX__ ;
148
148
149
149
// Find the shortest job that has arrived and not yet executed
150
150
for (int i = 0 ; i < n ; i ++ ) {
@@ -156,7 +156,7 @@ struct log* sjf(struct Process processes[], int n) {
156
156
157
157
if (shortest_job_index != -1 ) {
158
158
// Execute the shortest job
159
- int execution_time = processes [shortest_job_index ].job_time ;
159
+ double execution_time = processes [shortest_job_index ].job_time ;
160
160
current_time += execution_time ;
161
161
162
162
// Update turnaround, and waiting times
@@ -170,7 +170,7 @@ struct log* sjf(struct Process processes[], int n) {
170
170
completed ++ ;
171
171
} else {
172
172
// If no process is ready to execute, move time forward
173
- int soonest_job = INT_MAX ;
173
+ double soonest_job = __DBL_MAX__ ;
174
174
for (int i = 0 ; i < n ; i ++ ) {
175
175
if (!executed [i ] && processes [i ].arrival_time < soonest_job ) {
176
176
soonest_job = processes [i ].arrival_time ;
@@ -185,13 +185,13 @@ struct log* sjf(struct Process processes[], int n) {
185
185
186
186
187
187
struct log * shortestRemainingTimeFirst (struct Process processes [], int n ) {
188
- int current_time = 0 ;
188
+ double current_time = 0 ;
189
189
int completed = 0 ;
190
190
struct log * head = NULL ;
191
191
192
192
while (completed < n ) {
193
193
int shortest_job_index = -1 ;
194
- int shortest_job_time = INT_MAX ;
194
+ double shortest_job_time = __DBL_MAX__ ;
195
195
bool job_found = false;
196
196
197
197
for (int i = 0 ; i < n ; i ++ ) {
@@ -205,15 +205,15 @@ struct log* shortestRemainingTimeFirst(struct Process processes[], int n) {
205
205
}
206
206
207
207
if (!job_found ) {
208
- int temptime = INT_MAX ;
208
+ double temptime = __DBL_MAX__ ;
209
209
for (int i = 0 ;i < n ;i ++ ){
210
210
if (processes [i ].arrival_time > current_time && processes [i ].arrival_time < temptime ){
211
211
temptime = processes [i ].arrival_time ;
212
212
}
213
213
}
214
214
current_time = temptime ;
215
215
} else {
216
- int execution_time = INT_MAX ; // Execute one unit of time
216
+ double execution_time = __DBL_MAX__ ; // Execute one unit of time
217
217
for (int i = 0 ;i < n ;i ++ ){
218
218
if (processes [i ].arrival_time > current_time && processes [i ].remaining_time > 0 && i != shortest_job_index ){
219
219
if (processes [i ].arrival_time - current_time < execution_time ){
@@ -281,17 +281,17 @@ struct Process dequeue(struct Queue* queue) {
281
281
}
282
282
283
283
// Function to perform MLFQ scheduling
284
- struct log * mlfq (struct Process processes [], int n , int time_slice_high , int time_slice_med , int time_slice_low , int boost ) {
284
+ struct log * mlfq (struct Process processes [], int n , double time_slice_high , double time_slice_med , double time_slice_low , double boost ) {
285
285
struct Queue * queue_high = createQueue (n );
286
286
struct Queue * queue_med = createQueue (n );
287
287
struct Queue * queue_low = createQueue (n );
288
288
struct log * head = NULL ;
289
289
int queued [n ];
290
290
for (int i = 0 ;i < n ;i ++ ) queued [i ]= 0 ;
291
291
292
- int current_time = 0 ;
292
+ double current_time = 0 ;
293
293
int completed = 0 ;
294
- int boosttrack = 0 ;
294
+ double boosttrack = 0 ;
295
295
296
296
while (completed < n ) {
297
297
for (int i = 0 ; i < n ; i ++ ) {
@@ -306,7 +306,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
306
306
// Dequeue from the highest-priority queue first
307
307
if (!isEmpty (queue_high )) {
308
308
struct Process process = dequeue (queue_high );
309
- int execution_time = (process .remaining_time < time_slice_high ) ? process .remaining_time : time_slice_high ;
309
+ double execution_time = (process .remaining_time < time_slice_high ) ? process .remaining_time : time_slice_high ;
310
310
if (boosttrack + execution_time > boost ){
311
311
execution_time = boost - boosttrack ;
312
312
boosttrack = 0 ;
@@ -343,7 +343,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
343
343
}
344
344
} else if (!isEmpty (queue_med )) {
345
345
struct Process process = dequeue (queue_med );
346
- int execution_time = (process .remaining_time < time_slice_med ) ? process .remaining_time : time_slice_med ;
346
+ double execution_time = (process .remaining_time < time_slice_med ) ? process .remaining_time : time_slice_med ;
347
347
if (boosttrack + execution_time > boost ){
348
348
execution_time = boost - boosttrack ;
349
349
boosttrack = 0 ;
@@ -369,7 +369,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
369
369
}
370
370
} else if (!isEmpty (queue_low )) {
371
371
struct Process process = dequeue (queue_low );
372
- int execution_time = (process .remaining_time < time_slice_low ) ? process .remaining_time : time_slice_low ;
372
+ double execution_time = (process .remaining_time < time_slice_low ) ? process .remaining_time : time_slice_low ;
373
373
if (boosttrack + execution_time > boost ){
374
374
execution_time = boost - boosttrack ;
375
375
boosttrack = 0 ;
@@ -394,7 +394,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
394
394
enqueue (queue_low , process );
395
395
}
396
396
} else {
397
- int temptime = INT_MAX ;
397
+ double temptime = __DBL_MAX__ ;
398
398
for (int i = 0 ;i < n ;i ++ ){
399
399
if (processes [i ].arrival_time > current_time && processes [i ].arrival_time < temptime ){
400
400
temptime = processes [i ].arrival_time ;
@@ -433,15 +433,16 @@ int main(int argc, char **argv){
433
433
FILE * input = fopen (argv [1 ],"r" );
434
434
FILE * output = freopen (argv [2 ],"w" ,stdout );
435
435
struct Process processes [32768 ],clonedProcesses [32768 ];
436
- int TsRR = atoi (argv [3 ]),TsMLFQ1 = atoi (argv [4 ]),TsMLFQ2 = atoi (argv [5 ]),TsMLFQ3 = atoi (argv [6 ]),BMLFQ = atoi (argv [7 ]);
436
+ char * garb ;
437
+ double TsRR = strtod (argv [3 ],& garb ),TsMLFQ1 = strtod (argv [4 ],& garb ),TsMLFQ2 = strtod (argv [5 ],& garb ),TsMLFQ3 = strtod (argv [6 ],& garb ),BMLFQ = strtod (argv [7 ],& garb );
437
438
struct log * head ;
438
439
439
440
char process [256 ];
440
441
int numberProcesses = 0 ;
441
442
for (int i = 0 ; fgets (process ,sizeof (process ),input );i ++ ){
442
443
strcpy (processes [i ].pid ,strtok (process ," " ));
443
- processes [i ].arrival_time = atoi (strtok (NULL ," " ));
444
- processes [i ].job_time = atoi (strtok (NULL ," " ));
444
+ processes [i ].arrival_time = strtod (strtok (NULL ," " ), & garb );
445
+ processes [i ].job_time = strtod (strtok (NULL ," " ), & garb );
445
446
processes [i ].remaining_time = processes [i ].job_time ;
446
447
processes [i ].response_time = -1 ;
447
448
numberProcesses = i + 1 ;
0 commit comments