# Analog processor scheduling

I. internship content

Select a scheduling algorithm to achieve processor scheduling.

II. Purpose of practice

In a multiprogramming system, there are often several processes in readiness at the same time. When the number of ready processes is greater than the number of processors, it is necessary to determine which processes occupy the processor first according to some policy. This internship simulates the processor scheduling in the case of a single processor to help students better understand the work of processor scheduling.

III. practice topic

There are two questions in this internship. Students can choose one of them to do the internship.

Problem one: design a program to schedule the processor according to the priority number scheduling algorithm.

[hint]:

(1) suppose the system has five processes, each process is represented by a process control block PCB, and the format of the process control block is:

 Process name Pointer Required operation time Priority number state

Where, process name -- as the identification of the process, assume that the process names of the five processes are P1, P2, P3, P4, P5 respectively.

Pointer -- queue five processes according to the priority number, point out the first address of the process control block of the next process with the pointer, and the pointer in the last process is "0".

Run time required - assumes the number of units of time a process needs to run.

Priority number - the priority number given to a process. When scheduling, the process with a large priority number is always selected to execute first.

State - it can be assumed that there are two states, ready and end. The initial state of five processes is "ready", which is represented by "R". When a process finishes running, its state is "end", which is represented by "E".

(2) before running the processor scheduler you designed, determine its "priority number" and "required running time" for each process.

(3) for the convenience of scheduling, five processes are queued according to the given priority number. Use a unit to indicate the first process of the queue and a pointer to indicate the connection of the queue. Example:

K2

 K1 P1 K2 P2 K3 P3 K4 P4 K5 P5 0 K4 K5 K3 K1 2 3 1 2 4 1 5 3 4 2 R R R R R PCB1 PCB2 PCB3 PCB4 PCB5

(4) processor scheduling always selects the first process to run. By changing the priority number dynamically, the priority number will be reduced by "1" every time the process runs. As this internship is to simulate processor scheduling, the selected process is not actually started, but executed:

Priority number -1

Required operation Time-1

To simulate a run of a process.

It should be noted that in the actual system, when a process is selected to run, it must restore the process site and let it occupy the processor to run until a waiting event occurs or the run ends. These jobs are omitted here.

(5) after the process runs once, if it is required to run time ¹ 0, then it will be added to the queue (inserted according to the priority number and set the queue head flag); if it is required to run time = 0, then its status will be changed to "end" (E) and exit the queue.

(6) if the process queue in the ready state is not empty, repeat the steps in (4) and (5) above until all processes are in the end state.

(7) there should be display or print statement in the designed program, which can display or print the process name of each selected process and the change of process queue after running once.

(8) determine a set of "priority number" and "required running time" for any of the five processes, start the designed processor scheduler, display or print the process name of the selected process and the dynamic change process of the process control block.

```package System;

public class Pcb{
private String name;    //Process name
private int time;       //Process time
private int priorty;    //Priority number;
private String status;  //state
private Pcb pointer;    //Pointer

public Pcb(String name, int time, int priorty, String status, Pcb pointer) {
this.name = name;
this.time = time;
this.priorty = priorty;
this.status = status;
this.pointer = pointer;
}
public Pcb(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pcb getPointer() {
return pointer;
}
public void setPointer(Pcb pointer) {
this.pointer = pointer;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getPriorty() {
return priorty;
}
public void setPriorty(int priorty) {
this.priorty = priorty;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

//Display process
public void show() {
System.out.println("process" + name + ", Pointer->" + pointer.getName() + ", Process time=" + time + "," +
" Priority number=" + priorty+ ", state=" + status );
}
}```
```package System;

import java.util.Iterator;

public class Operate {
Pcb nullPcb = new Pcb("0", 0, 0, "E", null);//Create an empty PCB object

//Establish PCB process
public void CreatePcb(int n){
for(int i=1;i<=n;i++){
Pcb pcb=new Pcb();
pcb.setName("p"+i);
int priorty=(int)(Math.random()*10);    //Generate random priority number
int time=(int)(Math.random()*10)+1;        //Generate random time
pcb.setPriorty(priorty);
pcb.setTime(time);
pcb.setPointer(nullPcb);
pcb.setStatus("R");
}
}

//display PCB queue
public void showPcb(){
Iterator<Pcb> p=TotalPcb.iterator();
while(p.hasNext()){
Pcb pcb=p.next();
pcb.show();
}
System.out.println();
}

//delete PCB End of run process in queue
public void remove(){
for(int i=0;i<TotalPcb.size();i++){
if(TotalPcb.get(i).getStatus()=="E"){
System.out.println(TotalPcb.get(i).getName()+"End of operation,Exit queue");
TotalPcb.remove(i);
}
}
}

//Priority number after operation-1，time-1
public void renew(Pcb p) {
if (p.getPriorty()>0) {
p.setPriorty(p.getPriorty()-1);  //Priority number-1
}
if (p.getTime()>0) {
p.setTime(p.getTime()-1);        //time-1
}
if (p.getTime()==0){
p.setStatus("E");            //If time=0，Status setting E
}
}

//Queue sort
public void sortPcb() {
for(int i=0;i<TotalPcb.size();i++) {
for(int j=i+1;j<TotalPcb.size();j++) {
if(TotalPcb.get(i).getPriorty()<TotalPcb.get(j).getPriorty()) {
//First run with high priority
Pcb temp=TotalPcb.get(i);
TotalPcb.set(i, TotalPcb.get(j));
TotalPcb.set(j, temp);
}else if(TotalPcb.get(i).getPriorty()==TotalPcb.get(j).getPriorty()&&TotalPcb.get(i).getTime()>TotalPcb.get(j).getTime()) {
//Run first with the same priority number and the least time
Pcb temp=TotalPcb.get(i);
TotalPcb.set(i, TotalPcb.get(j));
TotalPcb.set(j, temp);
}
}
}
//Process pointer to next process
for (int k =0;k<TotalPcb.size()-1; k++) {
TotalPcb.get(k).setPointer(TotalPcb.get(k+1));
}
//The last process points to 0
if(TotalPcb.size()!=0){
TotalPcb.get(TotalPcb.size()-1).setPointer(nullPcb);
}
}

//Process operation
public void run(){
sortPcb();   //sort
System.out.println("********************************************************");
while(TotalPcb.size()!=0){
System.out.println("Pre run process queue:");
showPcb();  //Display queue
System.out.println(TotalPcb.get(0).getName()+"Process running time-1，priority-1");
renew(TotalPcb.get(0));
System.out.print(TotalPcb.get(0).getName()+"After the process runs:");
TotalPcb.get(0).show();
remove();  //delete PCB End of run process in queue
sortPcb();
System.out.println("\n Post run process queue:");
showPcb();
System.out.println("********************************************************");
}
System.out.println("All processes completed");

}
}```
```package System;

import java.util.Scanner;

public class Test
{
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
Operate op=new Operate();
System.out.print("Please enter the number of processes:");
op.CreatePcb(input.nextInt());
op.run();
}
}```

Tags: Java P4

Posted on Fri, 08 Nov 2019 08:57:25 -0500 by Roble