C++ program for Longest Remaining Time First in Operating System

0

 


This program takes the number of processes as input and then takes the burst time for each process. The processes are then sorted based on their remaining time using the sort function in C++. The waiting time for each process is calculated by adding the burst time of each process to the current time. Finally, the waiting time for each process is displayed.

This program is a basic implementation of the LRTF scheduling algorithm and can be modified to suit the specific requirements of the system.



#include<iostream>
#include<algorithm>

using namespace std;

// Structure to store the process details
struct Process {
  int pid; // process id
  int burstTime; // burst time of the process
  int remainingTime; // remaining time of the process
  int waitingTime; // waiting time of the process
};


// Function to compare two processes based on their remaining time
bool compareProcess(Process a, Process b) {
  return (a.remainingTime > b.remainingTime);
}

int main() {
  int n; // number of processes
  cout<<"Enter the number of processes: ";
  cin>>n;
  Process processes[n]; // array of processes

  for (int i=0; i<n; i++) {
    processes[i].pid = i+1;
    cout<<"Enter the burst time for process "<<i+1<<": ";
    cin>>processes[i].burstTime;
    processes[i].remainingTime = processes[i].burstTime;

  }

  // Sort the processes based on their remaining time
  sort(processes, processes+n, compareProcess);

  // Initialize the current time
  int currentTime = 0;

  // Calculate the waiting time for each process
  for (int i=0; i<n; i++) {
    processes[i].waitingTime = currentTime - processes[i].burstTime;
    currentTime += processes[i].burstTime;
  }

  // Display the waiting time for each process
  cout<<"Process\tWaiting Time"<<endl;

  for (int i=0; i<n; i++) {
    cout<<processes[i].pid<<"\t\t"<<processes[i].waitingTime<<endl;
  }

  return 0;
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !