-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
246 lines (198 loc) · 6.5 KB
/
Copy pathkernel.c
File metadata and controls
246 lines (198 loc) · 6.5 KB
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
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "pcb.h"
#include "kernel.h"
#include "shell.h"
#include "shellmemory.h"
#include "interpreter.h"
#include "ready_queue.h"
#define FRAME_PAGE_SIZE 3 //Each frame is defined as 3 lines long
bool active = false;
bool debug = false;
bool inBackground = false;
//Start a process given a filename that is in the same directory as the executable
int processInitialize(char *filename) {
//Open the provided file in read mode (in the same directory)
FILE* sourceFile;
sourceFile = fopen(filename, "r");
if (sourceFile == NULL) return FILE_ERROR;
//Open a target file in read + write mode in the backing store
int pid = generatePID();
char targetFileName[256];
sprintf(targetFileName, "backingStore/%s_%d", filename, pid); // Write file to backing store with filename_pid format
FILE* targetFile;
targetFile = fopen(targetFileName, "w+");
if (targetFile == NULL) return FILE_ERROR;
//Copy the provided file into the target file
char readCharacter;
int numOfLines = 0; //Count the number of lines or "instructions"
// Copy the contents of the provided file to the target file
while ((readCharacter = fgetc(sourceFile)) != EOF) {
if (readCharacter == '\n') numOfLines++; // Count the number of lines or "instructions"
fputc(readCharacter, targetFile); // Write the character to the target file
}
//Close the provided file
fclose(sourceFile);
//Pass the file pointer of the target file to shell memory to be loaded
//We get an array of allocated frames back
//REWIND THE FILE FOR READING SO YOU DONT SEG FAULT
rewind(targetFile);
// Load the first two pages of the file into the frame store, and get the allocated frames indices
int* allocatedFrames = loadFile(targetFile, numOfLines + 1); //The last line does not end with a \n, therefore +1 to num of lines
//Create a new PCB with the allocated frames and the number of lines, includes page table instantiation
PCB* newPCB = makePCB(allocatedFrames, numOfLines + 1, pid, targetFileName); //The last line does not end with a \n, therefore +1 to num of lines
QueueNode *node = malloc(sizeof(QueueNode));
node -> pcb = newPCB;
readyQueueAddToTail(node);
fclose(targetFile); //Maybe you want to keep this open for next section?
return 0;
}
// Looping through process execution
bool executeProcess(QueueNode *node, int quanta){
char *line = NULL;
PCB *pcb = node->pcb;
for(int i = 0; i < quanta; i++){
bool interrupt = false;
// Skip first incrementation of the process
if (pcb->instructionsExecuted != 0 ) {
interrupt = pcb->incrementPC(pcb);
}
// Go get the line to be executed
line = frameGetValueAtLine(pcb->PC);
// Interrupt due to page fault, process gets placed in the back of the queue
if (interrupt) {
return false;
}
pcb->instructionsExecuted++;
inBackground = true;
if(pcb->priority) {
pcb->priority = false;
}
// Terminate the process if all instructions have been executed
if(pcb->instructionsExecuted >= (pcb->numOfInstructions)){
parseInput(line);
terminateProcess(node);
inBackground = false;
return true;
}
parseInput(line);
inBackground = false;
}
return false;
}
void *schedulerFCFS() {
QueueNode *cur;
bool processComplete = true;
while (true) {
if (!processComplete) {
processComplete = executeProcess(cur, MAX_INT);
continue;
}
if (isReadyEmpty()) {
if (active) continue;
else ;break;
}
cur = readyQueuePopHead();
processComplete = executeProcess(cur, MAX_INT);
}
return 0;
}
void *schedulerSJF() {
QueueNode *cur;
while (true) {
if (isReadyEmpty()) {
if (active) continue;
else break;
}
cur = readyQueuePopShortestJob();
executeProcess(cur, MAX_INT);
}
return 0;
}
void *schedulerAGINGAlternative() {
QueueNode *cur;
while (true) {
if (isReadyEmpty()) {
if (active) continue;
else break;
}
cur = readyQueuePopShortestJob();
readyQueueDecrementJobLengthScore();
if (!executeProcess(cur, 1)) {
readyQueueAddToHead(cur);
}
}
return 0;
}
void *schedulerAGING() {
QueueNode *cur;
int shortest;
sortReadyQueue();
while (true) {
if (isReadyEmpty()) {
if (active) continue;
else break;
}
cur = readyQueuePopHead();
shortest = readyQueueGetShortestJobScore();
if (shortest < cur -> pcb -> jobLengthScore) {
readyQueuePromote(shortest);
readyQueueAddToTail(cur);
cur = readyQueuePopHead();
}
readyQueueDecrementJobLengthScore();
if (!executeProcess(cur, 1)) {
readyQueueAddToHead(cur);
}
}
return 0;
}
void *schedulerRR(void *arg) {
int quanta = ((int *) arg)[0];
QueueNode *cur;
while (true) {
// if head is null
if (isReadyEmpty()) {
// if process is active continue
if (active) continue;
else break;
}
// get the head of the queue
cur = readyQueuePopHead();
//If execute process is false, put the process at the end of the queue
//This means a page fault happened, and we should continue rerunning it
if (!executeProcess(cur, quanta)) {
readyQueueAddToTail(cur);
}
}
return 0;
}
int scheduleByPolicy(char* policy){
if (strcmp(policy, "FCFS") != 0 &&
strcmp(policy, "SJF") != 0 &&
strcmp(policy, "RR") != 0 &&
strcmp(policy, "AGING") != 0 &&
strcmp(policy, "RR30") != 0) {
return SCHEDULING_ERROR;
}
if (active) return 0;
if (inBackground) return 0;
int arg[1];
if (strcmp("FCFS", policy) == 0) {
schedulerFCFS();
} else if (strcmp("SJF", policy) == 0) {
schedulerSJF();
} else if (strcmp("RR", policy) == 0) {
arg[0] = 2;
schedulerRR((void *) arg);
} else if (strcmp("AGING", policy) == 0) {
schedulerAGING();
} else if (strcmp("RR30", policy) == 0) {
arg[0] = 30;
schedulerRR((void *) arg);
}
return 0;
}