Program for Preemptive Priority CPU Scheduling

0

 






#include <bits/stdc++.h>
using namespace std;

struct Process {
  int pid;  // Process ID
  int bt;   // Burst Time
  int priority;  // Priority
};

// Function to compare two processes based on priority
bool comparison(Process a, Process b) {
  return (a.priority > b.priority);
}

void priorityScheduling(Process proc[], int n) {
  sort(proc, proc + n, comparison);

  int total_time = 0;
  for (int i = 0; i < n; i++) {
    total_time += proc[i].bt;
  }

  cout << "Priority Scheduling Result: " << endl;
  int current_time = 0;
  for (int i = 0; i < n; i++) {
    current_time += proc[i].bt;
    cout << "Process ID: " << proc[i].pid << endl;
    cout << "Burst Time: " << proc[i].bt << endl;
    cout << "Turnaround Time: " << current_time << endl;
    cout << "Waiting Time: " << current_time - proc[i].bt << endl;
    cout << endl;
  }
}

int main() {
  Process proc[] = {{1, 10, 3}, {2, 5, 20}, {3, 8, 5}};
  int n = sizeof proc / sizeof proc[0];
  priorityScheduling(proc, n);
  return 0;
}


In this program, the Process structure is used to store the details of each process, such as its ID, burst time, and priority. The comparison function is used to sort the processes in the order of their priority. The priorityScheduling function calculates the turnaround time and waiting time for each process and prints the results.

Post a Comment

0Comments
Post a Comment (0)

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

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