-
Notifications
You must be signed in to change notification settings - Fork 2
/
Scheduler_fil.java
185 lines (164 loc) · 6.08 KB
/
Scheduler_fil.java
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
import java.util.*;
public class Scheduler extends Thread
{
private Vector queue;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 1000;
// New data added to p161
private boolean[] tids; // Indicate which ids have been used
private static final int DEFAULT_MAX_THREADS = 10000;
// A new feature added to p161
// Allocate an ID array, each element indicating if that id has been used
private int nextId = 0;
private void initTid( int maxThreads ) {
tids = new boolean[maxThreads];
for ( int i = 0; i < maxThreads; i++ )
tids[i] = false;
}
// A new feature added to p161
// Search an available thread ID and provide a new thread with this ID
private int getNewTid( ) {
for ( int i = 0; i < tids.length; i++ ) {
int tentative = ( nextId + i ) % tids.length;
if ( tids[tentative] == false ) {
tids[tentative] = true;
nextId = ( tentative + 1 ) % tids.length;
return tentative;
}
}
return -1;
}
// A new feature added to p161
// Return the thread ID and set the corresponding tids element to be unused
private boolean returnTid( int tid ) {
if ( tid >= 0 && tid < tids.length && tids[tid] == true ) {
tids[tid] = false;
return true;
}
return false;
}
// A new feature added to p161
// Retrieve the current thread's TCB from the queue
public TCB getMyTcb( ) {
Thread myThread = Thread.currentThread( ); // Get my thread object
synchronized( queue ) {
for ( int i = 0; i < queue.size( ); i++ ) {
TCB tcb = ( TCB )queue.elementAt( i );
Thread thread = tcb.getThread( );
if ( thread == myThread ) // if this is my TCB, return it
return tcb;
}
}
return null;
}
// A new feature added to p161
// Return the maximal number of threads to be spawned in the system
public int getMaxThreads( ) {
return tids.length;
}
public Scheduler( ) {
timeSlice = DEFAULT_TIME_SLICE;
queue = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}
public Scheduler( int quantum ) {
timeSlice = quantum;
queue = new Vector( );
initTid( DEFAULT_MAX_THREADS );
}
// A new feature added to p161
// A constructor to receive the max number of threads to be spawned
public Scheduler( int quantum, int maxThreads ) {
timeSlice = quantum;
queue = new Vector( );
initTid( maxThreads );
}
private void schedulerSleep( ) {
try {
Thread.sleep( timeSlice );
} catch ( InterruptedException e ) { }
}
// A modified addThread of p161 example
public TCB addThread( Thread t ) {
t.setPriority( 2 );
TCB parentTcb = getMyTcb( ); // get my TCB and find my TID
int pid = ( parentTcb != null ) ? parentTcb.getTid( ) : -1;
int tid = getNewTid( ); // get a new TID
if ( tid == -1)
return null;
TCB tcb = new TCB( t, tid, pid ); // create a new TCB
// the following if and for statements are for file system.
if ( parentTcb != null ) {
for ( int i = 0; i < 32; i++ ) {
tcb.ftEnt[i] = parentTcb.ftEnt[i];
// JFM added 2012-12-01
// increment the count for any file table entries inherited from parent
if ( tcb.ftEnt[i] != null )
tcb.ftEnt[i].count++;
}
}
queue.add( tcb );
return tcb;
}
// A new feature added to p161
// Removing the TCB of a terminating thread
public boolean deleteThread( ) {
TCB tcb = getMyTcb( );
if ( tcb == null )
return false;
else {
// JFM added 2012-12-01
// if any file table entries are still open, decrement their count
for ( int i = 3; i < 32; i++ )
if ( tcb.ftEnt[i] != null )
// JFM changed 2012-12-13
// close any open file descriptors rather than decrement the counts
// to ensure that system-wide file table entries are removed
// when no longer needed
SysLib.close( i );
return tcb.setTerminated( );
}
}
public void sleepThread( int milliseconds ) {
try {
sleep( milliseconds );
} catch ( InterruptedException e ) { }
}
// A modified run of p161
public void run( ) {
Thread current = null;
this.setPriority( 6 );
while ( true ) {
try {
// get the next TCB and its thrad
if ( queue.size( ) == 0 )
continue;
TCB currentTCB = (TCB)queue.firstElement( );
if ( currentTCB.getTerminated( ) == true ) {
queue.remove( currentTCB );
returnTid( currentTCB.getTid( ) );
continue;
}
current = currentTCB.getThread( );
if ( current != null ) {
if ( current.isAlive( ) )
current.setPriority( 4 );
else {
// Spawn must be controlled by Scheduler
// Scheduler must start a new thread
current.start( );
current.setPriority( 4 );
}
}
schedulerSleep( );
// System.out.println("* * * Context Switch * * * ");
synchronized ( queue ) {
if ( current != null && current.isAlive( ) )
current.setPriority( 2 );
queue.remove( currentTCB ); // rotate this TCB to the end
queue.add( currentTCB );
}
} catch ( NullPointerException e3 ) { };
}
}
}