FCFS algorithm in Kotlin | CPU scheduling | First Come First Serve

CPU scheduling is a process that determines the order in which processes will be executed on the CPU. There are various scheduling algorithms, and one of the simplest algorithms is the First Come First Serve (FCFS) algorithm.

In the FCFS algorithm, the processes are executed in the order in which they arrive. Each process is assigned a burst time, which is the amount of time it takes for the CPU to execute the process. Additionally, each process is also assigned an arrival time, which is the time at which the process arrives in the system.

The waiting time for a process is the amount of time it spends waiting in the queue before it can be executed. The turnaround time for a process is the amount of time it takes for the process to complete, including both the execution time and the waiting time.

Here’s an example to illustrate how the FCFS algorithm works:

Suppose we have three processes, P1, P2, and P3, with the following arrival times and burst times:

ProcessArrival TimeBurst Time
P106
P223
P344

To calculate the waiting time and turnaround time for each process, we first need to determine the order in which the processes will be executed. In FCFS, the order is determined by the arrival time, so the processes will be executed in the order P1, P2, P3.

We can then calculate the waiting time and turnaround time for each process using the following formulas:

Waiting time = (time process completes – arrival time) – burst time Turnaround time = time process completes – arrival time

Using these formulas, we can calculate the waiting time and turnaround time for each process as follows:

ProcessArrival TimeBurst TimeWaiting TimeTurnaround Time
P10606
P22347
P34459

Overall, the FCFS algorithm is a simple and intuitive algorithm, but it can lead to long waiting times for processes that arrive later.

The Kotlin Code for FCFS is:

Kotlin
fun main() {
    print("Enter the number of processes: ")
    val n = readLine()?.toIntOrNull() ?: return

    // Create arrays to store arrival time, burst time, waiting time, and turn around time
    val arrivalTime = IntArray(n)
    val burstTime = IntArray(n)
    val waitingTime = IntArray(n)
    val turnAroundTime = IntArray(n)

    // Read arrival time and burst time for each process
    for (i in 0 until n) {
        print("Enter arrival time for process ${i+1}: ")
        arrivalTime[i] = readLine()?.toIntOrNull() ?: return

        print("Enter burst time for process ${i+1}: ")
        burstTime[i] = readLine()?.toIntOrNull() ?: return
    }

    // Sort the processes based on arrival time
    val processes = arrivalTime.indices.sortedBy { arrivalTime[it] }

    // Calculate waiting time and turn around time for each process
    var completionTime = 0
    for (i in processes) {
        while (arrivalTime[i]>completionTime)
            completionTime++
        waitingTime[i] = (completionTime - arrivalTime[i]).coerceAtLeast(0)
        turnAroundTime[i] = waitingTime[i] + burstTime[i]
        completionTime = (completionTime + burstTime[i]).coerceAtLeast(completionTime)
    }

    // Print table with process information
    println("Process\tArrival Time\tBurst Time\tWaiting Time\tTurn Around Time")
    for (i in processes) {
        println("${i+1}\t\t${arrivalTime[i]}\t\t${burstTime[i]}\t\t${waitingTime[i]}\t\t${turnAroundTime[i]}")
    }

    // Print average waiting time
    val avgWaitingTime = waitingTime.average()
    println("Average waiting time: $avgWaitingTime")
    println("Average turn around time: ${turnAroundTime.average()}")
}
  1. The fun main() function is the starting point of the program, which contains all the code that will be executed.
  2. The first line of code in the main() function prompts the user to enter the number of processes.
  3. The readLine() function reads the user input from the console, which is converted to an integer using the toIntOrNull() function. If the user enters an invalid input or no input, the program exits using the return statement.
  4. The next four lines of code create four integer arrays with a length of n, which will be used to store arrival time, burst time, waiting time, and turn around time for each process.
  5. The for loop that follows prompts the user to enter the arrival time and burst time for each process, one by one. The values entered by the user are stored in the corresponding arrays.
  6. After all the values are entered, the processes array is created using the sortedBy() function, which sorts the processes based on their arrival time.
  7. The completionTime variable is initialized to zero, which represents the time when the first process arrives.
  8. The for loop that follows calculates the waiting time and turn around time for each process. For each process i, the loop checks if the arrival time is greater than the completion time, and if so, the completion time is incremented until it is greater than or equal to the arrival time. The waiting time is then calculated as the difference between the completion time and the arrival time, or zero if the process arrives after the completion time. The turn around time is calculated as the sum of the waiting time and the burst time. Finally, the completion time is updated to the sum of the current completion time and the burst time, or the current completion time if the process has already completed.
  9. After all the waiting times and turn around times are calculated, the for loop that follows prints a table with the process information, including the process number, arrival time, burst time, waiting time, and turn around time. The processes array is used to print the processes in the order of their arrival time.
  10. The last two lines of code calculate and print the average waiting time and turn around time, using the average() function on the waitingTime and turnAroundTime arrays.

Overall, this code reads in arrival times and burst times for a set of processes, sorts them by arrival time, calculates waiting times and turn around times for each process using the first come first serve scheduling algorithm, and outputs a table with process information and average waiting and turn around times.

Leave a Comment