I was looking for a program of CPU Scheduling FCFS in JAVA, but the example i found was complex, So i decided to write it by my own in simple syntax.

FCFS Scheduling

First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to their arrival time. The job which comes first in the ready queue will get the CPU first. The lesser the arrival time of the job, the sooner will the job get the CPU.

Sample input 1 :

Source : GeeksforGeeks

Process No. |  Arrival Time |  Execite Time
     P0     |       0       |       24
     P1     |       0       |        3
     P2     |       0       |        4
My code input format using the given table :
Enter No. of process: 3
Enter Arrival Time & Execute Time (Duration) of each Process
P0  0 24
P1  0 3
P2  0 4

Sample output 1 :

Process No. |   Arrival Time  |   Execite Time   |    Service Time
     P0     |        0        |        24         |        0
     P1     |        0        |        3         |        24
     P2     |        0        |        4         |        27

Average waithing time : 17.0

Sample input 2 :

Now lets input the values in random order, to check if the whole data sorts based on Arrival time !

Process No. |   Arrival Time  |   Execite Time
     P0     |        0        |        5
     P1     |        1        |        3
     P2     |        2        |        8
     P3     |        3        |        6
My code input format using the given table :
Enter No. of process: 4
Enter Arrival Time & Execute Time (Duration) of each Process
P0  2 8
P1  1 3
P2  0 5
P3  3 6

Sample output 2 :

Process No. |  Arrival Time |  Execite Time  |   Service Time
     P2     |       0       |       5        |       0
     P1     |       1       |       3        |       5
     P0     |       2       |       8        |       8
     P3     |       3       |       6        |       16

Average waithing time : 5.75

Explanation :

  1. First we took input the number of process "pn", then going to take Arrival time "at[pn]" and Execute time "et[pn]" for each process.
  2. Sort each array base on Arrival time "at[pn]" as the CPU Scheduling is FCFS. (look Sample 2)
  3. Calculate Service time "st[pn]".
  4. Calculate Average time and print.

Hope this will help you <3 and let me know if you got better solution.