Question

Could you please add Round Robin CPU scheduling algorithm with quantum 10 in this code? Scheduling.java...

Could you please add Round Robin CPU scheduling algorithm with quantum 10 in this code?

Scheduling.java

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Scheduling {
public static void main(String[] args) {

ArrayList<Proc> pr = new ArrayList<Proc>();
ArrayList<Proc> pr1 = new ArrayList<Proc>();
ArrayList<Proc> pr2 = new ArrayList<Proc>();
ArrayList<Proc> pr3 = new ArrayList<Proc>();
  

int count=0;
Random ran = new Random();
boolean unique=false;
int num=0;


for(int i=0;i<=5;i++){
unique=false;
Proc p1=new Proc();

while(!unique){
num=ran.nextInt(10) + 1;
if(i==0)
break;

for(int j=0;j<i;j++){
if(num==pr.get(j).id){
unique=false;
break;
}
  
else
unique=true;
}
}

p1.id=num;
p1.burstLength=ran.nextInt(10) + 10;
p1.priority=ran.nextInt(10) + 1;
pr.add(p1);
pr2.add(p1);
count++;
}

System.out.println("Process ID | Priority | Burst-length");
for(int i=0;i<count;i++){
System.out.println(pr.get(i).id+" "+pr.get(i).priority+" "+pr.get(i).burstLength);
}

boolean cont=true;
while(cont){

System.out.println("Do you want to add one more process true/false" );
Scanner n = new Scanner(System.in);
cont = n.nextBoolean();
if(cont){

Proc p1=new Proc();
unique=false;
  
while(!unique){
System.out.println("Enter process id" );
num=n.nextInt();
for(int j=0;j<count;j++){
if(num==pr.get(j).id){
unique=false;
break;
}
else
unique=true;
}
}
  
p1.id=num;
System.out.println("Enter process burst length" );
p1.burstLength=n.nextInt();
System.out.println("Enter process priority" );
p1.priority=n.nextInt();
pr2.add(p1);
pr.add(p1);
count++;
}

else
break;

}

System.out.println("Process ID | Priority | Burst-length");
for(int i=0;i<count;i++)
{
System.out.println(pr.get(i).id+" "+pr.get(i).priority+" "+pr.get(i).burstLength);
}

int max=100;
int ind=0;
int time=0;
while(pr.size()!=0)
{
max=100;
for(int i=0;i<count;i++){
if(pr.get(i).priority<max){
max=pr.get(i).priority;
ind=i;   
}
else if(pr.get(i).priority==max)
{
if(pr.get(i).burstLength<pr.get(ind).burstLength)
{
ind=i;
}

}

}
pr.get(ind).TWT=time-0;
time=time+pr.get(ind).burstLength;
pr1.add(pr.get(ind));
pr.remove(ind);
count--;
}
float awt;
float total=0;
for(int i=0;i<pr1.size();i++)
{
total=total+pr1.get(i).TWT;
}
awt=total/pr1.size();


time=0;
ind=0;
while(pr2.size()!=0)
{
max=100;
for(int i=0;i<pr2.size();i++)
{
if(pr2.get(i).priority<max)
{
max=pr2.get(i).priority;
ind=i;   
}
else if(pr2.get(i).priority==max)
{
if(pr2.get(i).id<pr2.get(ind).id)
{
ind=i;
}

}

}
pr2.get(ind).TWT=time-0;
time=time+pr2.get(ind).burstLength;
pr3.add(pr2.get(ind));
pr2.remove(ind);
}



float awt1;
float total1=0;
for(int i=0;i<pr3.size();i++)
{
total1=total1+pr3.get(i).TWT;
}
awt1=total1/pr3.size();
System.out.println("The avearge waiting time of SJF is "+ awt);
System.out.println("The avearge waiting time of priority scheduling is "+ awt1);
System.out.println("Process ID | Priority | Burst-length | Scheduling algorithm | Total waiting time");
if(awt1>awt)
{

for(int i=0;i<pr1.size();i++)
{
System.out.println(pr1.get(i).id+" "+pr1.get(i).priority+" "+pr1.get(i).burstLength+" "+"SJF"+" "+pr1.get(i).TWT);
}
for(int i=0;i<pr3.size();i++)
{
System.out.println(pr3.get(i).id+" "+pr3.get(i).priority+" "+pr3.get(i).burstLength+" "+"Priority"+" "+pr3.get(i).TWT);
}
}
else
{
for(int i=0;i<pr3.size();i++)
{
System.out.println(pr3.get(i).id+" "+pr3.get(i).priority+" "+pr3.get(i).burstLength+" "+"Priority"+" "+pr3.get(i).TWT);
}

for(int i=0;i<pr1.size();i++)
{
System.out.println(pr1.get(i).id+" "+pr1.get(i).priority+" "+pr1.get(i).burstLength+" "+"SJF"+" "+pr1.get(i).TWT);
}
}

}

}

Proc.java

public class Proc {

int id;

int burstLength;

int priority;

int TWT;

Proc()

{

}

void setId(int Id)

{

id=Id;

}

int getId()

{

return id;

}

void setburstLength(int burstlen)

{

burstLength=burstlen;

}

int getburstLength()

{

return burstLength;

}

void setpriority(int pri)

{

priority=pri;

}

int getpriority()

{

return priority;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code:-

import java.io.*;
class round
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int i,j,k,q,sum=0;
System.out.println(“Enter number of process:”);
int n=Integer.parseInt(in.readLine());
int bt[]=new int[n];
int wt[]=new int[n];
int tat[]=new int[n];
int a[]=new int[n];
System.out.println(“Enter brust Time:”);
for(i=0;i<n;i++)
{
System.out.println(“Enter brust Time for “+(i+1));
bt[i]=Integer.parseInt(in.readLine());
}
System.out.println(“Enter Time quantum:”);
q=Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
a[i]=bt[i];
for(i=0;i<n;i++)
wt[i]=0;
do
{
for(i=0;i<n;i++)
{
if(bt[i]>q)
{
bt[i]-=q;
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=q;
}
}
else
{
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=bt[i];
}
bt[i]=0;
}
}
sum=0;
for(k=0;k<n;k++)
sum=sum+bt[k];
}
while(sum!=0);
for(i=0;i<n;i++)
tat[i]=wt[i]+a[i];
System.out.println(“process\t\tBT\tWT\tTAT”);
for(i=0;i<n;i++)
{
System.out.println(“process”+(i+1)+”\t”+a[i]+”\t”+wt[i]+”\t”+tat[i]);
}
float avg_wt=0;
float avg_tat=0;
for(j=0;j<n;j++)
{
avg_wt+=wt[j];
}
for(j=0;j<n;j++)
{
avg_tat+=tat[j];
}
System.out.println(“average waiting time “+(avg_wt/n)+”\n Average turn around time”+(avg_tat/n));
}
}
/*Round robin Scheduling algorithm output:

Enter number of process:
4
Enter brust Time:
Enter brust Time for 1
4
Enter brust Time for 2
5
Enter brust Time for 3
6
Enter brust Time for 4
7
Enter Time quantum:
4

process BT WT TAT
process1 4 0 4
process2 5 12 17
process3 6 13 19
process4 7 15 22

average waiting time 10.0
Average turn around time15.5
*/

Add a comment
Know the answer?
Add Answer to:
Could you please add Round Robin CPU scheduling algorithm with quantum 10 in this code? Scheduling.java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.The following statement gets an element from position 4 in an array named myArray: x =...

    1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...

  • Can you write the algorithm for this code? #include<stdio.h> int main() { int process[20], priority[20], arrival_time[20],...

    Can you write the algorithm for this code? #include<stdio.h> int main() { int process[20], priority[20], arrival_time[20], burst_time[20], turnaround_time[20], waiting_time[20]; int i, j, limit, sum = 0, position, temp; float average_wait_time, average_turnaround_time; printf("Enter Total Number of Processes:\t"); scanf("%d", &limit); printf("\nEnter Burst Time and Priority For %d Processes\n", limit); for(i = 0; i < limit; i++) { printf("\nProcess[%d]\n", i + 1); printf("Process Burst Time:\t"); scanf("%d", &burst_time[i]); printf("Process Priority:\t"); scanf("%d", &priority[i]); process[i] = i + 1; } for(i = 0; i < limit;...

  • i need help converting this code to java please #include<stdio.h> typedef struct{ int pid,at,bt,ct,tat,wt,f; }process; int...

    i need help converting this code to java please #include<stdio.h> typedef struct{ int pid,at,bt,ct,tat,wt,f; }process; int main() { int n,i,j,st=0,c,tot=0,pno=0,swi=0; float atat=0,awt=0; printf("enter no of processes : "); scanf("%d",&n); process a[n],temp; for (i=0;i<n;i++){ a[i].pid=i+1; a[i].f=0; printf("enter at : "); scanf("%d",&a[i].at); printf("enter the bt : "); scanf("%d",&a[i].bt); printf("--------------------------- "); } while(1){ int min=999,c=n; if (tot==n) break; for (i=0;i<n;i++){ if ((a[i].at<=st)&&(a[i].f==0)&&(a[i].at<min)){ min=a[i].at; c=i; } } if(pno!=a[c].pid) swi++; if (c==n) st++; else{ a[c].ct=st+a[c].bt; st=st+a[c].bt; a[c].tat=a[c].ct-a[c].at; atat=atat+a[c].tat; a[c].wt=a[c].tat-a[c].bt; awt=awt+a[c].wt; a[c].f=1; tot++; } } printf("...

  • The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java....

    The names of the two input files as well as the output file are supposed to be provided as arguments. Could you please fix it? Thanks. DPriorityQueue.java: // Import the required classes import java.io.*; import java.util.*; //Create the class public class DPriorityQueue { //Declare the private members variables. private int type1,type2; private String CostInTime[][], SVertex, DVertex; private List<String> listOfTheNodes; private Set<String> List; private List<Root> ListOfVisitedNode; private HashMap<String, Integer> minimalDistance; private HashMap<String, Integer> distOfVertices; private PriorityQueue<City> priorityQueue; // prove the definition...

  • I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public...

    I cant get the code to work. Any help would be appreciated. Instruction.java package simmac; public class Instruction { public static final int DW = 0x0000; public static final int ADD = 0x0001; public static final int SUB = 0x0002; public static final int LDA = 0x0003; public static final int LDI = 0x0004; public static final int STR = 0x0005; public static final int BRH = 0x0006; public static final int CBR = 0x0007; public static final int HLT...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • Please help with my car traffic simulator! Code that I already have below, I do not know how to...

    Please help with my car traffic simulator! Code that I already have below, I do not know how to start it off! public class IntersectionSimulation { private final static int EAST_WEST_GREEN_TIME = 30 ; private final static int[] NORTH_SOUTH_GREEN_TIMES = { 20, 24, 30, 42 } ; private final static int[] CAR_INTERSECTION_RATES = { 3, 5, 10 } ; private final static int[] CAR_QUEUEING_RATES = { 5, 10, 30 } ; private final static int[] EXPERIMENT_DURATIONS = { 3*60, 5*60,...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT