Question: JAVA, PLS PLS READ CAREFULLY AND ANSWER COMPLETELY FOR UPVOTE STARTER CODE: class Disk // extends Thread { static final int NUM_SECTORS = 2048; StringBuffer

JAVA, PLS PLS READ CAREFULLY AND ANSWER COMPLETELY FOR UPVOTE

STARTER CODE:

class Disk

// extends Thread

{

static final int NUM_SECTORS = 2048;

StringBuffer sectors[] = new StringBuffer[NUM_SECTORS];

Disk()

{

}

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

{

}

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

{

}

}

class Printer

// extends Thread

{

Printer(int id)

{

}

void print(StringBuffer data) // call sleep

{

}

}

class PrintJobThread

extends Thread

{

StringBuffer line = new StringBuffer(); // only allowed one line to reuse for read from disk and print to printer

PrintJobThread(String fileToPrint)

{

}

public void run()

{

}

}

class FileInfo

{

int diskNumber;

int startingSector;

int fileLength;

}

class DirectoryManager

{

// private Hashtable T = new Hashtable();

DirectoryManager()

{

}

void enter(StringBuffer fileName, FileInfo file)

{

}

FileInfo lookup(StringBuffer fileName)

{

return null;

}

}

class ResourceManager

{

}

class DiskManager

{

}

class PrinterManager

{

}

class UserThread

extends Thread

{

UserThread(int id) // my commands come from an input file with name USERi where i is my user id

{

}

public void run()

{

}

}

public class MainClass

{

public static void main(String args[])

{

for (int i=0; i

System.out.println("Args[" + i + "] = " + args[i]);

System.out.println("*** 141 OS Simulation ***");

}

}

Homework 8 & 9 is to write a simple operating system simulation, called 141OS, that applies the programming language mechanisms that support concurrency in Java. 141OS allows multiple users to create, save, and print files, which are stored on multiple disks and printed to multiple printersall of which can be considered concurrent processes. The goal of the system is to exploit potential parallelism to keep the relatively slow hardware devices (disks and printers) as busy as possible.

Disk

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. (Change to 80 ms to submit to Gradescope.)

class Disk {

static final int NUM_SECTORS = 2048;

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

StringBuffer sectors[] = new StringBuffer[NUM_SECTORS];

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

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

}

Printer

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. (Change sleep time to 275 to submit to Gradescope.)

class Printer {

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

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.

Your program must be runnable from the command line and take command line arguments to define the number of users, disks, and printers. The command line format will be as follows:

$ java -jar 141OS.jar -#ofUsers -#ofDisks -#ofPrinters

An example command line with arguments would be

$ java -jar 141OS.jar -4 -2 -3

This example command with arguments specifies that this run of 141OS should have 4 users, 2 Disks, and 3 Printers defined, with USER0, USER1, USER2, and USER3 being files that give the commands for respective user input. The output will be in the files PRINTER0, PRINTER1, and PRINTER2 since there are three printers. Store instances of the appropriate objects in three separate arrays: Users, Printers, and Disks. (Refer to this Java tutorial for how to pass arguments to your program.)

The autograder will test that your programs correctly create the supplied number of printers and print at least something into each one of them.

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!