-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCB.java
More file actions
117 lines (101 loc) · 3.86 KB
/
PCB.java
File metadata and controls
117 lines (101 loc) · 3.86 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
import java.util.Hashtable;
/************************************************************
* A Process Control Block (PCB) data structure for a process.
* @author Gloire Rubambiza
* @since 11/12/2017
************************************************************/
public class PCB {
/** The PID for this process. */
private int pid;
/** The total number of memory references for this process. */
private int memRef;
/** The total number of page faults for this process. */
private int pageFaults;
/** The page table for this process. */
private Hashtable<Integer, Integer> pageTable;
/** The initial capacity of the page table. */
private int capacity = 64;
/*************************************************
* Instantiates a PCB for a process with (minimal)
* useful information
* The process will have 64 pages of 1KB each
* @param pid the process ID
* @param table the page table for this process
***********************************************/
public PCB (int PID) {
this.pid = PID;
this.memRef = 0;
this.pageFaults = 0;
this.pageTable = new Hashtable<Integer, Integer>(capacity);
}
/************************************************************
* Updates page table of the given process with new entries.
* @param inTable tells where the page is being added/replaced
* @param pid is the PID of the process.
* @param page is the page to be added/replaced in the table.
*************************************************************/
public void updateTable(boolean inTable, int page, int frame) {
if ( !(inTable) ) { // Adding for the first time
this.pageTable.put(page,frame);
} else { // Removing it from the table
this.pageTable.remove(page,frame);
}
}
/***********************************************
* Updates the reference count for the process.
***********************************************/
public void updateRefCount () {
this.memRef++;
}
/***********************************************
* Updates the page fault count for the process.
***********************************************/
public void updateFaults () {
this.pageFaults++;
}
/*********************************************
* Prints the current state of the page table.
* Code help on enumerating over a map obtained from:
* https://stackoverflow.com/questions/2216311/how-do-i-enumerate-the-keys-and
* -values-of-a-hashtable
*********************************************/
public void printTable() {
System.out.println("Page Frame");
for ( Integer key : this.pageTable.keySet() ) {
System.out.println(key + " -> " + this.pageTable.get(key) );
}
}
/**********************************************************
* Reports the total number of references for this process.
**********************************************************/
public int getTotalReferences () {
return this.memRef;
}
/**********************************************************
* Reports the total number of page faults for this process.
**********************************************************/
public int getTotalPageFaults () {
return this.pageFaults;
}
/************************************************
* Reports the size of the PCB's page table
* at the completion of each memory reference run.
* @return the size of the page table currently.
************************************************/
public int getPageSize () {
return this.pageTable.size();
}
/***********************************
* Reports the PID for the process.
**********************************/
public int getPID () {
return this.pid;
}
/****************************************
* Points the pcb table to its page table.
* @return the page table for the process.
*****************************************/
public Hashtable <Integer, Integer> getTable () {
return this.pageTable;
}
}