Question

In: Computer Science

Create a scheduler that takes unlimited processes. It will ask for number of jobs, arrival time,...

Create a scheduler that takes unlimited processes. It will ask for number of jobs, arrival time, and CPU burst time. It should use FCFS, SJF, and SRTF methods. It should also print out the gantt chart, waiting time, and turn around time for each of the processes. Please solve using Java.

Solutions

Expert Solution

FCFS

PROGRAM

import java.text.ParseException;

class GFG {

   // Function to find the waiting time for all
   // processes
   static void findWaitingTime(int processes[], int n,
           int bt[], int wt[]) {
       // waiting time for first process is 0
       wt[0] = 0;

       // calculating waiting time
       for (int i = 1; i < n; i++) {
           wt[i] = bt[i - 1] + wt[i - 1];
       }
   }

   // Function to calculate turn around time
   static void findTurnAroundTime(int processes[], int n,
           int bt[], int wt[], int tat[]) {
       // calculating turnaround time by adding
       // bt[i] + wt[i]
       for (int i = 0; i < n; i++) {
           tat[i] = bt[i] + wt[i];
       }
   }

   //Function to calculate average time
   static void findavgTime(int processes[], int n, int bt[]) {
       int wt[] = new int[n], tat[] = new int[n];
       int total_wt = 0, total_tat = 0;

       //Function to find waiting time of all processes
       findWaitingTime(processes, n, bt, wt);

       //Function to find turn around time for all processes
       findTurnAroundTime(processes, n, bt, wt, tat);

       //Display processes along with all details
       System.out.printf("Processes Burst time Waiting"
                   +" time Turn around time\n");

       // Calculate total waiting time and total turn
       // around time
       for (int i = 0; i < n; i++) {
           total_wt = total_wt + wt[i];
           total_tat = total_tat + tat[i];
           System.out.printf(" %d ", (i + 1));
           System.out.printf("   %d ", bt[i]);
           System.out.printf("   %d", wt[i]);
           System.out.printf("   %d\n", tat[i]);
       }
       float s = (float)total_wt /(float) n;
       int t = total_tat / n;
       System.out.printf("Average waiting time = %f", s);
       System.out.printf("\n");
       System.out.printf("Average turn around time = %d ", t);
   }

   // Driver code
   public static void main(String[] args) throws ParseException {
       //process id's
       int processes[] = {1, 2, 3};
       int n = processes.length;

       //Burst time of all processes
       int burst_time[] = {10, 5, 8};

       findavgTime(processes, n, burst_time);

   }
}

GANTT CHART

                                                                     SJF

PROGRAM

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();

}

GANTT CHART

SRTF

PROGRAM

class Process
{
   int pid; // Process ID
   int bt; // Burst Time
   int art; // Arrival Time
  
   public Process(int pid, int bt, int art)
   {
       this.pid = pid;
       this.bt = bt;
       this.art = art;
   }
}

public class GFG
{
   // Method to find the waiting time for all
   // processes
   static void findWaitingTime(Process proc[], int n,
                                   int wt[])
   {
       int rt[] = new int[n];
  
       // Copy the burst time into rt[]
       for (int i = 0; i < n; i++)
           rt[i] = proc[i].bt;
  
       int complete = 0, t = 0, minm = Integer.MAX_VALUE;
       int shortest = 0, finish_time;
       boolean check = false;
  
       // Process until all processes gets
       // completed
       while (complete != n) {
  
           // Find process with minimum
           // remaining time among the
           // processes that arrives till the
           // current time`
           for (int j = 0; j < n; j++)
           {
               if ((proc[j].art <= t) &&
               (rt[j] < minm) && rt[j] > 0) {
                   minm = rt[j];
                   shortest = j;
                   check = true;
               }
           }
  
           if (check == false) {
               t++;
               continue;
           }
  
           // Reduce remaining time by one
           rt[shortest]--;
  
           // Update minimum
           minm = rt[shortest];
           if (minm == 0)
               minm = Integer.MAX_VALUE;
  
           // If a process gets completely
           // executed
           if (rt[shortest] == 0) {
  
               // Increment complete
               complete++;
               check = false;
  
               // Find finish time of current
               // process
               finish_time = t + 1;
  
               // Calculate waiting time
               wt[shortest] = finish_time -
                           proc[shortest].bt -
                           proc[shortest].art;
  
               if (wt[shortest] < 0)
                   wt[shortest] = 0;
           }
           // Increment time
           t++;
       }
   }
  
   // Method to calculate turn around time
   static void findTurnAroundTime(Process proc[], int n,
                           int wt[], int tat[])
   {
       // calculating turnaround time by adding
       // bt[i] + wt[i]
       for (int i = 0; i < n; i++)
           tat[i] = proc[i].bt + wt[i];
   }
  
   // Method to calculate average time
   static void findavgTime(Process proc[], int n)
   {
       int wt[] = new int[n], tat[] = new int[n];
       int total_wt = 0, total_tat = 0;
  
       // Function to find waiting time of all
       // processes
       findWaitingTime(proc, n, wt);
  
       // Function to find turn around time for
       // all processes
       findTurnAroundTime(proc, n, wt, tat);
  
       // Display processes along with all
       // details
       System.out.println("Processes " +
                       " Burst time " +
                       " Waiting time " +
                       " Turn around time");
  
       // Calculate total waiting time and
       // total turnaround time
       for (int i = 0; i < n; i++) {
           total_wt = total_wt + wt[i];
           total_tat = total_tat + tat[i];
           System.out.println(" " + proc[i].pid + "\t\t"
                           + proc[i].bt + "\t\t " + wt[i]
                           + "\t\t" + tat[i]);
       }
  
       System.out.println("Average waiting time = " +
                       (float)total_wt / (float)n);
       System.out.println("Average turn around time = " +
                       (float)total_tat / (float)n);
   }
  
   // Driver Method
   public static void main(String[] args)
   {
       Process proc[] = { new Process(1, 6, 1),
                           new Process(2, 8, 1),
                           new Process(3, 7, 2),
                           new Process(4, 3, 3)};
      
       findavgTime(proc, proc.length);
   }
}

    GANTT CHART

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time Response Time
1 0 8 20 20 12 0
2 1 4 10 9 5 1
3 2 2 4 2 0 2
4 3 1 5 2 1 4
5 4 3 13 9 6 10
6 5 2 7 2 0 5

                    Avg Waiting Time = 24/6


Related Solutions

There are 4 processes with different arrival time and service time. Process Arrival Service A 0...
There are 4 processes with different arrival time and service time. Process Arrival Service A 0 10 B 3 4 C 5 6 D 7 2 a. Use FCFS, RR(4), SPN, SRT, and HRRN to schedule them. Calculate the waiting time and turnaround time for each process and the average turnaround time for each algorithm. b. Compare all the 5 algorithms. Considering the overhead time for each scheduling takes 1 time unit, which is the best? If the overhead takes...
Consider the following processes that came at time zero with the following order of arrival with...
Consider the following processes that came at time zero with the following order of arrival with the given CPU burst time. Draw the chart for average turnaround time, average wait time, and average response time versus the time quantum 1,2,3,4,5,6 and 7 using a round-robin CPU scheduling algorithm. Processes CPU Burst Time P1 6 P2 3 P3 1 P4 7
You are given the following processes with CPU-burst time, arrival time and priority (lower # means...
You are given the following processes with CPU-burst time, arrival time and priority (lower # means higher priority) Process CPU-burst Arrival time Priority P1 12 0 5 P2 6 6 2 P3 9 1 2 P4 14 3 1 P5 7 5 4 For each of the following scheduling algorithm, show (using the diagram as in the slides), how the process are being executed. Also calculate the average wait time. Shortest job first (non-preemptive) Shortest remaining job first (preemptive) Priority-based...
1. Consider the following set of processes, with the length of CPU burst and arrival time...
1. Consider the following set of processes, with the length of CPU burst and arrival time given in milliseconds.                                                              Process            Burst time       Priority            Arrival Time             P1                         10                    3                              0 P2                          1                     1                              2 P3                          2                     4                              4 P4                          5                     2                              8 a) Draw the Gantt chart that illustrates the execution of these processes using the preemptive priority scheduling algorithm (a smaller priority number implies a higher priority). b) What...
6. Consider the following set of processes P1, P2, P3, P4. Process Burst Time Arrival Time...
6. Consider the following set of processes P1, P2, P3, P4. Process Burst Time Arrival Time Priority P1 3 0 1 P2 5 1 2 P3 8 3 3 P4 4 4 2 a) Draw Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: first-come, first-served (FCFS), priority scheduling (larger number=high priority), and Round-Bobin (RR, quantum=2). b) Compute the average waiting time, turnaround time for the three algorithms. Turnaround time – amount of time to...
Unemployment that results because it takes time for workers to search for the jobs that best...
Unemployment that results because it takes time for workers to search for the jobs that best suit their tastes and skills is called 1. structural unemployment 2. frictional unemployment 3. cyclical unemployment
Assume you have the following jobs to execute with one processor: i t(pi) Arrival Time 0...
Assume you have the following jobs to execute with one processor: i t(pi) Arrival Time 0 75 0 1 40 10 2 25 10 3 20 80 4 45 85 Using the table, assume the context switch time is five time units with RR scheduling. Create a Gantt chart illustrating the execution of these processes. What is the turnaround time for process p3? What is the average wait time for the processes?
Create a C++ program which will accept an unlimited number of scores and calculates the average...
Create a C++ program which will accept an unlimited number of scores and calculates the average score. You will also need to prompt for the total points possible. Assume each test is worth 100 points. Requirements. • Enter the Student ID first, then prompt for grades. Keep prompt- ing for grades until the client enters ’calc’. That triggers final pro- cessing. • Make your code as reliable as possible. • Make your program output easy to read. • You cannot...
Create a function called time2Greeting. It takes a time (in military time) and returns a string...
Create a function called time2Greeting. It takes a time (in military time) and returns a string with the right greeting. Good Morning 4AM to before noon. Good Afternoon Noon to before 5PM Good Evening from 5PM to 11PM What are you doing up at this hour? between 11 and 4AM For illegal values, say: That is not a valid time. Example: What is your name?   John What time is it? 1315 Good afternoon, John. C++ programming
Create one application question at a time related to the following and ask for the answer....
Create one application question at a time related to the following and ask for the answer. 1. Taylor series 2. Complex Root of Equation-Newton-Rabson Method and/or Muller Method Mixed 3. Solution of a system of linear equations-Must include LU decomposition 4. Eigenvalues and Eigenvectors 5. Curved Connection-Least Squares Regression Method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT