-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCB.java
More file actions
146 lines (136 loc) · 4.3 KB
/
PCB.java
File metadata and controls
146 lines (136 loc) · 4.3 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
import java.util.*;
public class PCB
{
static List<Process> entries = new ArrayList<Process>();
static int numDone;
static
{
numDone=0;
}
static int addNewEntry(InputEntry entry)
{
if (SystemConfig.max_proc<=entries.size())
{
System.out.println("Adding Failed, max_proc " + SystemConfig.max_proc + " current_size : " + entries.size());
return -1;
}
else
{
Process proc = new Process(entry);
entries.add(proc);
entries.get(entries.size()-1).tableIndex = entries.size()-1;
System.out.println("Before adding to queue");
proc.printDetails();
addtoQueue(proc.tableIndex,0);
proc.printDetails();
return 0;
}
}
static void addtoQueue(int index, int type)
{
System.out.println("In addtoQueue for index " + index + " type " + type);
// there are 2 possibilities here
/*
the thing finished it's current specification and needs to move to the next specification
the thing hasnt finished it's current specification
*/
/* checking if it finished it's current specification */
Process current = entries.get(index);
if (current.specifications.getFirst().type==0) // this means it's in CPU
{
CpuSpec currcpu = (CpuSpec) current.specifications.getFirst();
/* checking if the current specification is done */
if (current.current_exec>=currcpu.time_req)
{
current.current_exec=0;
current.specifications.remove(0);
MemQueue.updateCurrentFreeMem(entries.get(index).curr_mem,1);
/* check if there is any I/O required */
int check=entries.get(index).checkforIO();
if (check!=0 && current.specifications.size()!=0)
{
System.out.println("There was no IO required for index " + index);
// we need to schedule the thing, so we check for memory
CpuSpec current_spec = (CpuSpec)entries.get(index).specifications.element();
if (MemQueue.current_free_memory>=current_spec.mem_req)
{
System.out.println("There was free memory, so we put it in Ready queue");
// we add to Ready queue
entries.get(index).state=3;
Ready.enqueue(entries.get(index));
}
else
{
System.out.println("There was no free memory, so we put it in memory queue");
// we add to memory waiting
entries.get(index).state=2;
MemQueue.enqueue(entries.get(index));
}
}
}
/* if the current specification isnt done */
else
{
Ready.enqueue(current);
}
}
else if (current.specifications.getFirst().type==1) // this means it's in I/O
{
IOSpec currio= (IOSpec) current.specifications.getFirst();
/* checking if the current specification is done */
if (current.current_exec>=currio.time_req)
{
current.current_exec=0;
current.specifications.remove(0);
MemQueue.updateCurrentFreeMem(entries.get(index).curr_mem,1);
/* check if there is any I/O required */
int check=entries.get(index).checkforIO();
if (check!=0 && current.specifications.size()!=0)
{
System.out.println("There was no IO required for index " + index);
// we need to schedule the thing, so we check for memory
CpuSpec current_spec = (CpuSpec)entries.get(index).specifications.element();
if (MemQueue.current_free_memory>=current_spec.mem_req)
{
System.out.println("There was free memory, so we put it in Ready queue");
// we add to Ready queue
entries.get(index).state=3;
Ready.enqueue(entries.get(index));
}
else
{
System.out.println("There was no free memory, so we put it in memory queue");
// we add to memory waiting
entries.get(index).state=2;
MemQueue.enqueue(entries.get(index));
}
}
}
}
}
static void removeDoneProcesses()
{
if(entries.size()==0)
return;
System.out.println("Checking for done processes and removing");
for (Iterator<Process> iterator = entries.iterator(); iterator.hasNext();)
{
Process proc = iterator.next();
if (proc.state==-1)
{
numDone+=1;
iterator.remove();
System.out.println("Process Removed : Process entries size = "+entries.size());
System.out.println("Number of procs complete : " + numDone);
}
for(Process proc1:entries)
proc1.tableIndex=entries.indexOf(proc1);
}
}
static void printProcessTable()
{
for(Process proc:entries){
proc.printDetailsforProcTable();
}
}
}