Tuesday 25 September 2012

Pithoijit-Round Robin Scheduling


#include<stdio.h>
#include<conio.h>
int b[15],t[15],z[15],w[15];
int i,j,n,q,max,rr,e;
main()
{
      printf("ROUND ROBIN Job Scheduling Algo");
      printf("\n*******************************");
      printf("\n\nEnter the no. of processes:");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
       {
                        w[i]=0;
       }
      printf("Enter the burst times of the processes \n");
      for(i=1;i<=n;i++)
      {
                       printf("process %d:",i);
                       scanf("%d",&b[i]); z[i]=b[i];
      }
      printf("Enter the time slice:");
      scanf("%d",&q);
      int round(void);
      round();
      getch();
      return 0;
}
int round()
{
    e=0;
    max=b[1];
    for(i=2;i<n;i++)
    if(max<=b[i])
    max=b[i];
    if(max%q==0)
                rr=max/q;
    else
                rr=max/q+1;
             
    for(i=1;i<=rr;i++)
    {
                      printf("Round %d:",i);
                     for(j=1;j<=n;j++)
                     {
                                      if(b[j]>0)
                                      {
                                             
                                                 b[j]=b[j]-q;
                                      if(b[j]>0)
                                      {
                                                printf("\nprocess %d,  time remaining %d",j,b[j]);
                                                e=e+q;
                                      }
                                      else
                                      {
                                            e=e+b[j]+q;
                                             b[j]=0;
                                           printf("\nprocess %d is completed",j);
                                      }
                                      t[j]=e;
                                      }
                     }
                     printf("\n\n\n");
    }
    for(i=1;i<=n;i++)
    {
                     w[i]=t[i]-z[i];
                   
    }
    printf("\nP\t\tb-t\t\tw-t\t\ttr-t");//process,burst time,waiting time,turn around time
    for(i=1;i<=n;i++)
    {
                     printf("\n%d\t\t%d\t\t%d\t\t%d",i,z[i],w[i],t[i]);
    }
    getch();
    return 0;
}

Pithoijit-Priority Queue Shortest Job First Scheduling


#include<stdio.h>
#include<conio.h>
main()
{
      int b1[15],b2[15],a[15],p[15],w[15],t[15],pr[15],pr1[15];
      int n,i,j,k,t1,tbtime=0;
      int i1,j1,e;
      printf("Enter the number of process\n");
      scanf("%d",&n);
      printf("Enter the burst time\n");
      for(i=0;i<n;i++)
      {
      scanf("%d",&b1[i]);
      p[i]=i;
      }
      printf("Enter the arrival time\n");
      for(i=0;i<n;i++)
      {
      scanf("%d",&a[i]);
      }
      printf("Enter priority:\n");
       for(i=0;i<n;i++)
      {
      scanf("%d",&pr[i]);
      }
      for(i=0;i<n;i++)
      {
                      pr1[i]=pr[i];
                      b2[i]=b1[i];
                      tbtime=tbtime+b2[i];
                      w[i]=0;
                      t[i]=0;
      }
      for(i=0;i<tbtime;i++)//time loop
      {
                 
                   
                                           //sorting of left burst time b2[]
                                           for(i1=0;i1<n;i1++)
                                           {
                                                             for(j1=i1+1;j1<n;j1++)
                                                             {
                                                                                   if(pr1[i1]>pr1[j1])
                                                                                   {
                                                                                                     t1=pr1[i1];
                                                                                                     pr1[i1]=pr1[j1];
                                                                                                     pr1[j1]=t1;
                                                                                   t1=b2[i1];
                                                                                   b2[i1]=b2[j1];
                                                                                   b2[j1]=t1;
                                                                                 
                                                                                   t1=a[i1];
                                                                                   a[i1]=a[j1];
                                                                                   a[j1]=t1;
                                                                                 
                                                                                   t1=p[i1];
                                                                                   p[i1]=p[j1];
                                                                                   p[j1]=t1;
                                                                                 
                                                                                   t1=w[i1];
                                                                                   w[i1]=w[j1];
                                                                                   w[j1]=t1;
                                                                                 
                                                                                   t1=b1[i1];
                                                                                   b1[i1]=b1[j1];
                                                                                   b1[j1]=t1;
                                                                                   }
                                                             }
                                           }//sorting end
                     for(j=0;j<n;j++)//process loop
                     {
                                           if((a[j]<=i)&&(b2[j]>0))//checking whether the process has arrived for execution
                                           {
                                                 //t[j]=t[j]+1;
                                                 b2[j]=b2[j]-1;
                                                 e=j;
                                                break;
                                                           
                                           }
                     }
                                         
                                            for(k=0;k<n;k++)
                                                 {
                                                                 if((a[k]<=i)&&(b2[k]>0)&&(k!=e))//for waiting process
                                                                 {
                                                                                                     
                                                                     w[k]=w[k]+1;
                                                                 }
                                                 }
                     // }//end of process loop
      }//end of time loop
      for(i=0;i<n;i++)
      {
        t[i]=w[i]+b1[i];            
      }
      printf("process\t b-t\t a-t\t w-t\t t-t\n");
      for(i=0;i<n;i++)
      {
      printf("%d\t %d\t %d\t %d\t %d\t",p[i],b1[i],a[i],w[i],t[i]);
      printf("\n");
      }
      getch();
}