Question

In: Computer Science

I'm in a computer science course and I have an assignment that asks me to implement...

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.

Solutions

Expert Solution

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​:


Related Solutions

Assignment Content In this course, you have learned that computer programming is key to computer science....
Assignment Content In this course, you have learned that computer programming is key to computer science. The ability to program a succession of instructions to resolve a computing problem is a valuable skill that is integral to many aspects of modern life. You have also seen the versatility and distinguishing characteristics that make Python 3 a popular choice for programmers. End-of-Course Essay Write a 1- to 2-page essay in which you connect this course to your own career or personal...
In this course, you have learned that computer programming is key to computer science. The ability...
In this course, you have learned that computer programming is key to computer science. The ability to program a succession of instructions to resolve a computing problem is a valuable skill that is integral to many aspects of modern life. You have also seen the versatility and distinguishing characteristics that make Python 3 a popular choice for programmers. End-of-Course Essay Write a 1- to 2-page essay in which you connect this course to your own career or personal life. Imagine...
I am in this electrical engineering project course in which an assignment is asking me this...
I am in this electrical engineering project course in which an assignment is asking me this and I cannot make heads or tails of what it means by this... Discuss the tradeoffs involved between using patents and trade secrets to protect intellectual property. Please be as descriptive as possible, THANKS
COP3014-Foundations of Computer Science Assignment #6 You will implement a program called "nursery_inv3.cpp" to process customer...
COP3014-Foundations of Computer Science Assignment #6 You will implement a program called "nursery_inv3.cpp" to process customer purchase orders (orders) for a nursery. You will read the data stored in a data file into a static array of purchase orders records, then process each purchase order record in the array, and finally print the array of purchase order records to a datafile. The purchase orders will be stored in order records. Each order record contains nine fields, which are as follows:...
I have a lab assignment that I'm not sure how to do. The experiment is a...
I have a lab assignment that I'm not sure how to do. The experiment is a cart moving 60cm distance and there is a fan on top of it making it have a mass of .56kg. Every trial there is 100g added to the cart. For this part, the time is kept the same. 1. If the force provided by the fan was the same for each run and we have chosen the same time interval, how does the impulse...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
This is my presentation/ research topic, Professionals conduct in Computer Science. I have to give a...
This is my presentation/ research topic, Professionals conduct in Computer Science. I have to give a 10 minutes presentation about it but I dont understand this topic and really dont know what to talk about to make it interesting for the class. I have to talk about what are the problems, questions, my assumption, point of view, and conclusion about the topic, which I am very confused. Im really scared of doing public speaking buy will try my best.
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For Networks in Computer Science, please tell me about: a.)   The responsibilities of the data link...
For Networks in Computer Science, please tell me about: a.)   The responsibilities of the data link layer? b.)   What is a frame? c.)   How does the byte count method for framing work? d.)   What is byte stuffing? e.)   How do the FLAG and ESC bytes work?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT