計算機操作系統進程調度五種算法爲:1.先來先服務算法 2.短作業優先算法 3.高響應比優先算法 4.時間片輪轉法 5.多級反饋隊列算法
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.先來先服務算法:主要爲每次調度作業就是從後備的就緒隊列中就是選擇一個或者多個最先進入該隊列的作業。可以設置一個靜態變量,讓靜態變量逐漸遞增,用一個List容器判斷每秒中進入就緒隊列中的作業。當一個作業執行完畢,再從List容器選出第一個進入該隊列的作業。一直循環到所有作業執行完畢。
/*先來先服務*/ 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("請輸入進程信息:");
for(int i=0;i<5;i++){
System.out.println("進程id"+i);
temp[i]=new jcdu();
temp[i].setid(i);
System.out.print("進程名:");
temp[i].setname(in.next());
System.out.print("到達時間:");
temp[i].setenter_time(in.nextInt());
System.out.print("服務時間:");
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){ //找出一個執行進程
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("當前沒有進程到達");
run_time++;
}
else{
for(j=0;j<do_time;j++){
for(jcdu x:li){
if(x.getenter_time()==run_time){
System.out.println("進程"+x.getname()+"在第"+run_time+"秒到達");
}
}
System.out.println("第"+run_time+"秒"+"執行進程"+name);
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
run_time++;
}
if(j==do_time){
System.out.println(name+"進程執行結束");
}
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("進程信息");
show(temp);
}
public static void show(jcdu [] other){
System.out.printf("%-5s %-5s %-5s %-5s\n","進程id","進程名","到達時間","服務時間");
for(jcdu x:other){
System.out.printf("%-5d %-5s %-5s %-5s\n",x.getid(),x.getname(),x.getenter_time(),x.getser_time());
}
}
}
2.短作業優先算法:主要爲每次調度作業就是從後備的就緒隊列中就是選擇一個或者多個要求服務時間最短的作業。可以設置一個靜態變量,讓靜態變量逐漸遞增,用一個List容器判斷每秒中進入就緒隊列中的作業。當一個作業執行完畢,再從List容器選出服務時間最短的進入該隊列的作業。一直循環到所有作業執行完畢。
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("請輸入進程信息:");
for(int i=0;i<5;i++){
System.out.println("進程id"+i);
temp[i]=new jcdu();
temp[i].setid(i);
System.out.print("進程名:");
temp[i].setname(in.next());
System.out.print("到達時間:");
temp[i].setenter_time(in.nextInt());
System.out.print("服務時間:");
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){ //找出一個執行進程
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("當前沒有進程到達");
run_time++;
}
else{
for(j=0;j<ser_time;j++){ //根據執行時間
for(jcdu x:li){
if(x.getenter_time()==run_time){
System.out.println("進程"+x.getname()+"在第"+run_time+"秒到達");
}
}
System.out.println("第"+run_time+"秒"+"執行進程"+name);
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
run_time++;
}
if(j==ser_time){
System.out.println(name+"進程執行結束");
}
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("進程信息");
show(temp);
}
public static void show(jcdu [] other){
System.out.printf("%-5s %-5s %-5s %-5s\n","進程id","進程名","到達時間","服務時間");
for(jcdu x:other){
System.out.printf("%-5d %-5s %-5s %-5s\n",x.getid(),x.getname(),x.getenter_time(),x.getser_time());
}
}
}
3.高響應比優先算法:主要爲每次調度作業就是從後備的就緒隊列中就是選擇一個或者多個響應比最高的作業。可以設置一個靜態變量,讓靜態變量逐漸遞增,用一個List容器判斷每秒中進入就緒隊列中的作業,並且每秒計算該作業的響應比{(等待時間+要求服務時間)/要求服務時間}。當一個作業執行完畢,再從List容器選出響應比最高的作業。一直循環到所有作業執行完畢。
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("請輸入進程信息:");
for(int i=0;i<5;i++){
System.out.println("進程id"+i);
temp[i]=new jcdu();
temp[i].setid(i);
System.out.print("進程名:");
temp[i].setname(in.next());
System.out.print("到達時間:");
temp[i].setenter_time(in.nextInt());
System.out.print("服務時間:");
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){ //找出一個執行進程
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("當前沒有進程到達");
run_time++;
}
else{
for(j=0;j<ser_time;j++){ //根據執行時間
for(jcdu x:li){
if(x.getenter_time()==run_time){
System.out.println("進程"+x.getname()+"在第"+run_time+"秒到達");
}
}
System.out.println("第"+run_time+"秒"+"執行進程"+name);
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
run_time++;
}
if(j==ser_time){
System.out.println(name+"進程執行結束");
}
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("進程信息");
show(temp);
}
public static void show(jcdu [] other){
System.out.printf("%-5s %-5s %-5s %-5s\n","進程id","進程名","到達時間","服務時間");
for(jcdu x:other){
System.out.printf("%-5d %-5s %-5s %-5s\n",x.getid(),x.getname(),x.getenter_time(),x.getser_time());
}
}
}
4.時間片輪轉算法:主要爲每次調度作業就是從後備的就緒隊列中就是選擇一個或者多個最先進入該隊列的作業。每次給隊列首元素分配時間片,當時間片執行完畢時,再把進程放到隊尾。一直循環到所有作業執行完畢。
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("請輸入進程信息:");
for(int i=0;i<5;i++){
System.out.println("進程id"+i);
temp[i]=new jcdu();
temp[i].setid(i);
System.out.print("進程名:");
temp[i].setname(in.next());
System.out.print("到達時間:");
temp[i].setenter_time(in.nextInt());
System.out.print("服務時間:");
temp[i].setser_time(in.nextInt());
}
System.out.println("請輸入時間片的長度 :");
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) //判讀是否有進程進入隊列
{
System.out.println(run_time+"進程"+temp[i].getname()+"進入就緒隊列");
li.add(temp[i]);
}
}
if(li.isEmpty())
{
System.out.println(run_time+"沒有可執行性進程");
}
else
{
jcdu te=li.get(0);
li.remove(0);
for(j=0;j<time_p;j++)
{
name=te.getname();
System.out.println("第"+run_time+"秒"+"執行進程"+name);
te.setdoingtime();
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(te.com())
{
pro_count++;
System.out.println("第"+run_time+"秒"+"進程"+name+"執行結束");
if(li.isEmpty())
{
System.out.println("就緒隊列爲空");
for(i=0;i<temp.length;i++)
{
if(temp[i].getenter_time() == run_time) //判讀是否有進程進入隊列
{
System.out.println(run_time+"進程"+temp[i].getname()+"進入就緒隊列");
li.add(temp[i]);
}
}
}
else
{
te=li.get(0);
li.remove(0);
}
}
run_time++;
System.out.print("就緒隊列:");
for(jcdu x:li){
System.out.print(x.getname()+" ");
}
System.out.println(" ");
}
if(!te.com())
li.add(te);
System.out.println("時間片執行完畢");
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){ //判讀是否有進程進入隊列
for(jcdu x:li){
if(x.getname()!=temp[i].getname())
q++;
}
if(q==li.size()){
li.add(temp[i]);
}
}
System.out.print("就緒隊列:");
for(jcdu x:li){
System.out.print(x.getname()+" ");
}
System.out.println(" ");
if(li.isEmpty()){
System.out.println("當前就緒隊列爲空");
run_time++;
}
else{
jcdu te=li.get(0);
li.remove(0); //取出當前執行的隊首元素
for(j=0;j<time_p;j++){
name=te.getname();
System.out.println("第"+run_time+"秒"+"執行進程"+name);
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
te.setdoingtime(); //已經執行的時間
if(te.com()){ //已經執行完畢 重新選取隊首元素
System.out.println(name+"進程執行結束");
pro_count++;
if(li.isEmpty()){
System.out.println("就緒隊列已空");
}
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){ //判讀下一秒是否有進程進入隊列
System.out.println("第"+run_time+"秒到達"+temp[pi].getname());
li.add(temp[pi]);
}
}
}
if(!te.com()) //如果時間片結束進程還沒有執行行結束從新放入隊尾元素
li.add(te);
}
if(pro_count==5)
break;
}
}*/
}
public static void show(jcdu [] other){
System.out.printf("%-5s %-5s %-5s %-5s\n","進程id","進程名","到達時間","服務時間");
for(jcdu x:other){
System.out.printf("%-5d %-5s %-5s %-5s\n",x.getid(),x.getname(),x.getenter_time(),x.getser_time());
}
}
}
5.多級反饋隊列算法:設置多個隊列,每個隊列的時間片長度不同,當隊列1中作業未執行完畢時,在放入二級隊列依次類推。一直循環到所有作業執行完畢。
class test{
public static int run_time=0; //運行時間
static int pro_count=0; //進程完成數
int ser_time=0; //要求服務時間
int en_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) //判讀是否有進程進入隊列
{
System.out.println(run_time+"秒進程"+temp[i].getname()+"進入就緒隊列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("就緒隊列"+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("當前時間片執行進程"+r.getname());
r.addser_time();
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(r.isequ()){
System.out.println("進程執行結束"+r.getname());
pro_count++;
break;
}
run_time++;
for(int w=0;w<temp.length;w++)
{
if(temp[w].getenter_time() == run_time) //判讀是否有進程進入隊列
{
System.out.println(run_time+"進程"+temp[w].getname()+"進入就緒隊列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("就緒隊列"+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("當前時間片執行進程"+r.getname());
r.addser_time();
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
if(r.isequ()){
System.out.println("進程執行結束"+r.getname());
pro_count++;
break;
}
run_time++;
for(int w=0;w<temp.length;w++)
{
if(temp[w].getenter_time() == run_time) //判讀是否有進程進入隊列
{
System.out.println(run_time+"進程"+temp[w].getname()+"進入就緒隊列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("就緒隊列"+key/2);
if(!li.isEmpty())
{
for(jcdu rw:li)
System.out.print(rw.getname());
}
System.out.println();
}
}
}
System.out.println(run_time);
}
}