-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
226 lines (161 loc) · 4.4 KB
/
kernel.c
File metadata and controls
226 lines (161 loc) · 4.4 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
#include "ram.h"
#include "pcb.h"
#include "cpu.h"
#include "kernel.h"
#include <io.h>
#include "memorymanager.h"
char *ram[40];
PCB* pcbArray[10];
int pcbCounter = 0;
typedef struct Node {
struct PCB* nPCB;
struct Node* next;
} Node;
Node *head = NULL;
Node *tail = NULL;
// initialise ram to NULL
void ini(){
for (int i = 0; i < 40; i++) {
ram[i] = NULL;
}
}
// initialize pcb array to NULL
void iniPcbArray(){
for (int i=0;i<10;i++){
pcbArray[i]=NULL;
}
}
// add element to the queue
void addToReady(struct PCB* pcb) {
// when there no element in the queue
if (head == NULL) {
Node *firstPCB = malloc(sizeof(struct Node));
firstPCB->nPCB = pcb;
firstPCB->next = NULL;
head = firstPCB;
tail = firstPCB;
} else {
Node*lastPCB = malloc(sizeof(struct Node));
lastPCB->next = tail;
lastPCB->nPCB = pcb;
tail = lastPCB;
}
pcbCounter++;
}
// remove head element from the queue
struct Node *removeHead() {
pcbCounter--;
Node*temp;
//when only one node
if (head == tail) {
temp = head;
head = NULL;
tail = NULL;
return temp;
} else {
temp = tail;
while (temp->next != head) {
temp = temp->next;
}
Node* tmpHead = head;
temp->next = NULL;
head = temp;
return tmpHead;
}
}
void scheduler(){
int isEnque=0;
while (head != NULL && tail !=NULL){// while le ready queue is not empty
Node*current_process = removeHead(); // pop the head pcb
struct PCB*currentPCB = current_process->nPCB;
CPU.offset=currentPCB->PC_offset;
CPU.IP = currentPCB->PC;
isEnque=runQuanta(2);// run process for 2 quanta
if (isEnque == -2){ // when page fault occurs
clearFrame(currentPCB,currentPCB->PC_page);
currentPCB->PC_page++;
if (currentPCB->PC_page == currentPCB->pages_max){
//clear ram et remove this pcb from queue
clearPCB(currentPCB);
}else {
if (currentPCB->pageTable[currentPCB->PC_page]!=-2){ // page is in ram pc points to a new frame
currentPCB->PC= currentPCB->pageTable[currentPCB->PC_page]*4;
currentPCB->PC_offset=0;
} else {
// find victim
int frame= findFrame();
if (findFrame()!=-1){// when there is empty frame in ram
loadPage(currentPCB->PC_page,(fopen(currentPCB->fileName,"r")),frame);
updatePageTable(currentPCB,currentPCB->PC_page,frame,-10);
} else{ // when all frame occupied--> find victim
int victim = findVictim(currentPCB);
loadPage(currentPCB->PC_page,(fopen(currentPCB->fileName,"r")),victim);
updatePageTable(currentPCB,currentPCB->PC_page,-1,victim);
}
currentPCB->PC = currentPCB->pageTable[currentPCB->PC_page]*4;
currentPCB->PC_offset=0;
}
addToReady(currentPCB);
}
}
else if(isEnque == -1){
// clear the pcb clear the ram update pcbArray
clearPCB(currentPCB);
} else {
currentPCB->PC_offset=CPU.offset;
currentPCB->PC=currentPCB->pageTable[currentPCB->PC_page]*4+currentPCB->PC_offset;
addToReady(currentPCB);
}
}
}
// clear the queue and the ram
void clearEverything() {
while (tail != NULL) {
Node*current_process = removeHead();
struct PCB *thisPCB = current_process->nPCB;
clearPCB(thisPCB);
}
}
void boot(){
// set every cell to NULL
ini();
// deletes former directory
char deleteCmd[30]= "rmdir /q /s BackingStore";
system (deleteCmd);
// creating backingStore directory
char *BackingStore = "./BackingStore";
int dir = mkdir(BackingStore);
}
int kernel(){
char prompt[100] = { '$', '\0' };
char userInput[1000];
int errorCode = 0;
printf("Kernel 1.0 loaded!\n");
printf("Welcome to the Farouk Arab Shell! \n");
printf("Version 3.0 Updated April 2020 \n");
while (1) {
printf("%s", prompt);
fgets(userInput, 999, stdin);
errorCode = parse(userInput);
// reset the user input
memset(userInput, '\0', sizeof(char) * 1000);
if (errorCode == 1) {
exit(0);
}
if (errorCode == 2) {
printf("Unknown command\n");
} else if (errorCode ==-1){
printf("there is more than 10 programs to execute!!");
}
}
return errorCode;
}
int main(void) {
int err = 0;
boot();
err = kernel();
}