This is a program simulating process management in an operating system. I am try
ID: 644215 • Letter: T
Question
This is a program simulating process management in an operating system. I am trying to set priority levels as a ratio of CPU_Used : Time Waiting. The lowest values will go to the top of the list. Any help would be great.
********************************************** PCB Class **********************************
import java.util.LinkedList ;
import java.util.Random ;
public class PCB
{
private String PCB_state;
private int PCB_ID ;
private int PCB_priority;
private String pgmCounter;
private int CPU_used;
private int CPU_max;
private int timeWaiting;
private static int PCB__K;
private Random random__X ;
// constructor methods
public PCB ()
{
Random random__X = new Random();
PCB__K+= 1 ; //=====> Increment the static variable for Process ID
PCB_state = "Ready" ;
PCB_ID = PCB__K ;
PCB_priority= 0 ;
pgmCounter = "" ;
CPU_used = 0 ;
CPU_max = random__X.nextInt(100) + 1 ; // Assign max to be between 1 and 1000
timeWaiting = 0 ;
}
// default constructor
public String showPCB()
{
return "state: " + PCB_state
+ " ID: " + Integer.toString(PCB_ID)
+ " K: " + pgmCounter
+ " priority: " + Integer.toString(PCB_priority)
+ " CPU used: " + Integer.toString(CPU_used)
+ " CPU max: " + Integer.toString(CPU_max)
+ " Wait: " + Integer.toString(timeWaiting)
;
}
// set methods
public String get_state()
{
return PCB_state;
}
public int get_ID()
{
return PCB_ID;
}
public int get_PCB_priority()
{
return PCB_priority ;
}
public int get_CPU_used()
{
return CPU_used;
}
public int get_CPU_max()
{
return CPU_max;
}
public String get_pgmCounter()
{
return pgmCounter;
}
public int get_timeWaiting()
{
return timeWaiting;
}
// Set methods , void returns no value
public void set_state(String state0)
{
PCB_state = state0 ;
}
public void set_PCB_priority(int priority0)
{
PCB_priority = priority0;
}
public void set_CPU_used(int CPU0)
{
CPU_used = CPU0 ;
}
public void add_CPU_used(int c0)
{
CPU_used += c0 ;
}
public void set_CPU_max(int CPU0)
{
CPU_max = CPU0 ;
}
public void set_pgmCounter(String pgmCounter0)
{
pgmCounter = pgmCounter0 ;
}
public void set_timeWaiting(int timeWaiting0)
{
timeWaiting = timeWaiting0 ;
}
public void add_timeWaiting(int t0)
{
timeWaiting += t0 ;
}
}
****************************************************** CPU_event Class *******************************************************
import java.util.Random ;
public class CPU_event // this class returns a result from a range by percentage
{
private int event ; private int rangeResult ;
private Random random__X ;
final static byte pct40 = 2 ; final static byte pct30 = 3 ;
final static byte pct13 = 7 ; final static byte pct10 = 10 ;
final static byte pct20 = 0 ;
public void CPU_event()
{
}
public int get_CPU_event()
{
Random random__X = new Random();
event = random__X.nextInt(1000)+1 ;
if ((event % pct10) == 0)
rangeResult = 1 ;
else
if ((event % pct13) == 0)
rangeResult = 2 ;
else
if ((event % pct30) == 0)
rangeResult = 3 ;
else
if ((event % pct40) == 0)
rangeResult = 4 ;
else
rangeResult = 0;
return rangeResult ;
}
}
*************************************** ProcessMgmt Class ***********************************************
import java.util.LinkedList ;
import java.util.Random ;
public class ProcessMgmt
{
public static void main(String args[])
{
//#010 Initialization of fields and data structures ///////////////
int QREADY__T = 25 ;
final int BLOCKIO = 3 ; final int INTERRUPT = 2 ;
final int COMPLETED = 1 ; final int BLOCKPAGE = 0 ;
Random random__X = new Random();
CPU_event event = new CPU_event();
int CPU_runtime ; int event__X ;
LinkedList<PCB> QReady = new LinkedList<PCB>(); //#005 Create the List for the Priority QReady
PCB PCB_Running ; //#020 Create the field for: PCB_Running
PCB PCB_Ready;
//#030
for (int ii = 0; ii < QREADY__T; ii++)
{
QReady.add(new PCB()) ; //#040 Add a new PCB object onto the LL
}
///////////////////////////////////////////////////////////////////////
//#080 ===> end of Initialization
System.out.println(" ***** Ready Queue *****");
for (PCB pcbLoop : QReady) //#090 Loop that executes based on the no. of nodes in the LL
System.out.println(pcbLoop.showPCB()); //#095 Print the PCB for an LL node
//#0100 Process until the active processes are all completed ///////
while ( QReady.size() > 0 )
{
//#0110 Next process to Run
PCB_Running = QReady.removeFirst(); //#0140 Assign the first node from QReady to Running
PCB_Running.set_state("Running") ; //#0145 Set the state value to "Running"
CPU_runtime = random__X.nextInt(20) + 1 ; //#0150 Get a random no. between 0 and 20
//#0160 Tally and set the CPU used for the process
PCB_Running.add_CPU_used(CPU_runtime) ;
System.out.println(" ***** Running Process *****");
System.out.println(PCB_Running.showPCB());
//#0180 Tally and set the wait times for the processes in QReady
for (PCB pcbLoop : QReady)
{
pcbLoop.add_timeWaiting(CPU_runtime);
}
//#0190 Check if the process completed (CPU used exceeds the CPU max)
if (PCB_Running.get_CPU_used() > PCB_Running.get_CPU_max())
{
PCB_Running.set_state("Completed");
System.out.println(" >>>>> Process "
+ PCB_Running.showPCB()
+ " <<<<<" );
continue; // iterate to the next in the WHILE loop
}
//-----------------------------------------------------------------
// Process did not complete, so continue
//-----------------------------------------------------------------
//#0200 Simulate the type of Block on the Process (I/O Block, Memory Paging Block, Interrupt)
event__X = event.get_CPU_event() ;
if (event__X == COMPLETED)
{
PCB_Running.set_state("Completed");
System.out.println(" >>>>> Process "
+ PCB_Running.showPCB()
+ " <<<<<" );
}
else
{
PCB_Running.set_state("Ready"); //#230 Set the state to "Ready" from "Running"
switch (event__X)
{
case INTERRUPT :
{
QReady.addFirst(PCB_Running) ; //#240 Add the PCB to the START of the QReady
System.out.println("***** Context Switch INTERRUPT event *****");
break;
}
case BLOCKPAGE :
{
int top3rd = QReady.size() / 3 ;
QReady.add(top3rd, PCB_Running) ; //#242 Add to the top 3rd of QReady
System.out.println("***** Context Switch PAGE event *****");
break;
}
case BLOCKIO :
{
int topHalf = QReady.size() / 2 ;
QReady.add(topHalf, PCB_Running) ; //#244 Add the PCB to the middle of QReady
System.out.println("***** Context Switch I/O event *****");
break;
}
default :
{
QReady.addLast(PCB_Running);
System.out.println("***** Context Switch TIME event *****");
break;
}
}
}
System.out.println(" ***** Context Switch Ready Queue *****");
for (PCB pcbLoop : QReady)
System.out.println(pcbLoop.showPCB());
}
}
}
Explanation / Answer
PCB is defined as the Process control block.the role of the PCBs is central in process management they are accesed and/or modified by most OS utilities,including those involved with scheduling,memory and I/O resources access the performance.
import java.util.LinkedList;
import java.util.Random;
public class PCB
{
private String PCB_state;
private int PCB_ID;
private int PCB_priority;
private String pgmCounter;
private int CPU_used;
private int CPU_max;
private int timeWaiting;
private static int PCB___K;
private Random random___X;
// constructor methods
public PCB()
{
Random random___X = new Random();
private int PCB_K+1;
private string PCB_state ="Ready";
private int PCB_ID =PCB___K;
private int PCB_priority =0;
private string pgmCounter ="stores the address of the current execution time";
private int CPU_used =0;
private int CPU_max =random___X.nextInt(100)+1;
private int timeWaitng =0;
}
public String showPCB()
{
return "state: " + PCB_state +" ID: " +
Integer.toString(PCB_ID) + " k: " +pgmCounter
+ " priority:" +
Integer.toString(PCB_priority)
+ " CPU used: " +
Integer.toString(CPU_used)
+ " CPU max:" +
Integer.toString(CPU_max)
+" wait: " +
integer.toString(timewaitng);
}
// set methods
set<string> mystring=new set<string>{'state','ID','priority','cpu_used','cpu_max','get_pgmcounter','get_timeWaiting'};
public String get_state()
{
return PCB_state;
}
public int get_ID()
{
return PCB_ID;
}
public int get_CPU_used()
{
return CPU_used;
}
public int get_CPU_max()
{
return CPU_max;
}
public String get_pgmCounter()
{
return pgmCounter;
}
public int get_timeWaiting()
{
return timeWaiting;
}hods,
//set methods,void returns no value
public void set_state(String state0)
{
PCB_state =state0;
}
public void set_PCB_priority(int priority0)
{
PCB_priority =priority0;
}
public void set_CPU_used(int CPU0)
{
CPU_used =CPU0;
}
public void add_CPU_used(int c0)
{
CPU_used +=c0;
}
public void set_CPU_max(int CPU0)
{
CPU_max =CPU0;
}
public void set_pgmCounter(String pgmCounter)
{
pgmCounter =pgmCounter0
}
publlic void set_timeWaiting(int timeWaiting0)
{
timeWaiting =timeWaiting0;
}
public void add_timeWaiting(int t0)
{
timeWaiting +=t0;
}
}
CPU_event class
import java.util.Random;
public class CPU_event
{
private int event;
private int rangeResult;
private Random random___X;
final static byte pct40 =2;
final static byte pct30 =3;
final static byte pct13 =7;
final static byte pct10 =10;
final static byte pct20 =0;
public void CPU_event()
{
}
public int get_CPU_event()
{
Random random_X =new Random();
event = random_X.nextInt(1000)+1;
if((event % pct10) ==0)
rangeResult =1;
else
if((event % pct13) ==0)
rangeResult =2;
else
if((event % pct30) ==0)
rangeResult =3;
else
if((event % pct40) ==0)
rangeResult =4;
else
rangeResult =0;
return rangeResult;
}
}
processMgmt Class
import java.util.LinkedList;
import java.util.Random;
public class ProcessMgmt
{
public static void main(String args[])
{
int QREADY____T =25;
final int BLOCKIO =3;
final int INTERRUPT =2;
final int COMPLETED =1;
final int BLOCKPAGE =0;
Random random__X =newRandom();
CPU_event event =newCPU_event();
int CPU_runtime;
int event__X;
LinkedLIST<PCB> QREADY = new LinkedList<PCB>();
PCB PCB_Ready;
for(int ii =0; ii<QREADY___T;ii++)
{
QReady.add(new PCB());
}
System.out.println(" Ready Queue");
for(PCB pcbLoop:QReady)
System.out.println(pcbloop.showPCB());
while(QReady.size()>0)
{
PCB_Running =QReady.removeFirst();
PCB_running.set_state("Running");
CPU_runtime =random_X.nextInt(20)+1;
PCB_Running.add_Cpu_used(CPU_runtime);
System.out.println(" Running process");
System.out.println(PCB_Running.showPCB());
for(PCB pcbLoop :QReady)
{
pcbLoop.add_timeWaiting(CPU_runtime);
}
if(PCB_Running.get_CPU_used() >PCB_Running.get_CPU_max())
PCB_Running.set_state("Completed")
System.out.println(" tprocess" + PCB_Running.showPCB() + " ");
}
event__X =event.get_CPU_event();
if (event__X ==COMPLETED)
{
PCB_Running.set_state("Completed")
System.out.println(" tprocess" + PCB_Running.showPCB() + " ");
}
else
{
PCB_Running.set_state("Ready");
switch (event_X)
{
case INTERRUPT
{
QREADY.addFirst(PCB_Running);
System.out.println(" ");
break;
}
case BLOCKPAGE
{
int top3rd = QReady.size()/3;
QReady.add(top3rd,PCB_Running);
System.out.println(" ");
break;
}
QReady.size case BLOCKIO :
{
int topHalf = QReady.size()/2;
QReady.add(topHalf,PCB_Running);
System.out.println(" ");
break;
}
default:
{
QReady.addLast(PCB_Running);
System.out.println(" ");
break;
}
}
}
System.out.println(" ");
for(PCB pcbLoop: QReady)
System.out.println(pcbLoop.showPCB());
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.