The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Sunday, August 7, 2011

Priority CPU Scheduling Algorithm


#include<stdio.h>
#include<conio.h>
#define max 10

int i,j,nop;

struct process
{
 int prn;
 int prin;
 int bt;
 int wt;
 int tt;
}pr[max],temp;

void ins_dat()
{
 for(i=0;i<nop;i++)
 {
  pr[i].prn = i+1;
  printf("\n Enter the Burst time and Priroty Number of Process #%d : ",pr[i].prn);
  scanf("%d %d",&pr[i].bt,&pr[i].prin);
 }
}

void Priority_scheduling()
{
 for(i=0;i<nop;i++)
 {
  for(j=0;j<(nop-1)-i;j++)
  {
   if(pr[j].prin > pr[j+1].prin)
   {
    temp = pr[j];
    pr[j] = pr[j+1];
    pr[j+1] = temp;
   }
  }
 }
 for(i=0;i<nop;i++)
 {
  pr[i].wt = i == 0 ? 0 : pr[i-1].tt ;
  pr[i].tt = pr[i].wt + pr[i].bt;
 }
}

void disp_dat()
{
 int ttl_tt=0,ttl_wt=0;
 float avg_tt,avg_wt;
 printf("\n\n Process   Waiting Time   Burst Time   Turnaround Time \n");
 printf(" ------------------------------------------------------- \n");
 for(i=0;i<nop;i++)
 {
  printf(" %d \t\t %d \t\t %d \t\t %d\n\n",pr[i].prn,pr[i].wt,pr[i].bt,pr[i].tt);
  ttl_wt += pr[i].wt;
  ttl_tt += pr[i].tt;
 }
 avg_wt = (float) ttl_wt/nop;
 avg_tt = (float) ttl_tt/nop;
 printf("\n\n Average Waiting Time  : %f",avg_wt);
 printf("\n Average Turnaround Time : %f",avg_tt);
}

void main()
{
 do
 {
 clrscr();
 printf("\n Enter the number of processes : ");
 scanf("%d",&nop);
 }while(nop > max || nop <= 0);
 ins_dat();
 Priority_scheduling();
 disp_dat();
 getch();
}

No comments:

Post a Comment