-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorymanager.c
157 lines (149 loc) · 4.29 KB
/
memorymanager.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"pcb.h"
#include"ram.h"
#include"kernel.h"
#include"memorymanager.h"
#include"interpreter.h"
char *ram[40];
int FILENAME=0;
int countTotalPages(FILE *f){
rewind(f);
int num=0;
char c=fgetc(f);
while(c!=EOF){
if(c=='\n') num++;
c=fgetc(f);
}
num++;//EOF also presents a line;
rewind(f);
if(detail()) printf("-> %d lines found\n", num);
int total;
if(num%4>0) total=((int) num/4)+1;
else total=(int) num/4;
if(detail()) printf("-> The file has %d pages\n", total);
return total;
}
void loadPage(int pageNumber, FILE *f, int frameNumber){
if(detail()) printf("-> loading file from pageNumber %d into frame %d\n", pageNumber, frameNumber);
rewind(f);
char buffer[100];
for(int i=0; i<pageNumber*4; i++){
if(fgets(buffer, 100, f)==NULL) {
printf("-> Error: pageNumber out of range\n");
return;
}
}
for(int i=0; i<4; i++){
if(fgets(buffer, 100, f)!=NULL) ram[frameNumber*4+i]=strdup(buffer);
else ram[frameNumber*4+i]=NULL;
}
if(detail()) printf("-> loaded successfully\n");
//if(detail()) printFrame(frameNumber);
}
int findFrame(){
for(int i=0; i<40; i+=4){
if(ram[i]==NULL) return i/4;
}
return -1;
}
int findVictim(struct PCB *pcb){
int r=rand()%10;
if(pcb->pageTable[r]==-1) return r;
else{
while((pcb->pageTable[r]!=-1) && r<=10) {
r++;
if(r>10) r=1;
}
return r;
}
}
int updatePageTable(struct PCB *p, int pageNumber, int frameNumber, int victimFrame){
if(detail()) printf("-> updating page table\n");
if(frameNumber<0) {
if(detail()) printf("-> setting pageNumber %d to VictimFrame %d\n", pageNumber, victimFrame);
p->pageTable[pageNumber]=victimFrame;
updateVictimPCB(victimFrame);
}
else{
if(detail()) printf("-> setting pageNumber %d to new frame %d\n", pageNumber, frameNumber);
p->pageTable[pageNumber]=frameNumber;
}
return 0;
}
int fileCopier(FILE *f, FILE *target){
char **tokens;
char buffer[100];
while(!feof(f)){
if(fgets(buffer, 100, f)==NULL) break;
if(buffer[0]=='\n') continue;
int len=strlen(buffer);
if(buffer[len-1]!='\n' && len<100){
buffer[len]='\n';
buffer[len+1]='\0';
}
fputs(buffer, target);
tokens=tokenize(buffer);
if(strcmp(tokens[0], "run")==0 && tokens[1]!=NULL && tokens[2]==NULL) {
//we will load the file of run command into the new file
FILE *new=fopen(tokens[1], "r");
fileCopier(new, target);
fclose(new);
}
}
free(tokens);
return 0;
}
//this will handle run command and copy the run file into the new file togather by recursion
int launcher(FILE *p){
if(p==NULL) {
printf("Error: File does not exist\n");
return -1;
}
if(detail()) printf("-> Calling launcher\n");
char tmp[4];
sprintf(tmp, "%d", FILENAME);
char *filepath=malloc(20);
filepath = strcpy(filepath, "BackingStore/");
filepath = strcat(filepath, tmp); FILENAME++;
//create a new file with new name
FILE *f=fopen(filepath, "w+");
if(detail()) printf("-> New file created\n");
fileCopier(p, f);
fclose(p);
if(detail()) printf("-> Old file closed\n");
//copy and close the original file
fclose(f);
f=fopen(filepath, "r");
free(filepath);
struct PCB *pcb;
int length=countTotalPages(f);
if(length<2) {
pcb=makePCB(1, f);
int targetFrame=findFrame();
if(targetFrame==-1) {
targetFrame=findVictim(pcb);
}
loadPage(0, f, targetFrame);
pcb->pageTable[0]=targetFrame;
}
else {
pcb=makePCB(length,f);
int targetFrame=findFrame();
if(targetFrame==-1) {
targetFrame=findVictim(pcb);
}
loadPage(0, f, targetFrame);
pcb->pageTable[0]=targetFrame;
targetFrame=findFrame();
if(targetFrame==-1) {
targetFrame=findVictim(pcb);
}
loadPage(1, f, targetFrame);
pcb->pageTable[1]=targetFrame;
}
pcb->PC=pcb->pageTable[pcb->PC_page]*4;
addToReady(pcb);
return 0;
}