Labels

C program for preemptive priority CPU scheduling algorithm

Introduction to preemptive priority scheduling :

                     The SJF algorithm is a special case of the general priority-scheduling algorithm. A priority is associated with each process, and the CPU is allocated to the process with the highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the CPU burst, the lower the priority, and vice versa. In this case, preemptive execution means the execution of a process will be deferred at any time when a process with higher priority comes.


C program for preemptive priority scheduling :


#include<stdio.h>
struct sjf
{
       int bt,at,wt,st,pno,tt,cbt,pr;
};

int get(struct sjf arr[],int t,int n)
{
       int imin,min=9999,i;
       for(i=0;i<n;i++)
       {
              if(arr[i].at<=t&&arr[i].st==0)
                     if(min>arr[i].pr)
                     {
                           min=arr[i].pr;
                           imin=i;
                     }
       }
       return imin;
}

void gantt_chart(struct sjf arr[],int p[],int n,int nop)
{
       int i,a[100],s=0;
       float avgtt=0,avgwt=0;
       for(i=0;i<n;i++)
       {
              while(i<n-1&&p[i]==p[i+1])
              {
                     s++;
                     i++;
              }
              s++;
              arr[p[i]].wt=s-arr[p[i]].at-arr[p[i]].tt;
       }
       for(i=0;i<nop;i++)
       {
              arr[i].tt+=arr[i].wt;
              avgwt+=arr[i].wt;
              avgtt+=arr[i].tt;
       }
       printf("Process\tArrival Time\tBurst Time\tTurnaround Time\tWaiting Time\n");
       for(i=0;i<nop;i++)
       {
              printf("[P%d]\t%d\t\t%d\t\t%d\t\t%d\n",arr[i].pno,arr[i].at,
                          arr[i].cbt,arr[i].tt,arr[i].wt);
       }
       avgwt = avgwt/nop;
       avgtt = avgtt/nop;
       printf("Average Waiting Time : %.2f\n",avgwt);
       printf("Average Turnaround Time : %.2f\n",avgtt);
       return;
}

int iscomplite(struct sjf arr[],int n)
{
       int i;
       for(i=0;i<n;i++)
              if(arr[i].st==0)
                     return 0;
       return 1;
}
int main()
{
       int n,i,a,t=0,j;
       int p[100];
       float avgwt=0,avgtt=0;
       struct sjf arr[100];
       printf("Enter Number of Processes\n");
       scanf("%d",&n);

     for(i=0;i<n;i++)
       {
              printf("Enter Arrival Time, priority & Burst Time for Process [P%d]\n",i);
              scanf("%d%d%d",&arr[i].at,&arr[i].pr,&arr[i].bt);
              arr[i].pno = i;
              arr[i].cbt = arr[i].bt;
              arr[i].st=0;
              arr[i].tt=arr[i].bt;
              arr[i].wt=0;
       }
       i=0;
       while(1)
       {
              if(iscomplite(arr,n))
                     break;
              a=get(arr,t,n);
              p[i]=a;
              arr[a].bt-=1;
              if(arr[a].bt==0)
                     arr[a].st=1;
              t=t+1;
              i++;
       }
       gantt_chart(arr,p,i,n);
       return 0;
} 




OUTPUT :




1 comment: