Question

Write a program in Java that will simulate FCFS (First Come First Serve) considering context switching is 0. The program should compute waiting time and turnaround time of every job as well as the average waiting time and average turnaround time. Then change the context switching time to 0.4 milliseconds. Arrival time CPU Burst (in milliseconds Process 0) P2 P3 4 100 P6

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

Dear student,

as per property of FCFS, the process coming first will be executed completely without any interruption or context switching.

The java program is provided below. It simulate FCFS, which sorts processes according to their arrival time and executes and computes waiting & arrival time. It also calculates their average waiting and average arrival time.

import java.util.Scanner;

public class FCFS {

   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
       int n, i, j, temp, time=0;
       float avg_wt=0, avg_tt=0;
       System.out.println("Enter no. of processes");
       n = sc.nextInt();
      
       int at[] = new int[n+1]; // Arrival time
       int ft[] = new int[n+1]; // Finish time
       int tt[] = new int[n+1]; // Turn around Time
       int wt[] = new int[n+1]; // Waiting time
       int ct[] = new int[n+1]; // CPU/Burst time
       String p[] = new String[n+1]; // Process name
       String ts;
      
       System.out.println("Insert details of processes as (Arrival_time, CPU_time)");
      
       for(i=1;i<=n;i++)
       {
           System.out.println("Process "+i);
           at[i] = sc.nextInt();
           ct[i] = sc.nextInt();
           ft[i] = 0;
           wt[i] = 0;
           tt[i] = 0;
           p[i] = "p"+i;
       }
      
       // sorts processes according to their arrival times
       for(i=1;i<n;i++)
       {
           for(j=0;j<n;j++)
           {
               if(at[j]>at[j+1])
               {
                   temp = at[j];
                   at[j] = at[j+1];
                   at[j+1] = temp;
                  
                   temp = ct[j];
                   ct[j] = ct[j+1];
                   ct[j+1] = temp;  
                  
                   ts = p[j];
                   p[j] = p[j+1];
                   p[j+1] = ts;
               }
           }
       }
      
       // Process each task
       for(i=1;i<=n;i++)
       {
           if(at[i]<=time)
           {
               time += ct[i];
               ft[i] += time;
               tt[i] = ft[i]-at[i];
               wt[i] = tt[i]-ct[i];
               avg_tt += tt[i];
               avg_wt += wt[i];
           }
           else
           {
               while(at[i]>time)
                   time++;
               time += ct[i];
               ft[i] = time;
               tt[i] = ft[i]-at[i];
               wt[i] = tt[i]-ct[i];
               avg_tt += tt[i];
               avg_wt += wt[i];              
           }

       }
      
       avg_tt /= n;
       avg_wt /= n;
       System.out.println("\nProcess AT \tFT \tCT \tTT \tWT");
       for(i=1;i<=n;i++)
       {
           System.out.println(p[i]+"\t"+at[i]+"\t"+ft[i]+"\t"+ct[i]+"\t"+tt[i]+"\t"+wt[i]);
       }
      
       System.out.println("\nAverage turnaound time = "+avg_tt);
       System.out.println("Average waiting time = "+avg_wt);
      
       sc.close();
   }
}

The output of this code for example provided by you is given below.....

Enter no. of processes
6
Insert details of processes as (Arrival_time, CPU_time)
Process 1
0 6
Process 2
3 2
Process 3
5 1
Process 4
9 7
Process 5
10 4
Process 6
11 3

Process AT    FT    CT    TT    WT
p1   0   6   6   6   0
p2   3   8   2   5   3
p3   5   9   1   4   3
p4   9   16   7   7   0
p5   10   20   4   10   6
p6   11   23   3   12   9

Average turnaound time = 7.3333335
Average waiting time = 3.5

Add a comment
Know the answer?
Add Answer to:
Write a program in Java that will simulate FCFS (First Come First Serve) considering context switching...
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
  • Write a Java program that will simulate SRTnext scheduling algorithms. For the algorithm, the pro...

    Write a Java program that will simulate SRTnext scheduling algorithms. For the algorithm, the program should compute waiting time and turnaround time of every job as well as the average waiting time and average turn around time Process Arrival time CPU Burst (in milliseconds) 6 P1 P2 P3 7 10 Ps Write a Java program that will simulate SRTnext scheduling algorithms. For the algorithm, the program should compute waiting time and turnaround time of every job as well as the...

  • Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS)...

    Please help me create this CLI CPU Scheduling Simulator in java: First Come First Serve (FCFS) Round Robin (RR) Process information The process information will be read from an input file. The format is: pid arrival_time burst_time All of fields are integer type where: pid is a unique numeric process ID arrival_time is the time when the task arrives in the unit of milliseconds burst_time the is the CPU time requested by a task, in the unit of milliseconds The...

  • Given the following set of processes with corresponding execution times (in ms), arrival times and priority...

    Given the following set of processes with corresponding execution times (in ms), arrival times and priority (1 – highest).  For each scheduling algorithm: Construct a table showing which process is active and for how long until all processes are completely serviced (as done in class). Calculate the average waiting time and turnaround time. Process ID Burst (ms) Arrival time P1 9 0 P2 12 0 P3 3 0 P4 30 0 P5 20 0 P6 10 0 First Come First Serve...

  • Consider the below table. Calculate the below given questions using FCFS and Shortest Remaining Time First...

    Consider the below table. Calculate the below given questions using FCFS and Shortest Remaining Time First Algorithm. S# Process ID Burst Time Arrival Time 1 P1 8 0 2 P2 6 0 3 P3 10 0 4 P4 2 2 5 P5 3 4 1) What is the average waiting time for all processes using FCFS? 2) What is the turnaround time for P5 using FCFS? 3) What is the Response time for P1 using FCFS? 4) What is the...

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:Processburst TimePriorityP1103P211P323P414P552For each of the scheduling algorithms, FCFS, Shortest-Job-First (SJF, non-preemptive), Priority (smaller priority number implies higher scheduling priority), and RR (quantum = 1) do the following.Draw a Gantt chart to show how these processes would be scheduled.Give the turnaround time (total time from the first arrival into ready state until CPU-burst is completed) of each process.Give the waiting time (total time spent in the Ready state) of each process.Give...

  • Implement a First Come First Serve (FCFS) non-preemptive OS scheduling simulation with multiple processes, in python...

    Implement a First Come First Serve (FCFS) non-preemptive OS scheduling simulation with multiple processes, in python or c++. The simulation takes the 3 processes(listed below) and they all arrive at time 0. A single process consists of {CPU BURST, I/O BURST, CPU BURST, I/O BURST, etc…} The simulation accounts for all 3 processes, and does not run one process at a time. Process1 {4,24,5,73,3,31,5,27,4,33,6,43,4,64,5,19,2} Process2 {18,31,19,35,11,42,18,43,19,47,18,43,17,51,19,32,10} Process3 {6,18,4,21,7,19,4,16,5,29,7,21,8,22,6,24,5} For example at time 0, Process1 arrives and enters running state, Process2...

  • Assume that the three processes arrived in order: Processes                            CPU Burst Time P1  &n

    Assume that the three processes arrived in order: Processes                            CPU Burst Time P1                                           17 P2                                            6 P3                                            8 a.     Please draw the Gantt chart if FCFS scheduling is used. b.    Please calculate the average waiting time and average completion time under FCFS. You MUST show the calculation procedure. c.      Please draw the Gantt chart if Round Robin is used. d.    Please calculate the average waiting time and completion time under RR with q = 3. You MUST show the calculation...

  • Please answer the following question in C++ language Consider the following set of processes, with the...

    Please answer the following question in C++ language Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process            Burst Time      Priority P1. 7 5 P2 2 4 P3 11 3 P4 9 1 P5 5 3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive...

  • Implement the following 3 CPU scheduling algorithms Simulate and evaluate each with the set of eight...

    Implement the following 3 CPU scheduling algorithms Simulate and evaluate each with the set of eight processes below. Use any programming language. The program listing should be submitted with the report. FCFS (First Come First Serve) non-preemptive (partial results provided) SJF non-preemptive MLFQ Multilevel Feedback Queue (absolute priority in higher queues)             Queue 1 uses RR scheduling with Tq = 5             Queue 2 uses RR scheduling with Tq = 10             Queue 3 uses FCFS All processes enter first...

  •          Process                    Arrival Time            &nbsp

             Process                    Arrival Time                             Burst Time                         P1                                0                                              2                         P2                                1                                              1                         P3                                2                                              4                         P4                                3                                              2                         P5                                4                                              1 Consider a set of processes given above with the arrival time and the length of CPU burst time given in cycles. Show a Gantt chart and calculations of average turnaround time and average waiting time of these processes under the shortest job first (SJF) with preemption CPU scheduling algorithm and the round-robin...

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