Priority Based Scheduling

DATE: 06/10/2020


AIM: To perform priority based scheduling

Algorithm

  1. Start
  2. Read the burst time of processes
  3. Declare the array size
  4. Read the number of processes
  5. Read the arrival time of processes
  6. Read the priorities of processes
  7. Sort the priorities and burst time in ascending order
  8. Calculate waiting time for each processes waiting[smallest] = end - a[smallest] - x[smallest]
  9. Calculate turnaround time for each processes turnaround[smallest] = end - a[smallest]
  10. Calculate the average waiting time and average turnaround time
  11. Display the values
  12. Stop

Program

#include<stdio.h>
int main(){
  int a[10], b[10], x[10], pr[10] = {0};
  int waiting[10], turnaround[10], completion[10];
  int i, j, smallest, count = 0, time, n;
  float avg = 0, tt = 0, end;
  printf("\\nEnter total number of Processes: ");
  scanf("%d", & n);
  printf("Enter arrival time of process:\\n");
  for (i = 0; i < n; i++) {
    printf("p(%d):", i + 1);
    scanf("%d", & a[i]);
  }
  printf("Enter burst time of process:\\n");
  for (i = 0; i < n; i++) {
    printf("p(%d):", i + 1);
    scanf("%d", & b[i]);
  }
  printf("Enter priority of process:\\n");
  for (i = 0; i < n; i++) {
    printf("p(%d):", i + 1);
    scanf("%d", & pr[i]);
  }
  for (i = 0; i < n; i++) x[i] = b[i];
  pr[9] = -1;
  for (time = 0; count != n; time++) {
    smallest = 9;
    for (i = 0; i < n; i++) {
      if (a[i] <= time && pr[i] > pr[smallest] && b[i] > 0) smallest = i;
    }
    time += b[smallest] - 1;
    b[smallest] = -1;
    count++;
    end = time + 1;
    completion[smallest] = end;
    waiting[smallest] = end - a[smallest] - x[smallest];
    turnaround[smallest] = end - a[smallest];
  }
  printf("\\nPid Arrival Burst Priority Complete Waiting Turn\\n");
  for (i = 0; i < n; i++) {
    printf("\\np%d\\t %d\\t %d\\t %d\\t %d\\t %d\\t %
      d\\ n ",i+1,a[i],x[i],pr[i],completion[i],waiting[i],turnaround[i]); avg = avg + waiting[i];
      tt = tt + turnaround[i];
    }
    printf("Average waiting time =%f \\n", avg / n);
    printf("Average Turnaround time =%f \\n", tt / n);
  }