-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernelsim.c
292 lines (232 loc) · 8.45 KB
/
kernelsim.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#define LINESIZE 1024
#define NUMLINES 20
typedef struct pcb {
int pid;
int arrivalTime;
int totalCPUTime;
int ioFreq;
int ioDur;
int timeRemain; //keeps track of the remaining time of the cpu
int ioRemain; //keeps track of the remaining time of I/O
int ioFreqCounter; //keeps track of the time for the next I/O
int oldState;
int currentState;
struct pcb *next;
} pcb_t;
typedef struct {
pcb_t *front; // Points to the node at the head of the queue's linked list.
pcb_t *rear; // Points to the node at the tail of the queue's linked list.
int size; // The # of nodes in the queue's linked list.
} queue_t;
pcb_t *pcb_construct(int processInfo[10]){
pcb_t* process = malloc(sizeof(pcb_t));
process->pid = processInfo[0];
process->arrivalTime = processInfo[1];
process->totalCPUTime= processInfo[2];
process->ioFreq = processInfo[3];
process->ioDur = processInfo[4];
process->timeRemain = processInfo[5];
process->ioRemain = processInfo[6];
process->ioFreqCounter = processInfo[7];
process->oldState= processInfo[8];
process->currentState= processInfo[9];
}
queue_t *queue_construct(void)
{
queue_t *queue = malloc(sizeof(queue_t));
// assert(queue != NULL);
queue->front = NULL;
queue->rear = NULL;
queue->size = 0;
return queue;
}
/* adds an element to the end of the queue */
void enqueue(queue_t *queue, pcb_t* p)
{
//assert(queue != NULL);
if (queue->front == NULL) {
queue->front = p;
} else {
queue->rear->next = p;
}
queue->rear = p;
queue->size += 1;
}
/* removes an element from the front of the queue */
pcb_t* dequeue(queue_t *queue)
{
// assert(queue != NULL);
int process[10];
process[0]= queue->front->pid;
process[1]= queue->front->arrivalTime;
process[2]= queue->front->totalCPUTime;
process[3]= queue->front->ioFreq;
process[4]= queue->front->ioDur;
process[5]= queue->front->timeRemain;
process[6]= queue->front->ioRemain;
process[7]= queue->front->ioFreqCounter;
process[8]= queue->front->oldState;
process[9]= queue->front->currentState;
pcb_t *node_to_delete = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
queue->size -= 1;
pcb_t*node_deleted =pcb_construct(process);
return node_deleted;
}
/* Loads the values of the test file into an array of lines */
int readFile (FILE* inputf, char file[NUMLINES][LINESIZE]){
int i =0;
while(fgets(file[i], LINESIZE, inputf))
{
file[i][strlen(file[i]) - 1] = '\0';
i++;
}
return i;
}
/* Loads the values into the array of line to an array of processes */
void initializeProcesses(int processes[][10], char file[NUMLINES][LINESIZE], int numCommands){
printf("number of commands %d\n", numCommands);
for (int i=0; i<numCommands; i++)
{
pcb_t* process = malloc(sizeof(pcb_t));
sscanf(file[i] , "%d %d %d %d %d", &processes[i][0], &processes[i][1], &processes[i][2], &processes[i][3], &processes[i][4]);
processes[i][5] = processes[i][2];
processes[i][6] = processes[i][4];
processes[i][7]=0;
processes[i][8]=0;
processes[i][9] = 0;
}
}
/* adds all the processes from the array to the new queue */
void addProcessestoNew(queue_t* new_queue, int processes[][10], char file[NUMLINES][LINESIZE], int numCommands){
for (int i=0; i<numCommands; i++)
{
pcb_t* process = pcb_construct(processes[i]);
enqueue(new_queue,process);
}
}
/* Sorts the processes based on the time they arrive at */
void sortProcesses(int processes[][10], int numCommands){
for (int i=0; i<numCommands-1; i++)
{
for (int j=i+1; j<numCommands; j++){
if (processes[i][1] > processes[j][1]){
for (int k=0; k<10;k++){
int temp = processes[i][k];
processes[i][k] = processes[j][k];
processes[j][k] = temp;
}
}
}
}
}
/* Printing the current state of the processes and saving it in a file */
void displayOutput(int clock, int pid, char state1[], char state2[], FILE* outputf){
printf("%3d %10d %10s %10s\n",clock,pid,state1, state2);
fprintf(outputf, "%3d %10d %10s %10s\n\n",clock,pid,state1, state2);
}
int main(int argc,char *argv[])
{
char file[NUMLINES][LINESIZE];
FILE *inputf = NULL;
FILE *outputf = NULL;
inputf = fopen(argv[1], "r");
outputf = fopen(argv[2], "w");
int numCommands = readFile(inputf, file);
queue_t* new_queue = queue_construct();
queue_t* ready_queue = queue_construct();
queue_t* waiting_queue = queue_construct();
int processes[numCommands][10];
initializeProcesses(processes, file, numCommands);
sortProcesses(processes,numCommands);
addProcessestoNew(new_queue, processes,file, numCommands);
int clock = 0; //the main clock for the system
int cpuBusy = 0; //makes sure only one cpu is running at a given time
//header of the file
printf("%3s %8s %12s %10s\n","TIME","PID", "PREVIOUS", "CURRENT");
fprintf(outputf, "%3s %8s %12s %10s\n\n","TIME","PID", "PREVIOUS", "CURRENT");
pcb_t* newProcess;
pcb_t* runningProcess;
pcb_t* waitingProcess;
int terminated = 0;
int waiting = 0;
int deleted = 0;
while (terminated< numCommands){
//goes from new to ready
if (new_queue->size>0 && clock >= new_queue->front->arrivalTime){
newProcess = dequeue(new_queue);
newProcess->oldState = 0;
newProcess->currentState = 1;
enqueue(ready_queue, newProcess);
displayOutput(clock,newProcess->pid, "NEW", "READY",outputf);
}
//goes from ready to running
if (cpuBusy==0){
if (ready_queue->size >0){
//goes from ready to running
runningProcess = dequeue(ready_queue);
runningProcess->oldState = 1;
runningProcess->currentState = 2;
displayOutput(clock,runningProcess->pid, "READY", "RUNNING",outputf);
cpuBusy = 1;
}
}
//process is waiting
if (waiting_queue->size>0 && waiting==0){
waitingProcess = dequeue(waiting_queue);
waiting = 1;
}
if (waiting==1){
waitingProcess->ioRemain-=1;
if (waitingProcess->ioRemain==0){
waiting = 0;
waitingProcess->oldState = 3;
waitingProcess->currentState = 2;
waitingProcess->ioRemain = waitingProcess->ioDur;
enqueue(ready_queue,waitingProcess);
displayOutput(clock,waitingProcess->pid, "WAITING", "READY",outputf);
}
}
//process is running
if (cpuBusy==1){
runningProcess->ioFreqCounter+=1;
//process is terminated
if (runningProcess->timeRemain == 0){
cpuBusy = 0;
runningProcess->oldState = 2;
runningProcess->currentState = 4;
displayOutput(clock,runningProcess->pid, "RUNNING", "TERMINATED",outputf);
deleted = 1;
terminated+=1;
}
//goes to waiting
else if (runningProcess->ioFreqCounter == runningProcess->ioFreq+1){
cpuBusy = 0;
runningProcess->oldState = 2;
runningProcess->currentState = 3;
runningProcess->ioFreqCounter = 0;
enqueue(waiting_queue, runningProcess);
displayOutput(clock,runningProcess->pid, "RUNNING", "WAITING",outputf);
}
else{
runningProcess->timeRemain-=1;
}
if (deleted ==1){
free(runningProcess);
deleted = 0;
}
}
clock++;
}
fclose(inputf);
fclose(outputf);
return 0;
}