Five algorithms for process scheduling of computer operating system __ algorithm

Source: Internet
Author: User


The five algorithms of computer operating system process scheduling are: 1. First come first service Algorithm 2. Short job priority algorithm 3. High response ratio precedence algorithm 4. Time slice Rotation Method 5. Multilevel Feedback Queue algorithm

public class Jcdu {
	String name;
	int id;
	int enter_time;
	int ser_time;
	Jcdu () {}
	void SetName (String aname) {
		this.name=aname;
	}
	void Setenter_time (int time) {
		this.enter_time=time
	}
	void Setser_time (int time) {
		this.ser_time=time
	}
	void SetID (int id) {
		this.id=id
	}
	String GetName () {return
		this.name;
	}
	int Getenter_time () {return
		this.enter_time;
	}
	int Getser_time () {return
		this.ser_time;
	}
	int GetID () {return
		this.id
	}}

1. First come first service algorithm: mainly for each scheduling job is from the standby ready queue is to select one or more of the first entry into the queue of jobs. You can set a static variable to gradually increment the static variable and use a list container to determine the jobs in the ready queue per second. When a job completes, the first job to enter the queue is selected from the list container. Loop until all jobs have finished executing.

/* First Come First service * * class test{public static int run_time=0;
		public static void Main (string[] args) {JCDU [] temp=new jcdu[5];
		Scanner in=new Scanner (system.in);
		int pro_count=0;
		int do_time=0;
		int en_time;
		int do_id=0;
		String Name=null;
		System.out.println ("Please enter process information:");
			for (int i=0;i<5;i++) {SYSTEM.OUT.PRINTLN ("process id" +i);
			Temp[i]=new JCDU ();
			Temp[i].setid (i);
			System.out.print ("Process name:");
			Temp[i].setname (In.next ());
			System.out.print ("Arrival time:");
			Temp[i].setenter_time (In.nextint ());
			System.out.print ("Service Time:");
			
		Temp[i].setser_time (In.nextint ());
		} List <jcdu>li=new arraylist<jcdu> ();
		for (Jcdu x:temp) {li.add (x);
			} while (true) {int J;
			En_time=integer.max_value;
					for (Jcdu X:li) {if (X.getenter_time () < En_time) {//Find an Execution process do_id=x.getid ();
					Do_time=x.getser_time ();
					Name=x.getname ();
				
				En_time=x.getenter_time (); } if (En_time>run_time) {System.out.println ("No process arrives at this moment");
				run_time++;
							} else{for (j=0;j<do_time;j++) {for (Jcdu X:li) {if (X.getenter_time () ==run_time) {
						SYSTEM.OUT.PRINTLN ("Process" +x.getname () + "in the first" +run_time+ "seconds to arrive");
					} System.out.println ("First" +run_time+ "seconds" + "execution process" +name);
						try {thread.currentthread (). Sleep (1000);
					catch (Interruptedexception e) {e.printstacktrace ();
					
					
					} run_time++;
					} if (J==do_time) {System.out.println (name+ "Process Execution End");
				} pro_count++;
					for (int i=0;i<li.size (); i++) {if (Li.get (i). GetID () ==do_id) {li.remove (i);
				} if (pro_count==5) break;
				} try {Thread.CurrentThread (). Sleep (1000);
			catch (Interruptedexception e) {e.printstacktrace ();
		 } System.out.println ("Process information");
		
		Show (temp);
	   public static void Show (JCDU [] other) {System.out.printf ("%-5s%-5s%-5s%-5s\n", "Process ID", "Process name", "Arrival Time", "Service Time"); for (Jcdu x:other) {System.out.printf ("%-5d%-5s%-5s%-5s\n", X.getid (), X.getname (), X.getenter_time (), X.getser_time ()); }
	}
}

2. Short job priority algorithm: mainly for each scheduling job is from the standby ready queue is to select one or more requirements of the shortest service time of the job. You can set a static variable to gradually increment the static variable and use a list container to determine the jobs in the ready queue per second. When a job is executed, the shortest service time from the list container is selected for the job entering the queue. Loop until all jobs have finished executing.

Class test{public static int run_time=0;
		public static void Main (string[] args) {JCDU [] temp=new jcdu[5];
		Scanner in=new Scanner (system.in);
		int pro_count=0;
		int ser_time;
		int en_time=0;
		int do_id=0;
		String Name=null;
		System.out.println ("Please enter process information:");
			for (int i=0;i<5;i++) {SYSTEM.OUT.PRINTLN ("process id" +i);
			Temp[i]=new JCDU ();
			Temp[i].setid (i);
			System.out.print ("Process name:");
			Temp[i].setname (In.next ());
			System.out.print ("Arrival time:");
			Temp[i].setenter_time (In.nextint ());
			System.out.print ("Service Time:");
			
		Temp[i].setser_time (In.nextint ());
		} List <jcdu>li=new arraylist<jcdu> ();
		for (Jcdu x:temp) {li.add (x);
			} while (true) {int J;
			Ser_time=integer.max_value; for (Jcdu X:li) {if (X.getenter_time () <=run_time && x.getser_time () < Ser_time) {//Find an execution process do_id
					=x.getid ();
					Ser_time=x.getser_time ();
					Name=x.getname ();
				En_time=x.getenter_time (); } if (en_time>run_time) {SysTEM.OUT.PRINTLN ("No process arrives at present");
			run_time++; } else{for (j=0;j<ser_time;j++) {//Based on Execution time for (Jcdu X:li) {if (X.getenter_time () ==run_time
						{System.out.println ("process" +x.getname () + "+run_time+" in the first "seconds");
					} System.out.println ("First" +run_time+ "seconds" + "execution process" +name);
						try {thread.currentthread (). Sleep (1000);
					catch (Interruptedexception e) {e.printstacktrace ();
					
					} run_time++;
					} if (J==ser_time) {System.out.println (name+ "Process Execution End");
				} pro_count++;
					for (int i=0;i<li.size (); i++) {if (Li.get (i). GetID () ==do_id) {li.remove (i);
				} if (pro_count==5) break;
				} try {Thread.CurrentThread (). Sleep (1000);
			catch (Interruptedexception e) {e.printstacktrace ();
		 } System.out.println ("Process information");
		
		Show (temp);
	   public static void Show (JCDU [] other) {System.out.printf ("%-5s%-5s%-5s%-5s\n", "Process ID", "Process name", "Arrival Time", "Service Time"); For (jCDU x:other) {System.out.printf ("%-5d%-5s%-5s%-5s\n", X.getid (), X.getname (), X.getenter_time (), X.getser_time ()); }
	}
}

3. High response ratio priority algorithm: mainly for each dispatch job is from the standby ready queue is to select one or more response than the highest job. You can set a static variable to gradually increment the static variable, use a list container to determine the job in the ready queue per second, and calculate the response per second for the job {(Wait time + required service time)/request service Time}. When a job completes, the most responsive job is selected from the list container. Loop until all jobs have finished executing.

Class test{public static int run_time=0;
		public static void Main (string[] args) {JCDU [] temp=new jcdu[5];
		Scanner in=new Scanner (system.in);
		int pro_count=0;
		int ser_time=0;
		int en_time=0;
		
		int do_id=0;
		String Name=null;
		System.out.println ("Please enter process information:");
			for (int i=0;i<5;i++) {SYSTEM.OUT.PRINTLN ("process id" +i);
			Temp[i]=new JCDU ();
			Temp[i].setid (i);
			System.out.print ("Process name:");
			Temp[i].setname (In.next ());
			System.out.print ("Arrival time:");
			Temp[i].setenter_time (In.nextint ());
			System.out.print ("Service Time:");
			
		Temp[i].setser_time (In.nextint ());
		} List <jcdu>li=new arraylist<jcdu> ();
		for (Jcdu x:temp) {li.add (x);
			} while (true) {int J;
			Double Wei=double.min_value;
			for (Jcdu X:li) {x.setweight (run_time); for (Jcdu X:li) {if (X.getenter_time () <=run_time && x.getweight () >wei) {//Find an execution process do_id=x.
					GetID ();
					Ser_time=x.getser_time ();
					Name=x.getname (); En_time=x.getenter_time ();
				Wei=x.getweight ();
				} if (En_time>run_time) {System.out.println ("No process arrives at this moment");
			run_time++; } else{for (j=0;j<ser_time;j++) {//Based on Execution time for (Jcdu X:li) {if (X.getenter_time () ==run_time
						{System.out.println ("process" +x.getname () + "+run_time+" in the first "seconds");
					} System.out.println ("First" +run_time+ "seconds" + "execution process" +name);
						try {thread.currentthread (). Sleep (1000);
					catch (Interruptedexception e) {e.printstacktrace ();
					
					} run_time++;
					} if (J==ser_time) {System.out.println (name+ "Process Execution End");
				} pro_count++;
					for (int i=0;i<li.size (); i++) {if (Li.get (i). GetID () ==do_id) {li.remove (i);
				} if (pro_count==5) break;
				} try {Thread.CurrentThread (). Sleep (1000);
			catch (Interruptedexception e) {e.printstacktrace ();
		 } System.out.println ("Process information");
		
		Show (temp); public static void Show (JCDU [] other) {System.out.printf ("%-5s%-5s%-5s%-5s\n", "Process ID", "Process name", "Arrival Time", "Service Time"); for (Jcdu x:other) {System.out.printf ("%-5d%-5s%-5s%-5s\n", X.getid (), X.getname (), X.getenter_time (), X.getser_time (
	 )); }
	}
}

4. Time slice rotation algorithm: mainly for each scheduling job is from the standby ready queue is to select one or more of the first entry into the queue of jobs. Each time slice is assigned to the first element of the queue, and when the time slice is finished, the process is then put to the end of the line. Loop until all jobs have finished executing.

Class test{public static int run_time=0;
		public static void Main (string[] args) {JCDU [] temp=new jcdu[5];
		Scanner in=new Scanner (system.in);
		int pro_count=0;
		int en_time=0;
		int time_p=0;
		String Name=null;
		System.out.println ("Please enter process information:");
			for (int i=0;i<5;i++) {SYSTEM.OUT.PRINTLN ("process id" +i);
			Temp[i]=new JCDU ();
			Temp[i].setid (i);
			System.out.print ("Process name:");
			Temp[i].setname (In.next ());
			System.out.print ("Arrival time:");
			Temp[i].setenter_time (In.nextint ());
			System.out.print ("Service Time:");
			
		Temp[i].setser_time (In.nextint ());
		System.out.println ("Please enter the length of the time Slice:");
		Time_p=in.nextint ();
		List <jcdu>li=new arraylist<jcdu> ();
		     while (true) {int J;
		     int i;
		     int q=0;
		    		    for (i=0;i<temp.length;i++) {if (temp[i].getenter_time () = = Run_time)//To interpret if there is a process entering the queue {
				        SYSTEM.OUT.PRINTLN (run_time+ "process" +temp[i].getname () + "Enter ready queue");
		    	      Li.add (Temp[i]);		        
		  }       } if (Li.isempty ()) {System.out.println (run_time+ "no executable process");
		    	 else {JCDU te=li.get (0);
		    	 Li.remove (0);
						for (j=0;j<time_p;j++) {name=te.getname ();
						System.out.println ("First" +run_time+ "seconds" + "execution process" +name);
						Te.setdoingtime ();
								try {thread.currentthread (). Sleep (1000);
							catch (Interruptedexception e) {e.printstacktrace ();
							} if (Te.com ()) {pro_count++;
							System.out.println ("First" +run_time+ "seconds" + "process" +name+ "execution End");
								if (Li.isempty ()) {System.out.println ("Ready queue is empty");
						    		     for (i=0;i<temp.length;i++) {if (temp[i].getenter_time () = = Run_time)//to interpret whether a process has entered the queue
								        {System.out.println (run_time+ "process" +temp[i].getname () + "Enter ready queue");
						    	         Li.add (Temp[i]);
}		        
						           }
							    }							else {te=li.get (0);
							   Li.remove (0);
						}} run_time++;
							System.out.print ("Ready queue:");
							   for (Jcdu X:li) {System.out.print (X.getname () + "");
		    	        } System.out.println ("");
		    	 } if (!te.com ()) Li.add (TE);
				    SYSTEM.OUT.PRINTLN ("Completion of the time sheet");
		         System.out.println ();
			
		} if (pro_count==5) break;
			}/*int J;
			int q=0;
			Double Wei=double.min_value;
			En_time=integer.max_value; for (int i=0;i<temp.length;i++) {if (temp[i].getenter_time () = = Run_time) {//interpret if there is a process entering the queue for (JCDU x		   
						: Li) {if (X.getname ()!=temp[i].getname ()) q++;
				        } if (Q==li.size ()) {Li.add (temp[i]);
			} System.out.print ("Ready queue:");
			   for (Jcdu X:li) {System.out.print (X.getname () + "");
			} System.out.println ("");
			 if (Li.isempty ()) {   System.out.println ("Current Ready queue is empty");
			    run_time++;
				else{JCDU te=li.get (0);                     Li.remove (0);
					Remove the currently executing team first element for (j=0;j<time_p;j++) {name=te.getname ();
					System.out.println ("First" +run_time+ "seconds" + "execution process" +name);
							try {thread.currentthread (). Sleep (1000);
						catch (Interruptedexception e) {e.printstacktrace ();            } te.setdoingtime ();
					   The time already executed if (te.com ()) {//has already been executed System.out.println the team head element (name+ "Process Execution End");
					   pro_count++;
				            if (Li.isempty ()) {System.out.println ("Ready queue is empty");
				          } else{te=li.get (0);
				         Li.remove (0);
				    }} run_time++; for (int pi=0;pi<temp.length;pi++) {if (temp[pi].getenter_time () = = Run_time) {//interpret next second if there is a process entering the queue S
							Ystem.out.println ("First" +run_time+ "second Arrival" +temp[pi].getname ());
							Li.add (Temp[pi]); }
				         } 
			      } if (!te.com ())//If the time slice end process has not executed the line end to add the team tail element Li.add (TE);
			     
			  } if (pro_count==5) break; }*/} public static void Show (JCDU [] other) {System.out.printf ("%-5s%-5s%-5s%-5s\n", "Process ID", "Process name", "" on arrival
	   "," "Service Time"); for (Jcdu x:other) {System.out.printf ("%-5d%-5s%-5s%-5s\n", X.getid (), X.getname (), X.getenter_time (), X.getser_time (
	     )); }
    }
}

5. Multi-level Feedback queue algorithm: Set up multiple queues, the time slice length of each queue is different, when the job is not completed in Queue 1, and so on into the two-tier queue. Loop until all jobs have finished executing.

Class test{public static int run_time=0;          Run time static int pro_count=0;                  Process completion number int ser_time=0;                     Require service time int en_time;
    Arrival time String Name=null;  Static JCDU [] temp={new Jcdu ("A", 1,0,3), New Jcdu ("B", 2,2,6), New Jcdu ("C", 3,4,4), New Jcdu ("D", 4,6,5), new
    JCDU ("E", 5,8,2)};
		public static void Main (string[] args) {duojiqueue ();
		public static void Duojiqueue () {List <jcdu> li1=new arraylist<jcdu> ();
		List <jcdu> li2=new arraylist<jcdu> ();  
	    Map<integer,list <jcdu> > Tmap=new hashmap<integer,list <jcdu>> ();
	    Tmap.put (2,LI1);
	    Tmap.put (4,LI2); while (pro_count!=5) {for (int i=0;i<temp.length;i++) {if (temp[i].getenter_time () = = Run_time)/
	    			/interpretation of whether there are processes entering the queue {System.out.println (run_time+ "SEC Process" +temp[i].getname () + "Enter ready queue 1");
	    		Li1.add (Temp[i]); }} set<integer> Keys=tmap.keySet ();
	  	    Iterator<integer> I=keys.iterator ();
	  	    	while (I.hasnext ()) {int key=i.next ();
	  	    	List <jcdu> Li=tmap.get (key);
	  	    	System.out.print ("Ready queue" +KEY/2);
	  		         if (!li.isempty ()) {for (Jcdu R:li) System.out.print (R.getname ());
	  	    } System.out.println ();
	  	    	while (!li1.isempty ()) {JCDU r=li1.get (0);
	  	    	Li1.remove (R);
	    			  for (int j=0;j<2;j++) {System.out.println ("Current time slice execution process" +r.getname ());
	    			   R.addser_time ();
							try {thread.currentthread (). Sleep (1000);
						catch (Interruptedexception e) {e.printstacktrace ();
	    				  } if (R.isequ ()) {System.out.println ("Process Execution End" +r.getname ());
	    				  pro_count++;
	    			  Break
	    		   } run_time++; for (int w=0;w<temp.length;w++) {if (temp[w].getenter_time () = = Run_time)//Interpreting if there is a process in progressInto the queue {System.out.println (run_time+ "process" +temp[w].getname () + "Enter ready queue 1");
		  	    		    Li1.add (Temp[w]);
	  	    	}} if (!r.isequ ()) {Li2.add (R);
		  	    } keys=tmap.keyset ();
		  	    I=keys.iterator ();
		  	    	while (I.hasnext ()) {int key=i.next ();
		  	    	 List <jcdu> Li=tmap.get (key);
		  	    	System.out.print ("Ready queue" +KEY/2);
		  		         if (!li.isempty ()) {for (Jcdu Rw:li) System.out.print (Rw.getname ());
		  	    } System.out.println ();
	  	    	} while (!li2.isempty ()) {JCDU r=li2.get (0);
	  	    	Li2.remove (R);
	    			  for (int j=0;j<4;j++) {System.out.println ("Current time slice execution process" +r.getname ());
	    			   R.addser_time ();
							try {thread.currentthread (). Sleep (1000);
						catch (Interruptedexception e) {e.printstacktrace ();
	    	} if (R.isequ ()) {			  SYSTEM.OUT.PRINTLN ("Process Execution Complete" +r.getname ());
	    				  pro_count++;
	    			  Break
	    		   } run_time++;
		  	    		    for (int w=0;w<temp.length;w++) {if (temp[w].getenter_time () = = Run_time)//To interpret if there is a process entering the queue
		  	    			{System.out.println (run_time+ "process" +temp[w].getname () + "Enter ready queue 1");
		  	    		    Li1.add (Temp[w]);
	  	    	}} if (!r.isequ ()) {Li2.add (R);
		  	    } keys=tmap.keyset ();
		  	    I=keys.iterator ();
		  	    	while (I.hasnext ()) {int key=i.next ();
		  	    	List <jcdu> Li=tmap.get (key);
		  	    	System.out.print ("Ready queue" +KEY/2);
		  		         if (!li.isempty ()) {for (Jcdu Rw:li) System.out.print (Rw.getname ());
		  	    } System.out.println ();
   }} System.out.println (Run_time);
 }
}



Related Article

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.