Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ady-31 opened this issue Oct 22, 2024 · 1 comment

Comments

@ady-31
Copy link

ady-31 commented Oct 22, 2024

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
Copy link
Author

ady-31 commented Oct 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant