Wednesday, 28 November 2012

Priority schduling algorithm


//Priority schduling algorithm implementation using C++
#include"iostream.h"
#include"conio.h"
#include"string.h"

int total,i,j,n,temp;
float avg;

class priority
{
 int bt[12],wt[12],pr[12];
 char p[12][6], tempc[5];
 public:
 priority()
 {
  wt[0]=0;
  total=0;
  avg=0;
 }
 void input(int n);
 void apply();
 void display();
};

 void priority::input(int n)
 {
  for(i=0;i<n;i++)
  {
   cout<<"Enter process "<<i+1<<" name :";
   cin>>p[i];
   cout<<"Enter Burst time :";
   cin>>bt[i];
   cout<<"enter priority :";
   cin>>pr[i];
  }
 }

void priority:: apply()
{
 for(i=0;i<n-1;i++)
 {
  for(j=i+1;j<n;j++)
  {
   if(pr[i]>pr[j])
   {
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    strcpy(tempc,p[i]);
    strcpy(p[i],p[j]);
    strcpy(p[j],tempc);
   }
  }
 }
 wt[0]=0;
 for(i=1;i<n;i++)
 {
  wt[i]=wt[i-1]+bt[i-1];
  total=total+wt[i];
 }
 avg=(float)total/n;
 for(i=1;i<n;i++)
 {
  wt[i]=wt[i-1]+bt[i-1];
  total=total+wt[i];
 }
  avg=(float)total/n;
}

 void priority::display()
 {
  cout<<"P_name\tP_time\tW_time\tPriority\n";
  for(i=0;i<n;i++)
  cout<<p[i]<<"\t"<<bt[i]<<"\t"<<wt[i]<<"\t"<<pr[i]<<"\n";
 }

void main()
{
 priority priority;
 clrscr();
 cout<<"Enter no. of processes :";
 cin>>n;
 priority.input(n);
 priority.apply();
 priority.display();
 cout<<"total waiting time :"<<total<<"\n";
 cout<<"Average waiting Time :"<<avg;
 getch();
}
//program written by Mars. 20/09/2011. OS lab.

1 comment:

  1. .thank you for sharing useful post.
    web programming tutorial
    welookups

    ReplyDelete

You are welcome, your comment helps me to make this blog more better.