-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.c
190 lines (161 loc) · 4.92 KB
/
kernel.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
//#include "interpreter.h"
#include "shellmemory.h"
#include "shell.h"
#include "pcb.h"
#include "ram.h"
#include "cpu.h"
#include "memorymanager.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
struct ReadyQueue
{
struct PCB *head;
struct PCB *tail;
} readyQueue;
void roundRobin();
void terminate();
void addToReady(struct PCB *pcb);
void boot();
int kernel();
int main(int argc, const char *argv[])
{
int error = 0;
boot();
error = kernel();
return error;
}
void boot()
{
// initialize array to NULL
for (int i = 0; i < 40; i++)
{
ram[i] = NULL;
}
// prepare the Backing Store
system("rm -rf BackingStore"); // you might get error saying that it doesn't exist. this is fine. everything will work.
system("mkdir BackingStore");
}
int kernel()
{
return shellUI();
}
struct PCB *myinit(char *filename)
{
int start = 0, end = 0; // dummy values, they'll get overwritten in addToRAM
char buffer[100]; //assumes max of 100 char per line
FILE *file = fopen(filename, "r");
if (file == NULL) // this SHOULD never fail since it's been guaranteed to exist in exec()
{
printf("Error opening the file from kernel.c \n");
return NULL;
} else
{
//addToRAM(file, &start, &end); // we're passing pointers so they'll be properly updated in addToRAM
fclose(file); // no longer needed
struct PCB *pcb = makePCB(start, end);
addToReady(pcb);
return pcb;
}
}
void addToReady(struct PCB *pcb)
{
if (readyQueue.head == NULL)
{
readyQueue.head = pcb;
return; // we don't assign value to tail
} else
{
struct PCB **traverser = &(readyQueue.head)->next;
while (*traverser)
{
traverser = &(*traverser)->next;
}
*traverser = pcb;
readyQueue.tail = *traverser;
}
}
void scheduler()
{
int pid = readyQueue.head->PID;
CPU.IP = (readyQueue.head)->pageTable[readyQueue.head->PC_page]; // CPU.IP holds the frame #
if (CPU.IP ==
-1) // then it's been victimized and we need to re-load the proper page, we will only load 1 page, not 2
{
// puts("@@@@@ we're fixing the victimized pcb @@@@@");
char str[40];
sprintf(str, "./BackingStore/%d.txt", readyQueue.head->PID);
FILE *fileInBackingStore = fopen(str, "r");
// we know that since it's been victimized, findFrame() will always return -1
// then we can right away execute the code from memorymanager.c to handle victim frames
int victimFrame = findVictim(readyQueue.head);
loadPage(readyQueue.head->PC_page, fileInBackingStore, victimFrame);
readyQueue.head->pageTable[readyQueue.head->PC_page] = victimFrame;
updatePageTable(readyQueue.head, readyQueue.head->PC_page, victimFrame, victimFrame);
readyQueue.head->PC_page++;
CPU.IP = (readyQueue.head)->pageTable[readyQueue.head->PC_page - 1]; // CPU.IP holds the frame #
}
CPU.offset = (readyQueue.head)->PC_offset;
int condition = run(2);
if (condition == 1)
{
terminate();
return;
}
if (condition == 2)
{ // this is when we changed pages in run, so we don't want to increment PC and PC_offset
roundRobin();
return;
}
// ELSE
readyQueue.head->PC += 2;
readyQueue.head->PC_offset += 2;
roundRobin();
return;
}
// this will always do the same, i.e. place head in the back of queue and move other programs up
void roundRobin()
{
struct PCB *tmp = readyQueue.head;
if (tmp->next == NULL)
{
scheduler(); // i.e. only one program in ready queue
} else if (tmp->next == readyQueue.tail) // i.e. only two programs in ready queue
{
readyQueue.head = readyQueue.tail;
readyQueue.tail = tmp;
readyQueue.head->next = readyQueue.tail;
readyQueue.tail->next = NULL;
scheduler();
} else
{
readyQueue.head = tmp->next;
tmp->next = NULL;
(readyQueue.tail)->next = tmp;
readyQueue.tail = tmp;
scheduler();
}
}
// this will terminate the program, this will always be called when the head of readyQueue has finished
void terminate()
{
if ((readyQueue.head)->next == NULL) // i.e. only one program left
{
int pid = readyQueue.head->PID;
char str[40];
sprintf(str, "rm BackingStore/%d.txt", pid);
system(str); // removes the file when done executing
free(readyQueue.head);
readyQueue.head = NULL;
return; // doesnt call scheduler, returns back to interpreter();
}
int pid = readyQueue.head->PID;
char str[40];
//printf("the PID that we're removing is : %d \n", pid);
sprintf(str, "rm BackingStore/%d.txt", pid);
system(str); // removes the file when done executing
struct PCB *tmp = (readyQueue.head)->next;
free(readyQueue.head);
readyQueue.head = tmp;
scheduler();
}