Class Disk

Object
  extended byDisk
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
FastDisk

public class Disk
extends Object
implements Runnable

A software simulation of a Disk.

You may not change this class.

This disk is slow and ornery. It contains a number of blocks, all BLOCK_SIZE bytes long. All operations occur on individual blocks. You can't modify any more or any less data at a time.

To read or write from the disk, call beginRead() or beginWrite(). Each of these functions will start the action and return immediately. When the action has been completed, the Disk calls Kernel.interrupt() to let you know the Disk is ready for more.

It may take a while for the disk to seek from one block to another. Seek time is proportional to the difference in block numbers of the blocks.

Warning: Don't call beginRead() or beginWrite() while the disk is busy! If you don't treat the Disk gently, the system will crash! (Just like a real machine!)

This disk saves its contents in the Unix file DISK between runs. Since the file can be large, you should get in the habit of removing it before logging off.

See Also:
Kernel

Nested Class Summary
protected static class Disk.DiskException
          The exception thrown when an illegal operation is attempted on the disk.
 
Field Summary
static int BLOCK_SIZE
          The size of a disk block in bytes.
private  byte[] buffer
          Memory buffer to/from which current I/O operation is transferring.
protected  boolean busy
          An indication of whether an I/O operation is currently in progress.
protected  int currentBlock
          Current location of the read/write head
protected  byte[] data
          The data stored on the disk
 int DISK_SIZE
          Total size of this disk, in blocks.
private  boolean isWriting
          An indication whether the current I/O operation is a write operation.
protected  int readCount
          A count of read operations performed, for statistics.
private  boolean requestQueued
          A flag set by beginRead or beginWrite to indicate that a request has been submitted.
protected  int targetBlock
          The block number to be read/written by the current operation.
protected  int writeCount
          A count of write operations performed, for statistics.
 
Constructor Summary
Disk(int size)
          Creates a new Disk.
 
Method Summary
 void beginRead(int blockNumber, byte[] buffer)
          Starts a new read operation.
 void beginWrite(int blockNumber, byte[] buffer)
          Starts a new write operation.
protected  void delay(int targetBlock)
          Sleeps for a while to simulate the delay in seeking and transferring data.
protected  void finishOperation()
          Indicates to the CPU that the current operation has completed.
 void flush()
          Saves the contents of this Disk.
 void run()
          This method simulates the internal microprocessor of the disk controler.
protected  void waitForRequest()
          Waits for a call to beginRead or beginWrite.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK_SIZE

public static final int BLOCK_SIZE
The size of a disk block in bytes.

See Also:
Constant Field Values

DISK_SIZE

public final int DISK_SIZE
Total size of this disk, in blocks.


currentBlock

protected int currentBlock
Current location of the read/write head


data

protected byte[] data
The data stored on the disk


busy

protected boolean busy
An indication of whether an I/O operation is currently in progress.


isWriting

private boolean isWriting
An indication whether the current I/O operation is a write operation. Only meaningful if busy == true.


targetBlock

protected int targetBlock
The block number to be read/written by the current operation. Only meaningful if busy == true.


buffer

private byte[] buffer
Memory buffer to/from which current I/O operation is transferring. Only meaningful if busy == true.


requestQueued

private boolean requestQueued
A flag set by beginRead or beginWrite to indicate that a request has been submitted.


readCount

protected int readCount
A count of read operations performed, for statistics.


writeCount

protected int writeCount
A count of write operations performed, for statistics.

Constructor Detail

Disk

public Disk(int size)
Creates a new Disk. If a Unix file named DISK exists in the local Unix directory, the simulated disk contents are initialized from the Unix file. It is an error if the DISK file exists but its size does not match "size". If there is no DISK file, the first block of the simulated disk is cleared to nulls and the rest is filled with random junk.

Parameters:
size - the total size of this disk, in blocks.
Method Detail

flush

public void flush()
Saves the contents of this Disk. The contents of this disk will be forced out to a file named DISK so that they can be restored on the next run of this program. This file could be quite big, so delete it before you log out. Also prints some statistics on disk operations.


delay

protected void delay(int targetBlock)
Sleeps for a while to simulate the delay in seeking and transferring data.

Parameters:
targetBlock - the block number to which we have to seek.

beginRead

public void beginRead(int blockNumber,
                      byte[] buffer)
Starts a new read operation.

Parameters:
blockNumber - The block number to read from.
buffer - A data area to hold the data read. This array must be allocated by the caller and have length of at least BLOCK_SIZE. If it is larger, only the first BLOCK_SIZE bytes of the array will be modified.

beginWrite

public void beginWrite(int blockNumber,
                       byte[] buffer)
Starts a new write operation.

Parameters:
blockNumber - The block number to write to.
buffer - A data area containing the data to be written. This array must be allocated by the caller and have length of at least BLOCK_SIZE. If it is larger, only the first BLOCK_SIZE bytes of the array will be sent to the disk.

waitForRequest

protected void waitForRequest()
Waits for a call to beginRead or beginWrite.


finishOperation

protected void finishOperation()
Indicates to the CPU that the current operation has completed.


run

public void run()
This method simulates the internal microprocessor of the disk controler. It repeatedly waits for a start signal, does an I/O operation, and sends an interrupt to the CPU. This method should not be called directly.

Specified by:
run in interface Runnable