-
Notifications
You must be signed in to change notification settings - Fork 2
/
Disk.java
169 lines (148 loc) · 4.03 KB
/
Disk.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
import java.io.*;
public class Disk extends Thread {
public static final int blockSize = 512;
private final int trackSize = 10;
private final int transferTime = 20;
private final int delayPerTrack = 1;
private int diskSize;
private byte data[];
private int command;
private final int IDLE = 0;
private final int READ = 1;
private final int WRITE = 2;
private final int SYNC = 3;
private boolean readyBuffer;
private byte[] buffer;
private int currentBlockId;
private int targetBlockId;
private final static int COND_DISK_REQ = 1; // wait condition
private final static int COND_DISK_FIN = 2; // wait condition
public Disk( int totalBlocks ) {
diskSize = ( totalBlocks > 0 ) ? totalBlocks : 1;
data = new byte[ diskSize * blockSize ];
command = IDLE;
readyBuffer = false;
buffer = null;
currentBlockId = 0;
targetBlockId = 0;
try {
FileInputStream ifstream = new FileInputStream( "DISK" );
int readableSize = ( ifstream.available( ) < data.length ) ?
ifstream.available( ) : data.length;
ifstream.read( data, 0, readableSize );
ifstream.close( );
} catch ( FileNotFoundException e ) {
SysLib.cerr( "threadOS: DISK created\n" );
} catch ( IOException e ) {
SysLib.cerr( e.toString( ) + "\n" );
}
}
public synchronized boolean read( int blockId, byte buffer[] ) {
if ( blockId < 0 || blockId > diskSize ) {
SysLib.cerr( "threadOS: a wrong blockId for read\n" );
return false;
}
if ( command == IDLE && readyBuffer == false ) {
this.buffer = buffer;
targetBlockId = blockId;
command = READ;
notify( );
return true;
} else
return false;
}
public synchronized boolean write( int blockId, byte buffer[] ) {
if ( blockId < 0 || blockId > diskSize ) {
SysLib.cerr( "threadOS: a wrong blockId for write\n" );
return false;
}
if ( command == IDLE && readyBuffer == false ) {
this.buffer = buffer;
targetBlockId = blockId;
command = WRITE;
notify( );
return true;
} else
return false;
}
public synchronized boolean sync( ) {
if ( command == IDLE && readyBuffer == false ) {
command = SYNC;
notify( );
return true;
} else
return false;
}
public synchronized boolean testAndResetReady( ) {
if ( command == IDLE && readyBuffer == true )
{
readyBuffer = false;
SysLib.disk( COND_DISK_REQ );
return true;
} else
return false;
}
public synchronized boolean testReady( ) {
if ( command == IDLE && readyBuffer == true ) {
return true;
} else
return false;
}
private synchronized void waitCommand( ) {
while ( command == IDLE ) {
try {
wait( );
} catch ( InterruptedException e ) {
SysLib.cerr( e.toString( ) + "\n" );
}
readyBuffer = false;
}
}
private void seek( ) {
int seekTime = transferTime + delayPerTrack
* Math.abs( targetBlockId/trackSize - currentBlockId/trackSize );
try {
Thread.sleep( seekTime );
} catch( InterruptedException e ) {
SysLib.cerr( e.toString( ) + "\n" );
}
currentBlockId = targetBlockId;
}
private synchronized void finishCommand( ) {
command = IDLE;
readyBuffer = true;
SysLib.disk( COND_DISK_FIN );
}
public void run ( ) {
while ( true ) {
waitCommand( );
seek( );
// System.out.println( "Disk: command = " + command );
switch( command ) {
case READ:
System.arraycopy( data, targetBlockId * blockSize,
buffer, 0,
blockSize );
break;
case WRITE:
System.arraycopy( buffer, 0,
data, targetBlockId * blockSize,
blockSize );
break;
case SYNC:
try {
FileOutputStream ofstream = new FileOutputStream( "DISK" );
ofstream.write( data );
ofstream.close( );
} catch ( FileNotFoundException e ) {
SysLib.cerr( e.toString( ) );
} catch ( IOException e ) {
SysLib.cerr( e.toString( ) );
}
// SysLib.cerr( "threadOS: DISK synchronized\n" );
break;
}
finishCommand( );
}
}
}