-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcb.c
More file actions
142 lines (131 loc) · 3.96 KB
/
pcb.c
File metadata and controls
142 lines (131 loc) · 3.96 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"ram.h"
#include"cpu.h"
#include"pcb.h"
#include"kernel.h"
struct PCBList *readyList;
struct PCB *makePCB(int pageNumber, FILE *f){
if(detail()) printf("Calling function makePCB in pcb.c\n");
struct PCB *new = malloc(sizeof(struct PCB));
new->PC=0;
new->PC_offset=0;
new->PC_page=0;
new->pages_max=pageNumber;
new->file=f;
for(int i=0; i<10; i++) new->pageTable[i]=-1;//-1 for not used
if(detail()) printf("-> New PCB created with PC: %d\n", new->PC);
return new;
}
int ReadyLength(){
if(readyList==NULL) return 0;
if(readyList->head==NULL) return 0;
int i=0;
struct PCBNode *cur = readyList->head;
while(cur!=NULL){
cur = cur->next;
i++;
}
return i;
}
void printPageTable(struct PCB *pcb){
printf("-> current PCB page table is :\n");
for(int i=0; i<pcb->pages_max; i++){
printf("Page: %d ---> Frame: %d\n", i, pcb->pageTable[i]);
}
}
void terminatePCB(struct PCB *pcb){
if(detail()) printf("-> Terminating current PCB\n");
for(int i=0; i<10; i++) {
if(pcb->pageTable[i]>=0) unloadFrame(pcb->pageTable[i]);
}
fclose(pcb->file);
free(pcb);
if(detail()) printf("-> Terminated\n");
}
void updateVictimPCB(int frameNumber){
struct PCBNode *cur = readyList->head;
while(cur!=NULL){
for(int i=0; i<cur->this->pages_max; i++){
if(cur->this->pageTable[i]==frameNumber) {//now page i is to be deleted
cur->this->pageTable[i]=-1;
return;
}
}
cur=cur->next;
}//loop through the readyList to find the victim PCB
for(int i=0; i<cpu->cur_PCB->pages_max; i++){
if(cpu->cur_PCB->pageTable[i]==frameNumber) {
cpu->cur_PCB->pageTable[i]=-1;
return;
}
}//see if the PCB in the cpu is the victim (unlikely to happen)
}
void addToReady(struct PCB *pcb){
if(detail()) printf("Calling function addToReady in pcb.c\n");
if(readyList==NULL) {
if(detail()) printf("-> Initializing ReadyList\n");
readyList=malloc(sizeof(struct PCBList));
readyList->head=NULL;
readyList->tail=NULL;
}
struct PCBNode *new = malloc(sizeof(struct PCBNode));
new->this=pcb;
new->next=NULL;
if(detail()) printf("-> Adding new PCB to ReadyList\n");
if(readyList->head==NULL) {
readyList->head=new;
readyList->tail=new;
}
else{
readyList->tail->next=new;
readyList->tail=new;
}
if(detail()) printf("-> %d PCBs in ReadyList currently\n", ReadyLength());
}
void assign_head_to_cpu(){
if(detail()) printf("Calling function assign_head_to_cpu in pcb.c\n");
if(readyList->head==NULL) {
printf("Error: empty readylist\n");
return;
}
cpu->cur_PCB=readyList->head->this;
if(readyList->head==readyList->tail){
free(readyList->head);
readyList->head=NULL;
readyList->tail=NULL;
}
else{
struct PCBNode *newhead=readyList->head->next;
free(readyList->head);
readyList->head=newhead;
}
if(detail()) {
printf("-> Head of the ReadyList is removed and assigned to CPU\n");
printf("-> %d PCBs in ReadyList currently\n", ReadyLength());
}
}
void from_cpu_to_tail(){
if(detail()) printf("Calling function from_cpu_to_tail in pcb.c\n");
if(readyList->head==NULL) {
addToReady(cpu->cur_PCB);
}
else{
struct PCBNode *tmp = malloc(sizeof(struct PCBNode));
tmp->this=cpu->cur_PCB;
tmp->next=NULL;
readyList->tail->next=tmp;
readyList->tail=tmp;
}
cpu->cur_PCB=NULL;
if(detail()) printf("-> PCB in the cpu assign back to tail of readyList\n");
}
int is_empty(){
if(readyList->head==NULL && cpu->cur_PCB==NULL) {
if(detail()) printf("-> The readyList and CPU's PCB is empty\n");
return 1;
}
else return 0;
}
void destoryReady() {free(readyList);}