SHORTEST JOB FIRST SCHEDULING

DATE : 29/09/2020

Aim

To implement process scheduling using shortest job first scheduling algorithm

Algorithm

  1. Start
  2. Declare the array size
  3. Read the number of processes to be inserted
  4. Read the arrival time of processes
  5. Read the burst time of processes
  6. Calculate turnaround time of process ta[c]=ct[c]-at[c]
  7. Calculate waiting time of processes wt[c]=ta[c]-bt[c]
  8. Calculate the average waiting time and average turnaround time
  9. Display the values
  10. Stop

Program

#include<stdio.h>

int main() {
    int pid[20] = {
  0
    }, at[10] = {
      0
    }, bt[10] = {
      0
    }, ct[10] = {
      0
    }, ta[10] = {
      0
    }, wt[10] = {
      0
    }, f[10] = {
      0
    };
    int n, st = 0, tot = 0;
    float avgwt = 0, avgta = 0;
    printf("Enter total number of processes ");
    scanf("%d", & n);
    printf("Enter arrival time and burst time for each process\\n\\n");
    for (int i = 0; i < n; i++) {
      printf("Arrival time of process[%d] scanf(" % d ",&at[i]);
        printf("Burst time of process[%d] ", i + 1); scanf("%d", & bt[i]); printf("\\n"); pid[i] = i + 1; f[i] = 0;
      }
      while (1) {
        int c = n, min = 999;
        if (tot == n) break;
        ",i+1);
        for (int i = 0; i < n; i++) {
          if ((at[i] <= st) && (f[i] == 0) && (bt[i] < min)) {
            min = bt[i];
            c = i;

          }
        }
        if (c == n)
          st++;
        else {
          ct[c] = st + bt[c];
          st += bt[c];
          ta[c] = ct[c] - at[c];
          wt[c] = ta[c] - bt[c];
          f[c] = 1;
          tot++;
        }
      }
      printf("\\npid arrival burst complete turn waiting\\n");
      for (int i = 0; i < n; i++) {
        avgwt += wt[i];
        avgta += ta[i];
        printf("P%d\\t %d\\t %d\\t %d\\t %d\\t %d\\n", i + 1, at[i], bt[i], ct[i], ta[i], wt[i]);
      }
      printf("\\naverage tat is %f", (float)(avgta / n));
      printf("\\naverage wt is %f", (float)(avgwt / n));
    }

Output

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/96071a4a-a3ef-4baa-b935-4d9f4b72afa1/Screen_Shot_2020-12-04_at_10.22.11_AM.png

Result