This lab assignment is filled with a lot of information, i tried the best i coul
ID: 664940 • Letter: T
Question
This lab assignment is filled with a lot of information, i tried the best i could to shorten the lab info. Also please comments on code so i know what you are doing.
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. 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.
- Present a visual display of the simulation.
- Here is the data set, mfq,txt, which your program should run on:
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
Lab Notes 1: Classes
You should use at least the following classes in the lab:
Driver Job MFQ CPU ObjectQueue
The Job class include these class variables:
The CPU class include these class variables:
- Use this Driver 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
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?
Here is a diagram as well
Flowchart to help think about this simulation lab
Explanation / Answer
import java.awt.*;
import java.util.*;
import java.applet.*;
public class OSProcess extends Object {
int id;
int priority;
int cpu_time_needed;
int cpu_time_chunk;
int cpu_time_whole;
int io_time_needed;
int io_time_whole;
int response_time;
int time_in;
public int nextPos; /* next process array index--"pointer" */
public boolean deleteFlag;
public OSProcess() {
}
public void init(int i, int p, int cpu, int io, int t) {
Random RandNumb;
RandNumb = new Random();
id = i;
System.out.println("Initializing PID#"+id);
priority = p;
cpu_time_needed = (int) (cpu * RandNumb.nextFloat())+1;
io_time_needed = (int) (io * RandNumb.nextFloat())+1;
time_in = t;
nextPos = -1;
System.out.println("Initialized PID#"+id);
}
public void paint(Graphics g, int ndown, int x, int y) {
y+=ndown*11;
g.setColor(Color.lightGray);
g.fillRect(x+1,y+1,23,14);
g.setColor(Color.black);
g.drawRoundRect(x,y,25,15,4,4);
int theAscent = g.getFontMetrics().getAscent();
int theLeading = g.getFontMetrics().getLeading();
String label = " "+id+".";
int theWidth = g.getFontMetrics().stringWidth(label);
int margin = (15-theWidth)/2;
g.setColor(Color.blue);
g.drawString(label,x+margin,y+2+theAscent+theLeading);
}
}
public class OSQueue extends Object {
int queueType; /* 1 = FIFO,
2 = LIFO,
3 = SJF,
4 = LJF,
5 = RR */
int maxElements;
int curElements;
int qHead;
int x,y; /* graphical coordinates */
public OSQueue(int q, int max, int xc, int yc) {
queueType = q;
maxElements = max;
qHead = -1;
x = xc; y = yc;
}
public void init() {
}
/*** put processes into the queue based on the queue type's ***/
/*** algorithm. The head must be the next in line to leave. ***/
public void insert(int P) {
int ptr, optr;
optr = 0;
System.out.println("We are in Q insert");
if (curElements == maxElements)
return;
switch(queueType) {
case 1: /* FIFO */
case 5: /* Round Robin */
/* traverse the Q to first */
System.out.println("IN OSQueueInsert, Qtype: "+queueType+", qHead: "+qHead+" P: "+P);
ptr = qHead;
if(qHead == -1) {
qHead = P;
OSSystem.ProcessArray[qHead].nextPos = -1;
} else {
optr = ptr;
while(ptr != -1) {
System.out.println("We are in FIFO while ptr = "+ptr);
optr = ptr;
ptr = OSSystem.ProcessArray[ptr].nextPos;
}
OSSystem.ProcessArray[optr].nextPos = P;
}
curElements++;
System.out.println("We are leaving FIFO; current elements: "+curElements);
break;
case 2: /* LIFO */
ptr = qHead;
System.out.println("IN OSQueueInsert, Qtype: "+queueType+", qHead: "+qHead+" P: "+P);
if (ptr != -1) {
/* head not empty */
qHead = P;
OSSystem.ProcessArray[P].nextPos = ptr;
}
else {
/* head is empty */
qHead = P;
OSSystem.ProcessArray[P].nextPos = -1;
}
curElements++;
System.out.println("Leaving LIFO; current elements: "+curElements);
break;
case 3: /* SJF */
ptr = qHead;
while (OSSystem.ProcessArray[P].cpu_time_whole > OSSystem.ProcessArray[ptr].cpu_time_whole) {
optr = ptr;
ptr = OSSystem.ProcessArray[ptr].nextPos;
}
if (qHead != ptr) {
OSSystem.ProcessArray[optr].nextPos = P;
OSSystem.ProcessArray[P].nextPos = ptr;
}
else {
qHead = P;
OSSystem.ProcessArray[P].nextPos = ptr;
}
curElements++;
break;
case 4: /* LJF */
ptr = qHead;
while (OSSystem.ProcessArray[P].cpu_time_whole > OSSystem.ProcessArray[ptr].cpu_time_whole) {
optr = ptr;
ptr = OSSystem.ProcessArray[ptr].nextPos;
}
if (qHead != ptr) {
OSSystem.ProcessArray[optr].nextPos = P;
OSSystem.ProcessArray[P].nextPos = ptr;
}
else {
qHead = P;
OSSystem.ProcessArray[P].nextPos = ptr;
}
curElements++;
break;
}
System.out.println("We are at the end of Q insert");
}
/*** this is the retest() method for longest job first retests ***/
public boolean retest(int cpuProc) {
if ((queueType == 4) && (OSSystem.ProcessArray[cpuProc].cpu_time_needed
< OSSystem.ProcessArray[qHead].cpu_time_needed))
return true;
else
return false;
}
/*** this routine pulls out the next process from the array ***/
/*** the next process of course is the one pointed to by head ***/
public int next() {
int np;
if (curElements > 0) {
np = qHead;
qHead = OSSystem.ProcessArray[np].nextPos;
OSSystem.ProcessArray[np].nextPos = -1;
curElements--;
return np;
}
return -1;
}
/**** here is where things in all the processes of the queue ****/
/**** are updated with every cycle of the simulation ****/
public void updateProcess() {
}
public boolean notEmpty() {
if (curElements > 0)
return true;
else
return false;
}
public void printStatus()
{
System.out.println(" Queue status, Type: "+queueType+" elements: "+curElements);
if(!notEmpty())
System.out.println("The Queue is empty");
else {
System.out.println("The Queue is not Empty");
System.out.println(" "+qHead);
int ptr = qHead;
while (ptr != -1) {
System.out.println(" "+OSSystem.ProcessArray[ptr].nextPos);
ptr = OSSystem.ProcessArray[ptr].nextPos;
}
}
}
public boolean full() {
if (curElements == maxElements)
return true;
else
return false;
}
public void paint(Graphics g) {
g.setColor(Color.green);
g.drawRoundRect(x, y, 40, maxElements*21+8, 5, 5);
g.setColor(Color.cyan);
g.fillRoundRect(x+1, y+1, 39, maxElements*21+7, 5, 5);
int ptr = qHead;
int i=0;
while (ptr != -1) {
OSSystem.ProcessArray[ptr].paint(g, i++, x+8, y+8);
ptr = OSSystem.ProcessArray[ptr].nextPos;
i = i+1;
}
}
}
public class OSDevice extends Object {
public int curr_process;
String devName;
int x,y;
public int execs;
public OSDevice(String Q, int xc, int yc) {
devName = Q;
execs=0;
curr_process = -1;
x = xc; y = yc;
}
public int execute() {
int retval;
Random RandNumb = new Random();
System.out.println("Executingg CPU; devName: "+devName);
execs++;
retval = 0;
if (devName.equals("CPU")) {
System.out.println("Qtype inside: "+devName);
OSSystem.ProcessArray[curr_process].cpu_time_whole++;
System.out.println("cpu_time_whole"+ OSSystem.ProcessArray[curr_process].cpu_time_whole);
System.out.println("cpu_time_needed"+
OSSystem.ProcessArray[curr_process].cpu_time_needed);
if (RandNumb.nextFloat() > .5)
retval = 1;
else{
if (OSSystem.ProcessArray[curr_process].cpu_time_whole >=
OSSystem.ProcessArray[curr_process].cpu_time_needed)
retval = 2;
else
retval = 3;
}
}
else if (devName.equals("IO")) {
System.out.println("Qtype inside IO device: "+devName);
OSSystem.ProcessArray[curr_process].io_time_whole++;
if (OSSystem.ProcessArray[curr_process].io_time_whole >=
OSSystem.ProcessArray[curr_process].io_time_needed)
retval = 2;
else
retval = 3;
}
return retval;
/**** need to return an integer return type ***/
/*** 0 = logic error ***/
/*** 1 = need IO ***/
/*** 2 = I'm finished ***/
}
public void insert(int p) {
curr_process = p;
OSSystem.ProcessArray[p].nextPos = -1;
}
public int remove() {
int x;
System.out.println("Removing from "+ devName +" "+curr_process);
x = curr_process;
curr_process = -1;
return x;
}
public boolean full() {
if (curr_process < 0)
return false;
else
return true;
}
public void printStatus()
{
System.out.println(" Device status for: "+devName+" process: "+curr_process);
if(!full())
System.out.println("The device is empty");
else
if (devName.equals("IO"))
System.out.println("The IO device is busy, io time req="+
OSSystem.ProcessArray[curr_process].io_time_needed+
"...io time whole="+OSSystem.ProcessArray[curr_process].io_time_whole);
else
System.out.println("The CPU device is busy, time req="+
OSSystem.ProcessArray[curr_process].cpu_time_needed+
" time whole="+OSSystem.ProcessArray[curr_process].cpu_time_whole);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRoundRect(x+1,y+1,39,39,5,5);
g.setColor(Color.red);
g.drawRoundRect(x, y, 40, 40, 5, 5);
if (curr_process != -1)
OSSystem.ProcessArray[curr_process].paint(g, 0, x+8, y+10);
}
}
public class OSSystem extends Applet implements Runnable {
private Thread p1=null;
/** counter variables **/
int TimeClock;
int CurrentID;
int nextSlot;
/** job parameter variables **/
int CPULevels;
int IOLevels;
int priorityLevels;
int newThreshhold;
int maxProcesses;
int numProcesses;
/** objects **/
OSDevice CPU, IO;
OSQueue ReadyQ, IOQ;
//DialogWindow testDlg;
Random RandNumb = new Random();
static OSProcess[] ProcessArray;
/** shared variables **/
public boolean getData = false;
public boolean started;
public void initialize(int n) {
TimeClock = 0;
CurrentID = 0;
CPULevels = 2;
IOLevels = 3;
priorityLevels = 10;
newThreshhold = 80;
maxProcesses = n;
numProcesses = 0;
nextSlot = 0;
CPU = new OSDevice("CPU",75,30);
IO = new OSDevice("IO",225,30);
ReadyQ = new OSQueue(1, 15,10,30); /* FIFO max 10 by default*/
IOQ = new OSQueue(2, 10,150,30); /* LIFO max 50 by default*/
/* here is where the all the process stay */
ProcessArray = new OSProcess[n];
for (int j=0; j numProcesses) && ((100*RandNumb.nextFloat()) <= newThreshhold))
return true;
else
return false;
}
public void start() {
if (p1 == null) {
p1 = new Thread(this);
if(!getData)
{
p1.stop();
getData = createdialog();
}
p1.start();
}
}
public void stop() {
if (p1 != null) {
p1.stop();
p1 = null;
}
}
public void run () {
boolean switchFlag, done, canFinish;
switchFlag = false;
int rc;
int P;
initialize(20);
done = false;
started = false;
canFinish = false;
while ( !done ) {
// see if the IO device needs something from IOQ
if (started && !IO.full() && IOQ.notEmpty())
IO.insert(IOQ.next());
if (started) {
if (switchFlag) {
P = CPU.remove();
CPU.insert(ReadyQ.next());
ReadyQ.insert(P);
} else {
if (CPU.full()) {
rc = CPU.execute();
switch(rc) {
case 1: /* need IO */
IOQ.insert(CPU.remove());
break;
case 2: /* finnished */
CPU.remove();
break;
case 3:
System.out.println("Continuing");
break;
}
}
if (ReadyQ.notEmpty() && !CPU.full())
CPU.insert(ReadyQ.next());
switchFlag = ReadyQ.retest(CPU.curr_process);
}
}
if (anynew()) {
System.out.println("Current ID inserted is: "+CurrentID);
ReadyQ.insert(newProcess(CurrentID, priorityLevels,
CPULevels, IOLevels, TimeClock));
CurrentID++;
started = true;
System.out.println("I am here after ReadyQ.Insert");
if (CurrentID == maxProcesses)
canFinish = true;
}
if (started && !IOQ.notEmpty() && !ReadyQ.notEmpty() &&
!CPU.full() && !IO.full() && numProcesses == maxProcesses)
done = true;
if (started) {
update(this.getGraphics());
ReadyQ.printStatus();
CPU.printStatus();
IOQ.printStatus();
IO.printStatus();
}
if (started && IO.full()) {
//System.out.println("Inside if(iofull)");
rc = IO.execute();
if (rc == 2) {
if (!ReadyQ.full())
ReadyQ.insert(IO.remove());
}
else
if (rc == 3)
System.out.println("Continuing after IO");
}
//System.out.println("after if(iofull) done: "+done);
TimeClock++;
System.out.println("-------");
delay(500);
}
imFinished(this.getGraphics());
System.out.println("--- the End ----");
}
public void imFinished(Graphics g) {
g.setColor(Color.red);
Font theFont = new Font("TimesRoman", Font.BOLD, 36);
String label = "Simulation Complete";
g.setFont(theFont);
g.drawString(label,10,150);
theFont = new Font("Helvetica", Font.PLAIN, 18);
g.setFont(theFont);
g.setColor(Color.black);
label = "CPU Cycles="+Integer.toString(CPU.execs)+
" CPU Utilization ="+Float.toString(100*CPU.execs/TimeClock)+"%";
g.drawString(label,10,175);
label = "IO Cycles ="+Integer.toString(IO.execs)+
" IO Utilization ="+Float.toString(100*IO.execs/TimeClock)+"%";
g.drawString(label,10,200);
label = "Clock Cycles="+Integer.toString(TimeClock);
g.drawString(label,10,225);
}
public int newProcess(int id, int p, int c, int i, int t) {
int currSlot = 0;
if (nextSlot < maxProcesses)
{
System.out.println("1.1: Max Processes: "+ maxProcesses+" NextSlot: "+nextSlot+" currSlot: "+currSlot);
currSlot = nextSlot++;
System.out.println("1.2: Max Processes: "+ maxProcesses+" NextSlot: "+nextSlot+" currSlot: "+currSlot);
}
else
{
System.out.println("2.1: Max Processes: "+ maxProcesses+" NextSlot: "+nextSlot+" currSlot: "+currSlot);
currSlot = nextSlot;
System.out.println("2.2: Max Processes: "+maxProcesses+"NextSlot: "+nextSlot+" currSlot: "+currSlot);
}
ProcessArray[currSlot].init(id,p,c,i,t);
numProcesses++;
return currSlot;
}
public void update(Graphics g) {
paint(g);
}
public void delay(int timetowait) {
try { p1.sleep(timetowait); }
catch(InterruptedException e) {}
}
public void paint(Graphics g) {
System.out.println(" ** PAINTING ** ");
ReadyQ.paint(g);
CPU.paint(g);
IOQ.paint(g);
IO.paint(g);
Font theFont = new Font("TimesRoman", Font.PLAIN, 12);
String label = "Ready Queue";
g.setColor(Color.blue);
g.setFont(theFont);
g.drawString(label,10,15);
label = "IO Queue ";
g.drawString(label,153,15);
label = "CPU";
g.drawString(label,85,95);
label = "IO Device ";
g.drawString(label,225,95);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.