Wednesday, October 1, 2014

Write a program in high level language to demonstrate the operation of the following CPU scheduling algorithms.

First Come First Serve
ALGORITHM:
1.      Start
2.      Get the number of processes
3.       
CODE:
#include
#include
void main()
{
     clrscr();
     int Bt[10],At[10],start[10],fin [10],TAT[10],wt[10], n,p[10],i;
     float avgwt,avgtat,totwt,tottat;
     printf("\n Enter number of Processes:");
     scanf("%d",&n);
     for(i=0;i
     {
                 printf("\n Input processes name,Arrival Time &Burst Time ");
                 scanf("%d %d %d",&p[i],&at[i],&bt[i] );
     }
     start[0]=at[0];
     fin [0]=start[0]+bt[0];
     wt[0]=0;
     TAT[0]=fin [0]-at[0];
     tottat=TAT[0];
     for(i=1;i
     {
         start[i]=fin [i-1];
         fin [i]=start[i]+bt[i];
         wt [i]=fin [i-1]-at[i];
         TAT[i]=fin [i]-at[i];
         totwt+= wt[i];
         tottat+=+TAT[i];
     }
     avgwt=totwt/n;
     avgtat=tottat/n;
     printf("Pname\tArrive time\tBurst time\tStart\tTAT\tFinish");
     for(i=0;i
     {
                 printf("\n%d\t%d\t\t%d\t\t%d\t%d\t%d",p[i],at[i],bt[i],start[i],TAT[i],fin [i]);
      }
      printf("\nAverage Waiting=%f",avgwt);
      printf("\nAverage Turn Over Around=%f",avgtat);
     getch();
}
Shortest Job First
ALGORITHM:

CODE:
#include
#include
void main()
{
     clrscr();
     int bt[10],at[10],start[10],fin [10],TAT[10],wt[10], n,p[10],i,j,temp;
     float avgwt,avgtat,totwt,tottat;
     printf("\n Enter number of Processes:");
     scanf("%d",&n);
     for(i=0;i
     {
                 printf("\n Input processes name,Arrival Time &Burst Time ");
                 scanf("%d %d %d",&p[i],&at[i],&bt[i] );
     }
     for(i=0;i
     {
                 for(j=i+1;j
                 {
                             if(bt[i]>bt[j])
                             {
                                         temp=bt[i];
                                         bt[i]=bt[j];
                                         bt[j]=temp;
                                         temp=p[i];
                                         p[i]=p[j];
                                         p[j]=temp;
                                         temp=at[i];
                                         at[i]=at[j];
                                         at[j]=temp;
                             }
                 }
     }

     start[0]=at[0];
     fin [0]=start[0]+bt[0];
     wt[0]=0;
     TAT[0]=fin [0]-at[0];
     tottat=TAT[0];
     for(i=1;i
     {
               start[i]=fin [i-1];
               fin [i]=start[i]+bt[i];
               wt [i]=fin [i-1]-at[i];
               TAT[i]=fin [i]-at[i];
               totwt+= wt[i];
               tottat+=+TAT[i];
     }
     avgwt=totwt/n;
     avgtat=tottat/n;
     printf("Pname\tArrive time\tBurst time\tStart\tTAT\tFinish");
     for(i=0;i
     {
                 printf("\n%d\t%d\t\t%d\t\t%d\t%d\t%d",p[i],at[i],bt[i],start[i],TAT[i],fin [i]);
      }
      printf("\nAverage Waiting=%f",avgwt);
      printf("\nAverage Turn Over Around=%f",avgtat);
     getch();
}

Round Robin
ALGORITHM:

CODE:
#include
#include
#include
#define m 10
void main()
{
         clrscr();
         int bt[m],start=0,est,c, n,p[m],i,q;
         printf("\nEnter the number of Processes:");
         scanf("%d",&n);
         printf("Enter Quantum");
         scanf("%d",&q);
         for(i=0;i
         {
                     printf("Enter Process name,Burst Time ");
                     scanf("%d %d",&p[i],&bt[i] );
         }
         do{
                     for(i=0;i
                     {
                                 if(bt[i]!=0){
                                             est=start+q;
                                             start=est;
                                             printf("\n%d -> %d",i+1,q);
                                             if(bt[i]>0&&bt[i]>=q)
                                                         bt[i]=bt[i]-q;
                                             else
                                                         bt[i]=0;
                     }
                     for(i=0;i
                     {
                                 if(bt[i]==0)
                                             c++;
                     }



         }while(c!=n);
        printf("\n Total Time Estimated=%d",start);
       getch();
}

Priority
ALGORITHM:

CODE:
#include
#include
#define m 10
void main()
{
         clrscr();
         int bt[m],at[m],start[m],fin [m],TAT[m],wt[m],p[m],pr[m],n,i,j,temp;
         float avgwt,avgtat,totwt,tottat;
         printf("\n Enter number of Processes:");
         scanf("%d",&n);
         for(i=0;i
         {
                     printf("\n Enter Process name,Arrival Time,Burst Time and Priority ");
                     scanf("%d %d %d %d",&p[i],&at[i],&bt[i],&pr[i]);
         }
         for(i=0;i
         {
                     for(j=i+1;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;
                                             temp=p[i];
                                             p[i]=p[j];
                                             p[j]=temp;
                                             temp=at[i];
                                             at[i]=at[j];
                                             at[j]=temp;
                                 }
                     }
         }

         start[0]=at[0];
         fin [0]=start[0]+bt[0];
         wt[0]=0;
         TAT[0]=fin [0]-at[0];
         tottat=TAT[0];
         for(i=1;i
         {
      start[i]=fin [i-1];
      fin [i]=start[i]+bt[i];
      wt [i]=fin [i-1]-at[i];
      TAT[i]=fin [i]-at[i];
      totwt+= wt[i];
      tottat+=+TAT[i];
         }
         avgwt=totwt/n;
         avgtat=tottat/n;
         printf("Pname\tArrive time\tBurst time\tPriority\tWaiting time\tTAT");
         for(i=0;i
         {
                     printf("\n%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d",p[i],at[i],bt[i],pr[i],wt[i],TAT[i]);
          }
          printf("\nAverage Waiting=%f",avgwt);
          printf("\nAverage Turn Over Around=%f",avgtat);
         getch();
}


No comments:

Post a Comment