-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfcfs_o.c
256 lines (236 loc) · 7.98 KB
/
fcfs_o.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* RICHMOND FRIMPONG
* CSE230004
* 212114641
* SUMMER 2015 SECTION Z
* A first come first serve algorithm with a CPU simulation
* July 15th , 2015
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "sch-helpers.h"
#define TRUE 1
FILE *log_file;
process processes[MAX_PROCESSES + 1];
int number_of_processes;
int nextProcess;
int context_switches;
int simulation_time;
int last_process_end_time = 0;
int cpu_utilized_time;
process_queue readyQueue;
process_queue waitingQueue;
// CPU's for Quadcore simulation
process *CPU[NUMBER_OF_PROCESSORS];
// Temporary "Before-Ready" queue
process *tmp_ready_process[MAX_PROCESSES + 1];
int tmp_ready_process_size;
void init_(void) {
number_of_processes = 0;
nextProcess = 0;
context_switches = 0;
cpu_utilized_time = 0;
simulation_time = 0;
tmp_ready_process_size = 0;
}
int compareArrivalTime(const void *a, const void *b) {
process *first = (process *) a;
process *second = (process *) b;
return first->arrivalTime - second->arrivalTime;
}
int compareProcessIds(const void *a, const void *b) {
process *first = (process *) a;
process *second = (process *) b;
if (first->pid == second->pid) {
return -1;
}
return first->pid - second->pid;
}
/**
* Move finish process into the waiting queue and terminate finish CPU or I/O bursts
*/
void running_process_to_waiting() {
int i;
for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
if (CPU[i] != NULL) {
fprintf(log_file, "CPU BURST && I/O BURST:%d\t", CPU[i]->bursts[CPU[i]->currentBurst].length);
fprintf(log_file, "STEPS:%d\n", CPU[i]->bursts[CPU[i]->currentBurst].step);
if (CPU[i]->bursts[CPU[i]->currentBurst].step == CPU[i]->bursts[CPU[i]->currentBurst].length) {
//context_switches++;
CPU[i]->currentBurst++;
if (CPU[i]->currentBurst < CPU[i]->numberOfBursts) {
enqueueProcess(&waitingQueue, CPU[i]); // Puts the finish Burst into the waiting queue
fprintf(log_file, "Waiting process id:%d\t&& Current Process id:%d\n", waitingQueue.front->data->pid, CPU[i]->pid);
} else {
CPU[i]->endTime = simulation_time;
last_process_end_time = simulation_time;
}
CPU[i] = NULL;
}
}
}
}
/**
* Returns the current number of cpus running
*/
int runningProcesses() {
int runningProcesses = 0;
int i;
for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
if (CPU[i] != NULL) {
runningProcesses++;
}
}
return runningProcesses;
}
/**
* Gets the next ready process to be fed into the Cpu
*/
process *get_next_sch_process() {
if (readyQueue.size == 0) {
return NULL;
}
process *cpu_ready = readyQueue.front->data;
dequeueProcess(&readyQueue);
return cpu_ready;
}
/**
* initialize the processes to a temporary array
*/
void incoming_process_init() {
while (nextProcess < number_of_processes && processes[nextProcess].arrivalTime <= simulation_time) {
tmp_ready_process[tmp_ready_process_size++] = &processes[nextProcess++];
}
}
/**
* Gets the most ready process and puts it the Cpu for execution
*/
void most_ready_running_in_cpu() {
int i = 0;
qsort(tmp_ready_process, tmp_ready_process_size, sizeof (process*), compareProcessIds);
for (i = 0; i < tmp_ready_process_size; i++) {
enqueueProcess(&readyQueue, tmp_ready_process[i]);
//printf("WHAT IS RUNNING:%d\n", readyQueue.front->data->pid);
}
// at this point we feed the pre ready process into the CPU
tmp_ready_process_size = 0;
for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
if (CPU[i] == NULL) {
CPU[i] = get_next_sch_process();
}
}
}
void waiting_to_ready() {
int i = 0;
;
int waitingQueueSize = waitingQueue.size;
for (i = 0; i < waitingQueueSize; i++) {
process *ready = waitingQueue.front->data;
dequeueProcess(&waitingQueue);
if (ready->bursts[ready->currentBurst].step == ready->bursts[ready->currentBurst].length) {
ready->currentBurst++;
tmp_ready_process[tmp_ready_process_size++] = ready;
} else {
enqueueProcess(&waitingQueue, ready);
}
}
}
void refresh_processes() {
int j;
int size = waitingQueue.size;
// update waiting state I\O burst
for (j = 0; j < size; j++) {
process *I_O = waitingQueue.front->data;
dequeueProcess(&waitingQueue);
I_O->bursts[I_O->currentBurst].step++;
enqueueProcess(&waitingQueue, I_O);
}
// update CPU BOUND process
for (j = 0; j < readyQueue.size; j++) {
process *CPU_BOUND = readyQueue.front->data;
dequeueProcess(&readyQueue);
CPU_BOUND->waitingTime++;
enqueueProcess(&readyQueue, CPU_BOUND);
}
// increases work done by either I/O or CPU bound processes
for (j = 0; j < NUMBER_OF_PROCESSORS; j++) {
if (CPU[j] != NULL) {
CPU[j]->bursts[CPU[j]->currentBurst].step++;
}
}
}
int main() {
clock_t ticks;
time_t start_time, end_time;
time(&start_time);
int i;
int status = 0;
log_file = fopen("log_file.txt", "w+");
if (log_file == NULL) {
fprintf(stderr, "LOG FILE CANNOT BE OPEN\n");
}
// initialize cpus
for (i = 0; i < NUMBER_OF_PROCESSORS; i++) {
CPU[i] = NULL;
}
init_();
initializeProcessQueue(&readyQueue);
initializeProcessQueue(&waitingQueue);
// read in process and initialize process values
while ((status = (readProcess(&processes[number_of_processes])))) {
if (status == 1) {
number_of_processes++;
}
if (number_of_processes > MAX_PROCESSES || number_of_processes == 0) {
break;
}
}
//sort process by their arrival times
qsort(processes, number_of_processes, sizeof (process), compareByArrival);
// main execution loop
while (TRUE) {
ticks = clock();
incoming_process_init();
running_process_to_waiting();
most_ready_running_in_cpu();
waiting_to_ready();
refresh_processes();
cpu_utilized_time += runningProcesses();
simulation_time++;
// break when there are no more running or incoming processes, and the waiting queue is empty
if (runningProcesses() == 0 && (number_of_processes - nextProcess) == 0 && waitingQueue.size == 0) {
break;
}
}
// calculations based on CPU simulations
int total_waiting_time = 0;
int turn_around_time = 0;
for (i = 0; i < number_of_processes; i++) {
turn_around_time += processes[i].endTime - processes[i].arrivalTime;
total_waiting_time += processes[i].waitingTime;
}
printf("********************************************************************\n");
printf("Average Waiting Time\t\t\t:%.2f\n", total_waiting_time / (double) number_of_processes);
printf("Average Turn Around Time\t\t:%.2f\n", turn_around_time / (double) number_of_processes);
printf("Time all for all CPU processes\t\t:%d\n", simulation_time);
printf("CPU Utilization Time\t\t\t:%.2f%c\n", (double) (cpu_utilized_time * 100.0) / (double) (simulation_time), (int) 37);
printf("Total Number of Context Switches\t:%d\n", context_switches);
printf("Last Process to finish ");
for (i = 0; i < number_of_processes; i++) {
if (processes[i].endTime == last_process_end_time) {
printf("PID\t\t:%d\n", processes[i].pid);
}
}
//printf("%d\n" , processes[number_of_processes].pid);
printf("********************************************************************\n");
time(&end_time);
double prg_time = (end_time - start_time) * 0.001;
double cpu_time = (double) ticks / CLOCKS_PER_SEC;
fprintf(log_file, "Program Time\t:%.2fsecs\n", prg_time);
fprintf(log_file, "CPU Time\t:%.2fsecs\n", cpu_time);
fclose(log_file);
return 0;
}