In: Computer Science
PROGRAMMING LANGUAGE : JAVA
In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display:
- The given info of each process
- CPU utilization
- The average wait time
- Turnaround time for each process
- CPU response time for each process
You need to start your demo with a UML diagram.
You need to implement the following algorithms:
numOfCPUs: 4
// list of processes to be scheduled
// processID arrivalTime totalExecTime IO_RequestAtTime IO_RequestAtTime ...
p0 0 10 2 5 8
p1 2 3
p2 10 7 1 2 3
- Further Details
The first line indicates how many CPU’s/cores do we have in the system. This sample file indicates that we have 4 CPU’s/cores
Any line that starts with // is a comment
Then we have a list of the processes. The first line:
p0 0 10 2 5 8
means we have process with process ID p0 that arrives at time 0. It needs 10 time on the CPU to finish executing its code. 2 time units after it has started, it asks for I/O. Any I/O request is fulfilled in 2 time units. At time 5 of its execution, p0 asks for I/O again. At time 8 of its execution, it asks for I/O again. There is no limit for how many times a process asks for I/O.
p1 does not ask for any I/O.
p2 arrives at time 10. 1 time unit after it stared execution, it requests I/O.
fcfs:
import java.util.*;
public class FCFS {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter no of process: ");
int n = sc.nextInt();
int pid[] = new int[n]; // process ids
int ar[] = new int[n]; // arrival times
int bt[] = new int[n]; // burst or execution times
int ct[] = new int[n]; // completion times
int ta[] = new int[n]; // turn around times
int wt[] = new int[n]; // waiting times
int temp;
float avgwt=0,avgta=0;
for(int i = 0; i < n; i++)
{
System.out.println("enter process " + (i+1) + " arrival time: ");
ar[i] = sc.nextInt();
System.out.println("enter process " + (i+1) + " brust time: ");
bt[i] = sc.nextInt();
pid[i] = i+1;
}
//sorting according to arrival times
for(int i = 0 ; i <n; i++)
{
for(int j=0; j < n-(i+1) ; j++)
{
if( ar[j] > ar[j+1] )
{
temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
}
}
}
// finding completion times
for(int i = 0 ; i < n; i++)
{
if( i == 0)
{
ct[i] = ar[i] + bt[i];
}
else
{
if( ar[i] > ct[i-1])
{
ct[i] = ar[i] + bt[i];
}
else
ct[i] = ct[i-1] + bt[i];
}
ta[i] = ct[i] - ar[i] ; // turnaround time= completion time- arrival time
wt[i] = ta[i] - bt[i] ; // waiting time= turnaround time- burst time
avgwt += wt[i] ; // total waiting time
avgta += ta[i] ; // total turnaround time
}
System.out.println("\npid arrival brust complete turn waiting");
for(int i = 0 ; i< n; i++)
{
System.out.println(pid[i] + " \t " + ar[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + ta[i] + "\t" + wt[i] ) ;
}
sc.close();
System.out.println("\naverage waiting time: "+ (avgwt/n)); // printing average waiting time.
System.out.println("average turnaround time:"+(avgta/n)); // printing average turnaround time.
}
}
sjf:
import java.util.*; public class SJF { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println ("enter no of process:"); int n = sc.nextInt(); int pid[] = new int[n]; int at[] = new int[n]; // at means arrival time int bt[] = new int[n]; // bt means burst time int ct[] = new int[n]; // ct means complete time int ta[] = new int[n]; // ta means turn around time int wt[] = new int[n]; //wt means waiting time int f[] = new int[n]; // f means it is flag it checks process is completed or not int st=0, tot=0; float avgwt=0, avgta=0; for(int i=0;i<n;i++) { System.out.println ("enter process " + (i+1) + " arrival time:"); at[i] = sc.nextInt(); System.out.println ("enter process " + (i+1) + " brust time:"); bt[i] = sc.nextInt(); pid[i] = i+1; f[i] = 0; } boolean a = true; while(true) { int c=n, min=999; if (tot == n) // total no of process = completed process loop will be terminated break; for (int i=0; i<n; i++) { /* * If i'th process arrival time <= system time and its flag=0 and burst<min * That process will be executed first */ if ((at[i] <= st) && (f[i] == 0) && (bt[i]<min)) { min=bt[i]; c=i; } } /* If c==n means c value can not updated because no process arrival time< system time so we increase the system time */ if (c==n) st++; else { ct[c]=st+bt[c]; st+=bt[c]; ta[c]=ct[c]-at[c]; wt[c]=ta[c]-bt[c]; f[c]=1; tot++; } } System.out.println("\npid arrival brust complete turn waiting"); for(int i=0;i<n;i++) { avgwt+= wt[i]; avgta+= ta[i]; System.out.println(pid[i]+"\t"+at[i]+"\t"+bt[i]+"\t"+ct[i]+"\t"+ta[i]+"\t"+wt[i]); } System.out.println ("\naverage tat is "+ (float)(avgta/n)); System.out.println ("average wt is "+ (float)(avgwt/n)); sc.close(); } } srtf: import java.io.*; public class SRTF { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("Please enter the number of Processes: "); n = Integer.parseInt(br.readLine()); int proc[][] = new int[n + 1][4];//proc[][0] is the AT array,[][1] - RT,[][2] - WT,[][3] - TT for(int i = 1; i <= n; i++) { System.out.println("Please enter the Arrival Time for Process " + i + ": "); proc[i][0] = Integer.parseInt(br.readLine()); System.out.println("Please enter the Burst Time for Process " + i + ": "); proc[i][1] = Integer.parseInt(br.readLine()); } System.out.println(); //Calculation of Total Time and Initialization of Time Chart array int total_time = 0; for(int i = 1; i <= n; i++) { total_time += proc[i][1]; } int time_chart[] = new int[total_time]; for(int i = 0; i < total_time; i++) { //Selection of shortest process which has arrived int sel_proc = 0; int min = 99999; for(int j = 1; j <= n; j++) { if(proc[j][0] <= i)//Condition to check if Process has arrived { if(proc[j][1] < min && proc[j][1] != 0) { min = proc[j][1]; sel_proc = j; } } } //Assign selected process to current time in the Chart time_chart[i] = sel_proc; //Decrement Remaining Time of selected process by 1 since it has been assigned the CPU for 1 unit of time proc[sel_proc][1]--; //WT and TT Calculation for(int j = 1; j <= n; j++) { if(proc[j][0] <= i) { if(proc[j][1] != 0) { proc[j][3]++;//If process has arrived and it has not already completed execution its TT is incremented by 1 if(j != sel_proc)//If the process has not been currently assigned the CPU and has arrived its WT is incremented by 1 proc[j][2]++; } else if(j == sel_proc)//This is a special case in which the process has been assigned CPU and has completed its execution proc[j][3]++; } } //Printing the Time Chart if(i != 0) { if(sel_proc != time_chart[i - 1]) //If the CPU has been assigned to a different Process we need to print the current value of time and the name of //the new Process { System.out.print("--" + i + "--P" + sel_proc); } } else//If the current time is 0 i.e the printing has just started we need to print the name of the First selected Process System.out.print(i + "--P" + sel_proc); if(i == total_time - 1)//All the process names have been printed now we have to print the time at which execution ends System.out.print("--" + (i + 1)); } System.out.println(); System.out.println(); //Printing the WT and TT for each Process System.out.println("P\t WT \t TT "); for(int i = 1; i <= n; i++) { System.out.printf("%d\t%2dms\t%2dms",i,proc[i][2],proc[i][3]); System.out.println(); } System.out.println(); //Printing the average WT & TT float WT = 0,TT = 0; for(int i = 1; i <= n; i++) { WT += proc[i][2]; TT += proc[i][3]; } WT /= n; TT /= n; System.out.println("The Average WT is: " + WT + "ms"); System.out.println("The Average TT is: " + TT + "ms"); } } RR: // Java program for implementation of RR scheduling public class GFG |