cyberbuddy-io / open-source-contribution-for-beginners

This repository targets beginners to guide them about open source and help them make their first contribution.
https://opensource.cyberbuddy.live/contributors/
MIT License
93 stars 169 forks source link

Improving FCFS Scheduling Algorithm: Handling Arrival Times and Execution Metrics #472

Open ady-31 opened 1 week ago

ady-31 commented 1 week ago

import java.util.Scanner;

class Process { int pid; // process ID int at; // arrival time int bt; // burst time int ct; // completion time int wt; // waiting time int tat; // turnaround time }

public class FCFS { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);

    // Read in data
    System.out.println("Give number of processes: ");
    int n = scanner.nextInt();
    Process[] arr = new Process[n];

    for (int i = 0; i < n; ++i) {
        arr[i] = new Process();
        System.out.println("Give ID of process " + (i + 1) + ":");
        arr[i].pid = scanner.nextInt();
        System.out.println("Give arrival time of process " + (i + 1) + ":");
        arr[i].at = scanner.nextInt();
        System.out.println("Give burst time of process " + (i + 1) + ":");
        arr[i].bt = scanner.nextInt();
    }

    int currentTime = 0; // Current time
    for (int i = 0; i < n; i++) {
        // Wait for the process to arrive
        if (currentTime < arr[i].at) {
            currentTime = arr[i].at; // Jump to the arrival time of the next process
        }
        arr[i].ct = currentTime + arr[i].bt; // Completion time
        currentTime = arr[i].ct; // Move time forward
    }

    System.out.println("\nProcess ID | Arrival Time | Burst Time | Completion Time | Waiting Time | Turnaround Time");
    for (int i = 0; i < n; i++) {
        arr[i].tat = arr[i].ct - arr[i].at; // Turnaround time
        arr[i].wt = arr[i].tat - arr[i].bt; // Waiting time

        System.out.printf("%-11d | %-12d | %-10d | %-15d | %-12d | %-15d\n", 
            arr[i].pid, arr[i].at, arr[i].bt, arr[i].ct, arr[i].wt, arr[i].tat);
    }

    scanner.close();
}

}

This title reflects the focus on enhancing the algorithm's accuracy and performance metrics.

ady-31 commented 1 week ago

This title reflects the focus on enhancing the algorithm's accuracy and performance metrics.