当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to implement SJF scheduling to this code?

问题描述:

I am developing code for printer that schedules jobs using Operating System concepts. I am accepting the arrival time and number of pages to be printed. I have used semaphore so that only one job can be printed at a time. How can I implement SJF scheduling to it?

Here is my code:

import java.util.*;

import java.util.concurrent.Semaphore;

public class newproj{

static boolean b=true;

public static Semaphore s = new Semaphore(1);

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

int n;

System.out.println("Enter number of print jobs:");

n=sc.nextInt();

int a[][]=new int[n][2];

System.out.println("Enter number of pages of each job and arrival time:");

for(int i=0;i<n;i++)

{

for(int j=0;j<2;j++)

{

a[i][j]=sc.nextInt();

}

}

Thread[] threads = new Thread[n];

for (int i = 0; i < threads.length; i++) {

threads[i] = new Thread(new user(i,a[i][0],a[i][1],s));

threads[i].start();

}

}

}

class user extends Thread{

int a,i=0;

int n,t,ct=0;

Semaphore s;

String st;

public user(int n,int a,int t,Semaphore s)

{

this.n=n;

this.a=a;

this.s=s;

this.t=t;

//System.out.println(this.n);

}

public void run()

{

while(true)

{

synchronized(this){

if((newproj.b==true)&&(ct>=t))

{

try{

s.acquire();

newproj.b=false;

}

catch(Exception e){}

System.out.println("Printing "+a+" pages of Job "+(n+1)+" "+(ct++));

try{

for(int j=1;j<=a;j++)

System.out.println("Printed "+j+" page of Job "+(n+1)+" "+(ct++));

Thread.sleep(1000);

}

catch(Exception e){}

System.out.println("Job "+(n+1)+" has finished printing "+(ct++));

newproj.b=true;

s.release();

try{

Thread.sleep(1000);

}

catch(Exception e){}

break;

}

else if((newproj.b==false)&&(ct>t))

{

System.out.println("Job "+(n+1)+" is waiting"+(ct++));

try{

Thread.sleep(1000);

}

catch(Exception e){}

}

else if(ct<t)

{

System.out.println("No Job to be executed"+(n+1)+" "+(ct++));

try{

Thread.sleep(1000);

}

catch(Exception e){}

}

}

}

};

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: