-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler_pri.java
More file actions
164 lines (145 loc) · 4.43 KB
/
Scheduler_pri.java
File metadata and controls
164 lines (145 loc) · 4.43 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.*; // Scheduler_pri.java
public class Scheduler extends Thread
{
private Vector<TCB> queue;
private int timeSlice;
private static final int DEFAULT_TIME_SLICE = 1000;
// New data added to the original algorithm
private boolean[] tids; // Indicate which ids have been used
private static final int DEFAULT_MAX_THREADS = 10000;
// A new feature added to the original algorithm
// 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 the original algorithm
// 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 the original algorithm
// 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 the original algorithm
// 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 the original algorithm
// 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<TCB>( );
initTid( DEFAULT_MAX_THREADS );
}
public Scheduler( int quantum ) {
timeSlice = quantum;
queue = new Vector<TCB>( );
initTid( DEFAULT_MAX_THREADS );
}
// A new feature added to the original algorithm
// A constructor to receive the max number of threads to be spawned
public Scheduler( int quantum, int maxThreads ) {
timeSlice = quantum;
queue = new Vector<TCB>( );
initTid( maxThreads );
}
private void schedulerSleep( ) {
try {
Thread.sleep( timeSlice );
} catch ( InterruptedException e ) {
}
}
// A modified addThread of the original algorithm
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
queue.add( tcb );
return tcb;
}
// A new feature added to the original algorithm
// Removing the TCB of a terminating thread
public boolean deleteThread( ) {
TCB tcb = getMyTcb( );
if ( tcb!= null ) {
return tcb.setTerminated( );
} else
return false;
}
public void sleepThread( int milliseconds ) {
try {
sleep( milliseconds );
} catch ( InterruptedException e ) { }
}
// A modified run of the original algorithm
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 ) { };
}
}
}