Question: JAVA Implement each disk as an array of StringBuffers Each disk has a capacity, NUM_SECTORS. The constructor must allocate all the StringBuffers (one per sector)

JAVA

Implement each disk as an array of StringBuffers

Each disk has a capacity, NUM_SECTORS.

The constructor must allocate all the StringBuffers (one per sector) when the Disk is created, and must not allocate anything after construction.

Only a single sector may be read or written at a time, so to store a file, a write operation must be issued for each input line.

Read and write operations each take 800 milliseconds; to simulate this delay, each of the read/write functions must sleep for that amount of time before copying the data to/from any disk sector.

class Disk {

static final int NUM_SECTORS = 2048;

static final int DISK_DELAY = 800; // make it 80 for testing

StringBuffer sectors[] = new StringBuffer[NUM_SECTORS];

void write(int sector, StringBuffer data); // call sleep

void read(int sector, StringBuffer data); // call sleep

}

Each printer will write data to a file named PRINTERi, where i is the index of this printer (starting at 0).

A printer can only handle one line of text at a time.

It will take 2750 milliseconds to print one line; the thread needs to sleep to simulate this delay before each line is printed.

class Printer {

static final int PRINT_DELAY = 2750; // make it 275 for testing

void print(StringBuffer data); // call sleep

}

UserThread

Each user will read from a file named USERi, where i is the index of this user. Each USERi file will contain a series of the following three commands:

.save fileName

.end

.print fileName

UserThread interprets the users commands.

All lines between a .save and a .end are part of the data in fileName. Each line in the file takes up one sector on the disk.

UserThread handles saving files to disk itself.

UserThread will create a PrintJobThread whenever it gets a print command.

PrintJobThread

PrintJobThread handles printing an entire print request.

It must request exclusive access rights to a printer (blocking if they are all busy), then repeatedly read a sector from the Disk and then send it to the printer - one line (StringBuffer) at a time.

A UserThread will create and start a new PrintJobThread for each print request.

Note: if each PrintJobThread is not a distinct thread, there will be very little parallelism in the OS!

(A slightly different design is to have one thread per printer, handling the current print request for that printer. The UserThread would enqueue the name of a file to be printed, and this thread would process the files in FIFO order.)

FileInfo

A FileInfo object will hold the disk number, length of the file (in sectors), and the index of the starting sector (i.e., which sector the first line of the file is in).

This FileInfo will be used in the DirectoryManager to hold meta information about a file after saving to a disk, as well as to allow a PrintJobThread to locate the file contents on disk for printing.

class FileInfo {

int diskNumber;

int startingSector;

int fileLength;

}

DirectoryManager

The DirectoryManager is a table that knows where files are stored on disk via mapping file names into disk sectors. Use the pre-defined Java Class Hashtable to store this information (in the form of FileInfo), but the HashTable must be defined inside class DirectoryManager.

class DirectoryManager {

private Hashtable T =

new Hashtable();

void enter(StringBuffer fileName, FileInfo file);

FileInfo lookup(StringBuffer fileName);

}

ResourceManager

A ResourceManager gives a specific Thread exclusive access rights to a resource, either a Disk or a Printer.

Derive PrinterManager and DiskManager from ResourceManager. The implementation will be something like the following (be sure you understand what the code below does and how it works. Why must these methods be synchronized? What do wait() and notify() do?):

class ResourceManager { boolean isFree[]; ResourceManager(int numberOfItems) { isFree = new boolean[numberOfItems]; for (int i=0; isynchronized int request() { while (true) { for (int i = 0; i < isFree.length; ++i) if ( isFree[i] ) { isFree[i] = false; return i;

}

this.wait(); // block until someone releases Resource

}

}

synchronized void release( int index ) { isFree[index] = true; this.notify(); // let a blocked thread run } }

DiskManager

The DiskManager is derived from ResourceManager and also keeps track of the next free sector on each disk, which is useful for saving files.

The DiskManager should contain the DirectoryManager for finding file sectors on Disk.

PrinterManager

The PrinterManager is derived from ResourceManager.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!