diff --git a/Miscellaneous Algorithms/longest remaining time first/program.c b/Miscellaneous Algorithms/longest remaining time first/program.c new file mode 100644 index 00000000..1257508d --- /dev/null +++ b/Miscellaneous Algorithms/longest remaining time first/program.c @@ -0,0 +1,68 @@ +#include + +struct Process { + int pid; + int arrivalTime; + int burstTime; + int remainingTime; + int completionTime; + int waitingTime; + int turnaroundTime; +}; + +void calculateLRTF(struct Process processes[], int n) { + int time = 0, completed = 0, maxIndex; + while (completed != n) { + maxIndex = -1; + for (int i = 0; i < n; i++) { + if (processes[i].arrivalTime <= time && processes[i].remainingTime > 0) { + if (maxIndex == -1 || processes[i].remainingTime > processes[maxIndex].remainingTime) { + maxIndex = i; + } + } + } + + if (maxIndex == -1) { + time++; // No process is ready to execute + } else { + processes[maxIndex].remainingTime--; + time++; + + if (processes[maxIndex].remainingTime == 0) { // Process completed + completed++; + processes[maxIndex].completionTime = time; + processes[maxIndex].turnaroundTime = processes[maxIndex].completionTime - processes[maxIndex].arrivalTime; + processes[maxIndex].waitingTime = processes[maxIndex].turnaroundTime - processes[maxIndex].burstTime; + } + } + } +} + +void displayProcesses(struct Process processes[], int n) { + printf("PID\tArrival\tBurst\tCompletion\tTurnaround\tWaiting\n"); + for (int i = 0; i < n; i++) { + printf("%d\t%d\t%d\t%d\t\t%d\t\t%d\n", + processes[i].pid, processes[i].arrivalTime, processes[i].burstTime, + processes[i].completionTime, processes[i].turnaroundTime, + processes[i].waitingTime); + } +} + +int main() { + int n; + printf("Enter the number of processes: "); + scanf("%d", &n); + + struct Process processes[n]; + for (int i = 0; i < n; i++) { + processes[i].pid = i + 1; + printf("Enter arrival time and burst time for process %d: ", processes[i].pid); + scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime); + processes[i].remainingTime = processes[i].burstTime; + } + + calculateLRTF(processes, n); + displayProcesses(processes, n); + + return 0; +} diff --git a/Miscellaneous Algorithms/longest remaining time first/readme.md b/Miscellaneous Algorithms/longest remaining time first/readme.md new file mode 100644 index 00000000..cf5d03c8 --- /dev/null +++ b/Miscellaneous Algorithms/longest remaining time first/readme.md @@ -0,0 +1,61 @@ +Longest Remaining Time First (LRTF) is a CPU scheduling algorithm used to manage process execution in operating systems. It’s a variant of Shortest Job Next (SJN) but is preemptive and prioritizes processes based on the remaining burst time, rather than the shortest. + +Key Concepts of LRTF +1.Preemptive Scheduling: +LRTF is a preemptive version of the Longest Job First (LJF) scheduling algorithm. This means that if a new process with a longer remaining time arrives, it can interrupt the currently executing process. +This contrasts with non-preemptive scheduling (like First Come First Serve) where a process must complete once it starts, regardless of new processes arriving. +2.Remaining Burst Time: +Burst time is the amount of time a process requires to complete. +LRTF tracks the remaining burst time of all processes in the system, choosing the one with the longest remaining burst time for execution. If a process with a longer burst time arrives, the CPU shifts focus to this process. +3.Arrival Time: +Each process has an arrival time, indicating when it becomes available in the system. LRTF schedules based only on processes that have arrived by the current time. +CPU Utilization: + +The CPU scheduler continuously checks the remaining burst times of the ready processes. +Since LRTF may lead to frequent switching (if longer processes continually arrive), there could be an increase in CPU overhead from these context switches. + +How LRTF Works +Process Arrival: + +Processes arrive in the system at their designated arrival times. +Checking for the Longest Remaining Time: + +At each time unit, the scheduler identifies the process with the longest remaining burst time among those that have already arrived. +If a new process with a longer remaining burst time than the current process arrives, it interrupts the current process, which goes back to the ready queue with its updated remaining time. +Completion: + +When a process completes (remaining burst time becomes zero), it’s removed from the ready queue. +The scheduler then checks for the next longest remaining time among the remaining processes. +Metrics Calculated: + +Completion Time: The time at which a process finishes its execution. +Turnaround Time:Completion Time−Arrival Time. This represents the total time taken from arrival to completion. +Waiting Time: +Turnaround Time−Burst Time. This indicates how long a process spent waiting in the ready queue. + + +Advantages of LRTF +Effective for Longer Processes: + +LRTF prioritizes longer processes, which may benefit them as they get CPU time sooner. +Potential for Shorter Average Waiting Time for Small Jobs: + +If there are frequent preemptions, short jobs can complete without being delayed by longer jobs. +Flexibility and Responsiveness: + +By preempting based on remaining time, LRTF can dynamically adjust priorities. +Disadvantages of LRTF +High Overhead: + +LRTF requires frequent checking of remaining times, and process switching can be intensive, leading to CPU overhead from context switching. +Poor Response Time for Short Jobs: + +Short processes may experience significant delays if they have to wait for longer processes to complete. +Starvation Risk: + +Short processes may be repeatedly preempted by longer jobs, causing potential starvation. +Complexity: + +Tracking and comparing remaining times continuously adds complexity to the scheduling mechanism. +When to Use LRTF +LRTF is best suited for environments where long-running processes need priority or where we want to avoid short processes monopolizing the CPU. However, for general-purpose systems, other algorithms like Round Robin or SJF may offer better performance and fairness.