In: Computer Science
I'm in a computer science course and I have an assignment that asks me to implement a CPU scheduler in Java. Here are some details for the task:
The program should ask for the number of processes.
Then the program will ask for burst and arrival times for each process.
Using that information, the program should schedule the processes using the following scheduling algorithms:
First Come First Serve (FCFS)
Shortest Jobs First (SJF)
Shortest Remaining Time First (SRTF)
The output of the program would return a Gantt chart that demonstrates the algorithms, along with the turnaround time and waiting time for each process for each algorithm.
I'm not really sure how to go about accomplishing this, as we were only going over CPU scheduling conceptually and not actually covering how to implement. Any help/advice would be appreciated. Thank you.
CODE
import java.util.Scanner;
public class CPU
{
public static void main(String [] args)
{
int i;
System.out.print("Enter how many processes do you want to
schedule:");
Scanner read=new Scanner(System.in);
int n=read.nextInt();
float a_t[]=new float[n];
int p[]=new int[n];
float b_t[]=new float[n];
float w_t[]=new float[n];
float t_a_t[]=new float[n];
float c_t[]=new float[n];
float at[]=new float[n];
float bt[]=new float[n];
int pro[]=new int[n];
float rbt[]=new float[n];
int preempt=0;
for(i=0;i<n;i++)
{
p[i]=i+1;
System.out.print("\nEnter process "+(i+1)+" arrival time:");
a_t[i]=read.nextFloat();
System.out.print("Enter process "+(i+1)+" burst time:");
b_t[i]=read.nextFloat();
rbt[i]=b_t[i];
}
System.out.println("Entered order of processes is: ");
System.out.println("PID\tA_T\tB_T");
for(i=0;i<n;i++)
System.out.println("p"+p[i]+"\t"+a_t[i]+"\t"+b_t[i]);
for(i=n-1;i>0;i--)
{
for(int j=0;j<n-1;j++)
{
if(a_t[j]>a_t[j+1])
{
float temp=a_t[j];
a_t[j]=a_t[j+1];
a_t[j+1]=temp;
int tem=p[j];
p[j]=p[j+1];
p[j+1]=tem;
temp=b_t[j];
b_t[j]=b_t[j+1];
b_t[j+1]=temp;
}
}
}
System.out.println("\n\nProcesses After Sorting on the basis of
arrival time is in fcfs:");
System.out.println("PID\tA_T\tB_T");
for(i=0;i<n;i++)
System.out.println("p"+p[i]+"\t"+a_t[i]+"\t"+b_t[i]);
c_t[0]=a_t[0]+b_t[0];
t_a_t[0]=c_t[0]-a_t[0];
w_t[0]=0;
for(i=1;i<n;i++)
{
if(a_t[i]>c_t[i-1])
{
c_t[i]=a_t[i]+b_t[i];
}
else
{
c_t[i]=c_t[i-1]+b_t[i];
}
t_a_t[i]=c_t[i]-a_t[i];
w_t[i]=t_a_t[i]-b_t[i];
}
float c_time=0;
float t_a_time=0;
float w_time=0;
System.out.println("\n\nProcesses are executed....in
fcfs\n");
System.out.println("PID\tA_T\tB_T\tC_T\tT_A_T\tW_T");
for(i=0;i<n;i++)
{ c_time=c_time+c_t[i];
t_a_time=t_a_time+t_a_t[i];
w_time=w_time+w_t[i];
System.out.println("p"+p[i]+"\t"+a_t[i]+"\t"+b_t[i]+"\t"+c_t[i]+"\t"+t_a_t[i]+"\t"+w_t[i]);
}
//sjf
for(i=n-1;i>0;i--)
{
for(int j=0;j<n-1;j++)
{
if(a_t[j]>a_t[j+1])
{
float temp=a_t[j];
a_t[j]=a_t[j+1];
a_t[j+1]=temp;
int tem=p[j];
p[j]=p[j+1];
p[j+1]=tem;
temp=b_t[j];
b_t[j]=b_t[j+1];
b_t[j+1]=temp;
}
if(a_t[j]==a_t[j+1])
{
if(b_t[j]>b_t[j+1])
{
float temp=a_t[j];
a_t[j]=a_t[j+1];
a_t[j+1]=temp;
int tem=p[j];
p[j]=p[j+1];
p[j+1]=tem;
temp=b_t[j];
b_t[j]=b_t[j+1];
b_t[j+1]=temp;
}
}
}
}
int c=0;
float e_t=0;
for(i=0;i<n;i++)
{
if(i==0)
{
e_t=a_t[i]+b_t[i];
}
else if(a_t[i]>e_t)
{
e_t=a_t[i]+b_t[i];
}
else
{
e_t=e_t+b_t[i];
}
for(int j=i+1;j<n;j++)
{
if(a_t[j]<=e_t)
c++;
}
for(int k=c-1;k>=0;k--)
{
for(int l=i+1;l<(c+i);l++)
{
if(b_t[l]>b_t[l+1])
{
float temp=a_t[l];
a_t[l]=a_t[l+1];
a_t[l+1]=temp;
int tem=p[l];
p[l]=p[l+1];
p[l+1]=tem;
temp=b_t[l];
b_t[l]=b_t[l+1];
b_t[l+1]=temp;
}
}
}
c=0;
}
System.out.println("\n\nProcesses After Sorting on the basis of
arrival time and Burst time in sjf are:");
System.out.println("PID\tA_T\tB_T");
for(i=0;i<n;i++)
System.out.println("p"+p[i]+"\t"+a_t[i]+"\t"+b_t[i]);
c_t[0]=a_t[0]+b_t[0];
t_a_t[0]=c_t[0]-a_t[0];
w_t[0]=0;
for(i=1;i<n;i++)
{
if(a_t[i]>c_t[i-1])
{
c_t[i]=a_t[i]+b_t[i];
}
else
{
c_t[i]=c_t[i-1]+b_t[i];
}
t_a_t[i]=c_t[i]-a_t[i];
w_t[i]=t_a_t[i]-b_t[i];
}
w_time=0;
t_a_time=0;
c_time=0;
System.out.println("\n\nProcesses are executed....in sjf\n");
System.out.println("PID\tA_T\tB_T\tC_T\tT_A_T\tW_T");
for(i=0;i<n;i++)
{ c_time=c_time+c_t[i];
t_a_time=t_a_time+t_a_t[i];
w_time=w_time+w_t[i];
System.out.println("p"+p[i]+"\t"+a_t[i]+"\t"+b_t[i]+"\t"+c_t[i]+"\t"+t_a_t[i]+"\t"+w_t[i]);
}
float a_w_t=w_time/n;
float a_t_a_t=t_a_time/n;
float a_c_t=c_time/n;
System.out.println("\nAverage waiting time is:"+a_w_t);
System.out.println("Average turn around time is:"+a_t_a_t);
System.out.print("Average completion time is:"+a_c_t+"\n");
//srtf
for(i=n;i>=0;i--)
{
for(int j=0;j<n-1;j++)
{
if(a_t[j]>a_t[j+1])
{
float temp=a_t[j];
a_t[j]=a_t[j+1];
a_t[j+1]=temp;
int tem=p[j];
p[j]=p[j+1];
p[j+1]=tem;
temp=rbt[j];
rbt[j]=rbt[j+1];
rbt[j+1]=temp;
temp=b_t[j];
b_t[j]=b_t[j+1];
b_t[j+1]=temp;
}
if(a_t[j]==a_t[j+1])
{
if(b_t[j]>b_t[j+1])
{
float temp=a_t[j];
a_t[j]=a_t[j+1];
a_t[j+1]=temp;
int tem=p[j];
p[j]=p[j+1];
p[j+1]=tem;
temp=rbt[j];
rbt[j]=rbt[j+1];
rbt[j+1]=temp;
temp=b_t[j];
b_t[j]=b_t[j+1];
b_t[j+1]=temp;
}
}
}
}
System.out.println("Processes after sorting on the basis of arrival
times and burst times in srtf are:");
System.out.println("PID\tA_T\tB_T");
for(i=0;i<n;i++)
System.out.println("p"+(p[i])+"\t"+a_t[i]+"\t"+rbt[i]);
int j=0;
c=0;
int count=0;
float st=0;
float et=0;
int pr=0;
while(count<n)
{
preempt=0;
for(pr=0;pr<n;pr++)
{
if(rbt[pr]>0)
break;
}
if(a_t[pr]>et)
{
st=a_t[pr];
et=a_t[pr]+rbt[pr];
}
else
{
st=et;
et=et+rbt[pr];
}
for(i=(int)st+1;i<=et;i++)
{
for(j=0;j<n;j++)
{
if(a_t[j]==i)
{
preempt++;
}
}
if(preempt!=0)
break;
}
if(preempt!=0)
{
c=0;
for(int d=0;d<n;d++)
{
if(a_t[d]<=i)
c++;
}
}
if(i<=et)
et=(float)i;
rbt[pr]=rbt[pr]-(et-st);
if(rbt[pr]==0)
{
c_t[count]=et;
t_a_t[count]=c_t[count]-a_t[pr];
w_t[count]=t_a_t[count]-b_t[pr];
pro[count]=p[pr];
at[count]=a_t[pr];
bt[count]=b_t[pr];
count++;
}
for(int l=c;l>0;l--)
{
for(int m=0;m<c-1;m++)
{
if(rbt[m]>rbt[m+1])
{
float temp=a_t[m];
a_t[m]=a_t[m+1];
a_t[m+1]=temp;
int tem=p[m];
p[m]=p[m+1];
p[m+1]=tem;
temp=rbt[m];
rbt[m]=rbt[m+1];
rbt[m+1]=temp;
temp=b_t[m];
b_t[m]=b_t[m+1];
b_t[m+1]=temp;
}
}
}
/* for(int g=0;g<n;g++)
System.out.print(p[g]+"["+rbt[g]+"]\t");*/
}
w_time=0;
t_a_time=0;
c_time=0;
System.out.println("\n\nProcesses are executed....\n");
System.out.println("PID\tA_T\tB_T\tC_T\tT_A_T\tW_T");
for(i=0;i<count;i++)
{
c_time=c_time+c_t[i];
t_a_time=t_a_time+t_a_t[i];
w_time=w_time+w_t[i];
System.out.println("p"+(pro[i])+"\t"+at[i]+"\t"+bt[i]+"\t"+c_t[i]+"\t"+t_a_t[i]+"\t"+w_t[i]);
}
a_w_t=w_time/n;
a_t_a_t=t_a_time/n;
a_c_t=c_time/n;
System.out.println("\nAverage waiting time is:"+a_w_t);
System.out.println("Average turn around time is:"+a_t_a_t);
System.out.print("Average completion time is:"+a_c_t+"\n");
}
}
Output: