Process Management Simulation-time slice rotation method and priority number method. Comments are welcome.

Source: Internet
Author: User

Time slice rotation method:

PCB. Ava

Public class PCB {private string name; private int PRI; private int runtime; // run time private int needtime; private string state; // R indicates run, F indicates completion, W indicates waiting for private int counter; // counter public PCB (string name, int counter, int runtime, int needtime, string state) {This. name = Name; this. counter = counter; this. runtime = runtime; this. needtime = needtime; this. state = State;} public void Info () {/* system. out. println ("process name" + "" + "RunTime" + "" + "needtime" + "" + "counter" + "status "); */system. out. println (this. name + "" + this. runtime + "" + this. needtime + "" + this. counter + "" + this. state);} public void run () {} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getpri () {return PRI;} public void setpri (int pri) {This. PRI = PRI;} public int getneedtime () {return needtime;} public void setneedtime (INT needtime) {This. needtime = needtime;} Public String getstate () {return state;} public void setstate (string state) {This. state = State;} public int getruntime () {return runtime;} public void setruntime (INT runtime) {This. runtime = runtime;} public int getcounter () {return counter;} public void setcounter (INT counter) {This. counter = counter ;}}

Timepieces. Java

Import Java. util. shortlist; public class timepieces {static shortlist <PCB> readypcb = new shortlist <PCB> (); static shortlist <PCB> finishpcb = new shortlist <PCB> (); static int distinct COUNT = (INT) (math. random () * 10); static int cuptime = (INT) (math. random () * 5 + 1); Public static void main (string [] ARGs) {creatpcb (); system. out. println ("time slice:" + cuptime); system. out. println ("A total of" + distinct count + "processes are generated "); Showlist (readypcb); runpcb ();} public static void creatpcb () {for (INT I = 0; I <distinct count; I ++) {int needtime = (INT) (math. random () * 10 + 1); string name = I + "; PCB = new PCB (name, 0, 0, needtime," W "); readypcb. add (PCB) ;}} public static void runpcb () {While (! Readypcb. isempty () {readypcb. get (0 ). setstate ("R"); int runtime = readypcb. get (0 ). getruntime (); int needtime = readypcb. get (0 ). getneedtime (); int counter = readypcb. get (0 ). getcounter (); readypcb. get (0 ). setruntime (runtime + 1); readypcb. get (0 ). setneedtime (needtime-1); readypcb. get (0 ). setcounter (counter + 1); system. out. println ("ready queue:"); showlist (readypcb);/* add it to the completed queue after running, and remove */If (readypcb. get (0 ). getneedtime () = 0) {finishpcb. add (readypcb. get (0); readypcb. get (0 ). setstate ("F"); readypcb. remove (0);} else if (readypcb. get (0 ). getcounter () = cuptime) {PCB = readypcb. get (0); PCB. setcounter (0); PCB. setstate ("W"); readypcb. addlast (PCB); readypcb. remove (0);} system. out. println ("completed queue:"); showlist (finishpcb) ;}} public static void showlist (complete list <PCB> PCB) {system. out. println ("process name" + "" + "RunTime" + "" + "needtime" + "" + "counter" + "status "); for (Int J = 0; j <PCB. size (); j ++) {PCB. get (j ). info ();} system. out. println ("");}}

Priority: Priority = 50-running time

Each running time, the priority value is reduced by 3 to compete again.

PCB. Java

Public class PCB {private string name; private int PRI; private int runtime; // run time private int needtime; private string state; // R indicates run, F indicates completion, W indicates waiting for private int counter; // counter public PCB (string name, int counter, int runtime, int needtime, int PRI, string state) {This. name = Name; this. counter = counter; this. runtime = runtime; this. needtime = needtime; this. PRI = PRI; this. state = State;} public void Info () {/* system. out. println ("process name" + "" + "RunTime" + "" + "needtime" + "" + "counter" + "status "); */system. out. println (this. name + "" + this. runtime + "" + this. needtime + "" + this. counter + "" + this. PRI + "" + this. state);} public void run () {} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int getpri () {return PRI;} public void setpri (int pri) {This. PRI = PRI;} public int getneedtime () {return needtime;} public void setneedtime (INT needtime) {This. needtime = needtime;} Public String getstate () {return state;} public void setstate (string state) {This. state = State;} public int getruntime () {return runtime;} public void setruntime (INT runtime) {This. runtime = runtime;} public int getcounter () {return counter;} public void setcounter (INT counter) {This. counter = counter ;}}

Priprocess. Java

Import Java. util. permission list; public class priprocess {static permission list <PCB> readypcb = new permission list <PCB> (); static permission list <PCB> finishpcb = new permission list <PCB> (); static int distinct COUNT = (INT) (math. random () * 10); public static void main (string [] ARGs) {system. out. println ("A total of" + running count + "processes"); createpcb (); showlist (readypcb); runpcb ();} public static void createpcb () {for (INT I = 0; I <PCBC Ount; I ++) {int needtime = (INT) (math. random () * 10 + 1); string name = I + "; PCB = new PCB (name, 0, 0, needtime, 50-needtime," W "); readypcb. add (PCB) ;}} public static void runpcb () {While (! Readypcb. isempty () {system. out. println ("ready queue:"); sortlist (readypcb); int counter = readypcb. get (0 ). getcounter (); int pri = readypcb. get (0 ). getpri (); int needtime = readypcb. get (0 ). getneedtime (); int runtime = readypcb. get (0 ). getruntime (); readypcb. get (0 ). setstate ("R"); showlist (readypcb); readypcb. get (0 ). setcounter (counter + 1); readypcb. get (0 ). setneedtime (needTime-1); readypcb. get (0 ). setpri (pri-3); readypcb. get (0 ). setruntime (runtime + 1);/* add it to the Completion queue after running, and remove */If (readypcb. get (0 ). getneedtime () = 0) {readypcb. get (0 ). setstate ("F"); readypcb. get (0 ). setcounter (0); finishpcb. add (readypcb. get (0); readypcb. remove (0);} sortlist (readypcb); system. out. println ("completed queue:"); showlist (finishpcb) ;}}/* bubble sort */public static void sortlist (sorted list <PCB> PCB) {int I, j, k; int lenth = PCB. size (); for (I = 0; I <lenth-1; I ++) {k = I; for (j = I + 1; j <lenth; j ++) {If (PCB. get (j ). getpri ()> PCB. get (k ). getpri () {pcb j = PCB. get (j); PCB pcbk = PCB. get (k); PCB. remove (k); PCB. add (K, J); PCB. remove (j); PCB. add (J, pcbk) ;}}} public static void showlist (PCB) {system. out. println ("process name" + "" + "RunTime" + "" + "needtime" + "" + "counter" + "" + "Priority" + "" + "+" status "); for (int c = 0; C <PCB. size (); C ++) {PCB. get (c ). info ();} system. out. println ("");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.