PLEASE READ CAREFULLY. This assignment has a lot information and i did the best
ID: 664983 • Letter: P
Question
PLEASE READ CAREFULLY. This assignment has a lot information and i did the best i could to cut it short. This subject is Data Structure. We are learning about intro to Queues. This is a two-part assignment.
1) Perform a hand simulation (draw a pitcure) of the mfq simulation lab to ensure that you understand the algorithm. Your output should agree with the partial output provided in the lab document. This picture is really important for me to see
2) In this lab you will write a Java program that will simulate an operating system’s job scheduling policy to determine which process will be assigned the CPU when it becomes available. We will utilize a system of queues to simulate a sophisticated job scheduling mechanism, the multi-level feedback queue (MFQ)
Here are materials on what to use for this lab below.
The input to this simulation will consist of job requests, each composed of three elements:
· Arrival time – Time job is initially submitted to the pattern
· Process identifier (pid) – Identifies each process as it travels through the system
· CPU time – CPU time required by the process to complete its job
The output, displayed in a tabular format, should include a table entry each time a job enters the system indicating the:
· Event
· System time
· Processor identifier (pid)
· CPU time needed
When a process a completes and leaves the system, an appropriate table entry should be output to the indicate:
· Event
· System time
· Process identifier (pid)
· Total time in the system
· Identify of the lowest- level queue in which the process resided
- When the simulation is complete, you should also output the following:
· Total number of jobs
· Total time of all jobs in system – sum of the time each job is in the system
· Average response time – where response time is defines as the interval from the time a process enters the system to the time of first response, or the time it starts running
· Average turnaround time for the jobs – where turnaround time is defined as the interval from the time a process enters the system to the time of its completion
· Average waiting time – where waiting is the amount of time that a process is kept waiting in a queue
· Average throughout for the system as a whole – the number of jobs divided by the sum of total time of all jobs in the system
· Total CPU idle time – time the CPU has no jobs to run
- Be sure that your program sends all output to a file called csis.txt (printwriter). This output file will be submitted along with our source code for the lab.
- Be sure all your data is properly labeled and all average values are displayed to two decimal places.
- Here is the data set which your program should run on. Please put this in the mfq.txt
2 101 3
7 102 1
9 103 7
12 104 5
14 105 1
17 106 1
18 107 50
24 108 2
34 109 12
37 110 3
44 111 10
45 112 48
50 113 1
56 114 1
71 115 3
81 116 2
-Here are the first couple of lines of output and the last couple of lines of output that your program should generate. This is IMPORTANT as well to show that your output is displaying correctly.
Lab Notes 1: Classes
Its important you should have 6 total classes for your lab:
Driver Job MFQ CPU ObjectQueue ObjectQueueInterface
The Job class include these class variables:
The CPU class include these class variables:
- Use this Driver class that is given. DRIVER CLASS IS THE MAIN CLASS
// Driver Class Java
Import java.io*;
public class Driver
{
publice static void(String [ ] args) throws IOException
{
PrintWriter pw = new PrintWriter(new FileWriter(“csis.txt”));
MFQ mfq = new MFQ(pw);
mfw.getJobs();
mfw.outputHeader();
mfw.runSimulation();
mfw.outStats();
pw.close();
}
}
- Use this ObjectQueue is given
//ObjectQueue.java
public class ObjectQueue {
private Object[] item;
private int front;
private int rear;
private int count;
public ObjectQueue() {
item = new Object[1];
front = 0;
rear = -1;
count = 0;
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == item.length;
}
public void clear() {
item = new Object[1];
front = 0;
rear = -1;
count = 0;
}
public void insert(Object o) {
if (isFull())
resize(2 * item.length);
rear = (rear+1) % item.length;
item[rear] = o;
++count;
}
public Object remove() {
if (isEmpty()) {
System.out.println("Queue Underflow");
System.exit(1);
}
Object temp = item[front];
item[front] = null;
front = (front+1) % item.length;
--count;
if (count == item.length/4 && item.length != 1)
resize(item.length/2);
return temp;
}
public Object query() {
if (isEmpty()) {
System.out.println("Queue Underflow");
System.exit(1);
}
return item[front];
}
private void resize(int size) {
Object[] temp = new Object[size];
for (int i = 0; i < count; ++i) {
temp[i] = item[front];
front = (front+1) % item.length;
}
front = 0;
rear = count-1;
item = temp;
}
}
Lab Notes 2 – Input Queue
Along with the four queues that you will be using for this lab, I would also recommend creating an input queue. Before the lab simulation begins, read each line of input from mfq.txt, insert data into a newly created Job object, and then insert the Job object at the front of the queue to determine whether or not it’s time for the Job object at the front of the input queue to determine whether or not it’s time for the Job object to enter the simulation, i.e., is the system time equal to the arrival time of the Job object at the front of the queue?
Lab Notes 3 - String Class Split Method to Parse Strings
- When reading in data from a file on a line-by-line basis, we often have the need to parse(i.e., divide up) the string to extract the individual pieces of information. The queue lab requires that you read the mfq.txt input file that contains three integers per line to grab each of the three integers on the line.
- We can use Scanner class to reach each line from the input file
- You could use then use the StringTokenizer class to parse each line read from the input file. However, the StringTokenizer class is losing favor to the split() method found in the String class.
Here is a diagram as well
Explanation / Answer
CPU.java
import java.io.*;
public class CPU
{
ObjectQueue Oq1 = new ObjectQueue();
ObjectQueue Oq2 = new ObjectQueue();
ObjectQueue Oq3 = new ObjectQueue();
ObjectQueue Oq4 = new ObjectQueue();
int cpuTime = 0,
quantum = 0;
private PrintWriter writer;
Job job = new Job(writer);
int jobID = job.jobID();
int enQue = job.enQue();
int workTime = 0;
public CPU(PrintWriter Objwri)throws IOException
{
this.writer = Objwri;
}
public void cpudoWork(Object queueTop)
{
job.cutter((String)queueTop);
System.out.println(cpuTime + "**" + job.enQue() + "**" + job.workTime());
if(job.enQue() == cpuTime)
{
Oq1.objinsert(queueTop);
workTime = job.workTime();
cpuqueueOne();
}
else if(Oq1.objisEmpty() && Oq2.objisEmpty() && Oq3.objisEmpty() && Oq4.objisEmpty())
{
cpuTime++;
cpudoWork(queueTop);
}
else if(!Oq1.objisEmpty())
{
cpuqueueOne();
cpudoWork(queueTop);
}
else if(!Oq2.objisEmpty())
{
cpuqueueTwo();
cpudoWork(queueTop);
}
else if(!Oq3.objisEmpty())
{
cpuqueueThree();
cpudoWork(queueTop);
}
else if(!Oq4.objisEmpty())
{
cpuqueueFour();
cpudoWork(queueTop);
}
}
public int cpugetTime()
{
return cpuTime;
}
public void cpuqueueOne()
{
if(quantum < 2 && workTime > 0)
{
quantum++;
workTime--;
cpuTime++;
}
else if(workTime == 0)
{
System.out.println("1: " + cpuTime + " " + quantum);
System.out.println(cpuTime + " REMOVED: " + Oq1.objremove());
workTime = job.workTime();
quantum = 0;
}
else
{
Oq2.objinsert(Oq1.objremove());
System.out.println("1: " + cpuTime + " " + quantum);
quantum = 0;
cpuqueueTwo();
}
}
public void cpuqueueTwo()
{
if(quantum < 4 && workTime > 0)
{
quantum++;
workTime--;
cpuTime++;
}
else if(workTime == 0)
{
System.out.println("2: " + cpuTime + " " + quantum);
System.out.println(cpuTime + " REMOVED: " + Oq2.objremove());
workTime = job.workTime();
quantum = 0;
}
else
{
Oq3.objinsert(Oq2.objremove());
System.out.println("2: " + cpuTime + " " + quantum);
quantum = 0;
cpuqueueThree();
}
}
public void cpuqueueThree()
{
if(quantum < 8 && workTime > 0)
{
quantum++;
workTime--;
cpuTime++;
}
else if(workTime == 0)
{
System.out.println("3: " + cpuTime + " " + quantum);
System.out.println(cpuTime + " REMOVED: " + q3.objremove());
workTime = job.workTime();
quantum = 0;
}
else
{
Oq4.objinsert(Oq3.objremove());
System.out.println("3: " + cpuTime + " " + quantum);
quantum = 0;
cpuqueueFour();
}
}
public void cpuqueueFour()
{
if(quantum < 16 && workTime > 0)
{
quantum++;
workTime--;
cpuTime++;
}
else if(workTime == 0)
{
System.out.println("4: " + cpuTime + " " + quantum);
System.out.println(cpuTime + " REMOVED: " + Oq4.objremove());
quantum = 0;
workTime = job.workTime();
}
else
{
Oq4.objinsert(Oq4.objremove());
System.out.println("4: " + cpuTime + " " + quantum);
System.out.println(workTime);
System.out.println(Oq4.objquery());
}
}
}
Job.java
import java.io.*;
import java.util.Scanner;
public class Job
{
private Scanner read = new Scanner(new File("input.txt"));
String s = read.nextLine();
String[] st = s.split("\s+");
public Job(PrintWriter write)throws IOException
{
}
public Job(PrintWriter write, Scanner read) throws IOException
{
this.read = read;
}
public void jobformatS()
{
if(s.startsWith(" "))
s = s.replaceFirst(" ", "");
else;
}
public void job(Scanner read)throws IOException
{
s = read.nextLine();
jobformatS();
st = s.split("\s+");
}
public Object jjobList()
{
return enQue() + " " + jobID() + " " + workTime();
}
public void cutter(String queue)
{
st = queue.split("\s+");
}
public int enQue()
{
return Integer.parseInt(String.valueOf(st[0]));
}
public int jobID()
{
return Integer.parseInt(String.valueOf(st[1]));
}
public int workTime()
{
return Integer.parseInt(String.valueOf(st[2]));
}
}
MFQ.java
import java.io.*;
import java.util.Scanner;
public class MFQ extends Driver
{
private PrintWriter writer;
ObjectQueue que = new ObjectQueue();
CPU cpu = new CPU(writer);
Scanner read = new Scanner(new File("input.txt"));
Job job = new Job(writer, read);
public MFQ(PrintWriter Objwri)throws IOException
{
this.writer = Objwri;}
public void getJobs()throws IOException
{
Job job = new Job(writer, read);
if(!read.hasNextLine()){}
else
{
job.job(read);
que.objinsert(job.jjobList());
getJobs();
}
}
public void outputHeader()
{
System.out.println(" Add Process Work Time: ID: Time:");
}
public void runSimulation()throws IOException
{
if(!que.objisEmpty())
{cpu.cpudoWork(que.objremove());
System.out.println(cpu.cpugetTime());
if(!que.objisEmpty())
System.out.println(que.objquery());
else;
runSimulation();}
}
public void outStats()
{
if(!que.objisEmpty())
{System.out.println(que.objquery());
writer.println(que.objremove());
outStats();
}
}
}
ObjectQueue.java
public class ObjectQueue implements ObjectQueueInterface
{
private Object[] item;
private int front;
private int rear;
private int size;
public ObjectQueue()
{
size = 100;
item = new Object[size];
front = size-1;
rear = size-1;
}
public ObjectQueue(int max)
{
size = max;
item = new Object[size];
front = size-1;
rear = size-1;
}
public boolean objisEmpty()
{
return front == rear;
}
public boolean objisFull()
{
return rear == size-1 ? front == 0 : front == rear+1;
}
public void objclear()
{
item = new Object[size];
front = size-1;
rear = size-1;
}
public void objinsert(Object x)
{
if (objisFull())
{
System.out.println("Insert Runtime Error: Queue Overflow");
System.exit(1);
}
if (rear == size-1)
rear = 0;
else
rear++;
item[rear] = x;
}
public Object objremove()
{
if (objisEmpty())
{
System.out.println("Remove Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1)
front = 0;
else
front++;
Object temp = item[front];
item[front] = null;
return temp;
}
public Object objquery()
{
if (objisEmpty())
{
System.out.println("Query Runtime Error: Queue Underflow");
System.exit(1);
}
if (front == size-1)
return item[0];
else
return item[front+1];
}
}
Driver.java
import java.io.*;
public class Driver {
public static void main(String[] args) throws IOException{
PrintWriter pw = new PrintWriter(
new FileWriter("csis.txt"));
MFQ mfq = new MFQ(pw);
mfq.getJobs();
mfq.outputHeader();
mfq.runSimulation();
mfq.outStats();
pw.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.