Skip to content

Commit b895bcb

Browse files
committed
double
1 parent b171b93 commit b895bcb

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.o
22
main
3-
output.txt
3+
output.txt
4+
A2Tester

Assignment-2/main.c

+34-33
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77
// Structure to represent a process
88
struct Process {
99
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
1515
};
1616

1717
//Linked list of log struct
1818
struct log{
1919
char pid[32];
20-
int start;
21-
int finish;
20+
double start;
21+
double finish;
2222
struct log *next;
2323
};
2424
//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){
2626
struct log *newNode = (struct log*)malloc(sizeof(struct log));
2727
strcpy(newNode->pid,pid);
2828
newNode->start = start;
@@ -31,7 +31,7 @@ struct log* createLog(char *pid, int start, int finish){
3131
return newNode;
3232
}
3333
//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){
3535
struct log *newNode = createLog(pid,start,finish);
3636
if(*head == NULL){
3737
*head = newNode;
@@ -57,14 +57,14 @@ void combineLogs(struct log*head){
5757
}
5858
void printLog(struct log *head){
5959
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);
6161
head = head->next;
6262
}
6363
printf("\n");
6464
}
6565

6666
void printAvgTurnaroundResponse(struct Process processes[], int n){
67-
int sum = 0;
67+
double sum = 0;
6868
for(int i=0;i<n;i++){
6969
sum += processes[i].turnaround_time;
7070
}
@@ -78,7 +78,7 @@ void printAvgTurnaroundResponse(struct Process processes[], int n){
7878

7979

8080
struct log* fcfs(struct Process processes[], int n) {
81-
int current_time = 0;
81+
double current_time = 0;
8282
struct log* head=NULL;
8383

8484
for (int i = 0; i < n; i++) {
@@ -97,16 +97,16 @@ struct log* fcfs(struct Process processes[], int n) {
9797
}
9898

9999

100-
struct log* roundRobin(struct Process processes[], int n, int time_slice) {
100+
struct log* roundRobin(struct Process processes[], int n, double time_slice) {
101101
int remaining_processes = n;
102-
int current_time = 0;
102+
double current_time = 0;
103103
int current_process = 0;
104104

105105
struct log *head = NULL;
106106

107107
while (remaining_processes > 0) {
108108
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)
110110
? processes[current_process].remaining_time
111111
: time_slice;
112112
if(processes[current_process].response_time == -1){
@@ -132,7 +132,7 @@ struct log* roundRobin(struct Process processes[], int n, int time_slice) {
132132

133133

134134
struct log* sjf(struct Process processes[], int n) {
135-
int current_time = 0;
135+
double current_time = 0;
136136
int completed = 0;
137137
bool executed[n]; // Array to track if a process has been executed
138138
struct log *head = NULL;
@@ -144,7 +144,7 @@ struct log* sjf(struct Process processes[], int n) {
144144

145145
while (completed < n) {
146146
int shortest_job_index = -1;
147-
int shortest_job_time = INT_MAX;
147+
double shortest_job_time = __DBL_MAX__;
148148

149149
// Find the shortest job that has arrived and not yet executed
150150
for (int i = 0; i < n; i++) {
@@ -156,7 +156,7 @@ struct log* sjf(struct Process processes[], int n) {
156156

157157
if (shortest_job_index != -1) {
158158
// Execute the shortest job
159-
int execution_time = processes[shortest_job_index].job_time;
159+
double execution_time = processes[shortest_job_index].job_time;
160160
current_time += execution_time;
161161

162162
// Update turnaround, and waiting times
@@ -170,7 +170,7 @@ struct log* sjf(struct Process processes[], int n) {
170170
completed++;
171171
} else {
172172
// If no process is ready to execute, move time forward
173-
int soonest_job = INT_MAX;
173+
double soonest_job = __DBL_MAX__;
174174
for (int i = 0; i < n; i++) {
175175
if (!executed[i] && processes[i].arrival_time < soonest_job) {
176176
soonest_job = processes[i].arrival_time;
@@ -185,13 +185,13 @@ struct log* sjf(struct Process processes[], int n) {
185185

186186

187187
struct log* shortestRemainingTimeFirst(struct Process processes[], int n) {
188-
int current_time = 0;
188+
double current_time = 0;
189189
int completed = 0;
190190
struct log* head = NULL;
191191

192192
while (completed < n) {
193193
int shortest_job_index = -1;
194-
int shortest_job_time = INT_MAX;
194+
double shortest_job_time = __DBL_MAX__;
195195
bool job_found = false;
196196

197197
for (int i = 0; i < n; i++) {
@@ -205,15 +205,15 @@ struct log* shortestRemainingTimeFirst(struct Process processes[], int n) {
205205
}
206206

207207
if (!job_found) {
208-
int temptime = INT_MAX;
208+
double temptime = __DBL_MAX__;
209209
for(int i=0;i<n;i++){
210210
if(processes[i].arrival_time > current_time && processes[i].arrival_time < temptime){
211211
temptime = processes[i].arrival_time;
212212
}
213213
}
214214
current_time = temptime;
215215
} else {
216-
int execution_time = INT_MAX; // Execute one unit of time
216+
double execution_time = __DBL_MAX__; // Execute one unit of time
217217
for(int i=0;i<n;i++){
218218
if(processes[i].arrival_time > current_time && processes[i].remaining_time > 0 && i != shortest_job_index){
219219
if(processes[i].arrival_time - current_time < execution_time){
@@ -281,17 +281,17 @@ struct Process dequeue(struct Queue* queue) {
281281
}
282282

283283
// 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) {
285285
struct Queue* queue_high = createQueue(n);
286286
struct Queue* queue_med = createQueue(n);
287287
struct Queue* queue_low = createQueue(n);
288288
struct log* head = NULL;
289289
int queued[n];
290290
for(int i=0;i<n;i++) queued[i]=0;
291291

292-
int current_time = 0;
292+
double current_time = 0;
293293
int completed = 0;
294-
int boosttrack = 0;
294+
double boosttrack = 0;
295295

296296
while (completed < n) {
297297
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
306306
// Dequeue from the highest-priority queue first
307307
if (!isEmpty(queue_high)) {
308308
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;
310310
if(boosttrack + execution_time > boost){
311311
execution_time = boost - boosttrack;
312312
boosttrack = 0;
@@ -343,7 +343,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
343343
}
344344
} else if (!isEmpty(queue_med)) {
345345
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;
347347
if(boosttrack + execution_time > boost){
348348
execution_time = boost - boosttrack;
349349
boosttrack = 0;
@@ -369,7 +369,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
369369
}
370370
} else if (!isEmpty(queue_low)) {
371371
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;
373373
if(boosttrack + execution_time > boost){
374374
execution_time = boost - boosttrack;
375375
boosttrack = 0;
@@ -394,7 +394,7 @@ struct log* mlfq(struct Process processes[], int n, int time_slice_high, int tim
394394
enqueue(queue_low, process);
395395
}
396396
} else {
397-
int temptime = INT_MAX;
397+
double temptime = __DBL_MAX__;
398398
for(int i=0;i<n;i++){
399399
if(processes[i].arrival_time > current_time && processes[i].arrival_time < temptime){
400400
temptime = processes[i].arrival_time;
@@ -433,15 +433,16 @@ int main(int argc, char **argv){
433433
FILE *input = fopen(argv[1],"r");
434434
FILE *output = freopen(argv[2],"w",stdout);
435435
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);
437438
struct log* head;
438439

439440
char process[256];
440441
int numberProcesses=0;
441442
for(int i=0; fgets(process,sizeof(process),input);i++){
442443
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);
445446
processes[i].remaining_time = processes[i].job_time;
446447
processes[i].response_time = -1;
447448
numberProcesses = i+1;

0 commit comments

Comments
 (0)