Showing posts with label scheduling. Show all posts
Showing posts with label scheduling. Show all posts

Saturday, August 9, 2014

Round Robin and Priority Scheduling Program

This is an operating systems lab program to implement round robin as well as priority scheduling. If you have any doubts please let me know.

#include<stdio.h>

struct process
{
char p[10];
int at,bt,pr,wt,tt,r,ar;
}a[100],temp;
int n;

void sorta()
{
    int i,j;
    for(i=0;i<n-1;i++)
    {
         for(j=0;j<n-i-1;j++)
         {
            if(a[j].at>a[j+1].at)
           {
               temp=a[j+1];
               a[j+1]=a[j];
               a[j]=temp;
           }
         }
    }
}

void sortp()
{
      int i,j;
      for(i=0;i<n-1;i++)
      {
           for(j=0;j<n-i-1;j++)
           {
               if(a[j].pr>a[j+1].pr)
              {
                   temp=a[j+1];
                   a[j+1]=a[j];
                   a[j]=temp;
              }
           }
      }
}


void sortar()
{
   int i,j;
   for(i=0;i<n-1;i++)
   {
        for(j=0;j<n-i-1;j++)
        {
             if((a[j].ar>a[j+1].ar)||(a[j].ar==a[j+1].ar&&a[j].ar!=a[j].at))
             {
    temp=a[j+1];
                a[j+1]=a[j];
                 a[j]=temp;
}
        }
   }
}

void main()
{
         int i,c,t=0,f1=1,f=0,tq=0,cf,nt=0;
         float aw=0,at=0;
         printf("Enter the number of Processes: ");
         scanf("%d",&n);
         printf("Enter the process details");
        for(i=0;i<n;i++)
        {
printf("\nProcess ID:");
scanf("%s",&a[i].p);
printf("\nPriority:");
scanf("%d",&a[i].pr);
printf("\nArrival time:");
scanf("%d",&a[i].at);
printf("\nBurst time:");
scanf("%d",&a[i].bt);

a[i].wt=a[i].tt=0;
a[i].r=a[i].bt;
a[i].ar=a[i].at;
         }
printf("\nMENU:\n1.Priority\n2.Round Robin\n");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
  sorta();
  sortp();
  printf("\nGantt Chart for Priority\n");
  printf("%d",t);
  while(f1==1)
  {
     f1=0;
     for(i=0;i<n;i++)
     {
          if(a[i].at<=t&&a[i].r!=0)
          {
            a[i].wt=t-a[i].at;
            aw+=a[i].wt;
            printf("-----%s",a[i].p);
            t=t+a[i].bt;
            printf("--------");
            printf("%d",t);
            a[i].tt=t-a[i].at;
             at+=a[i].tt;
            a[i].r=0;
            f=0;
            f1=1;
            break;
           }
            else if(f==0&&a[i].r!=0)
            {
            nt=a[i].at;
            f=f1=1;
            }
            else if(nt>a[i].at&&a[i].r!=0)
 
                nt=a[i].at;
       }
     if(f==1)
     {
            t=nt;
            printf(" %d",t);
            f=0;
     }
  }

  printf("\nAverage waiting time=%f",(aw/n));
  printf("\nAverage turn around time=%f\n",(at/n));
  break;
case 2:
printf("\nEnter the time quantum:");
scanf("%d",&tq);
if(tq>0)
{
sorta();
printf("\nGantt Chart for Round Robin\n");
printf("%d",t);
while(f1==1)
{
   cf=0;
   f1=0;
   sortar();
   for(i=0;i<n;i++)
   {
         if(a[i].at<=f&&a[i].r!=0)
        {
a[i].wt+=t-a[i].tt;
printf("------%s",a[i].p);
printf("------");
if(a[i].r<tq)
{
    t=t+a[i].r;
    a[i].r=0;
}
        else
        {
t=t+tq;
a[i].r-=tq;
         }
          printf("%d",t);
          a[i].tt=a[i].ar=t;
         if(cf==0)
        {
cf=1;
f=t;
         }
  }
  if(a[i].r!=0&&f1==0)
  f1=1;
  if(a[i].at>f)
  break;
}
if(cf==0&&f1==1)
  {
      t=f=a[i].at;
      printf(" %d",t);
   }
}
for(i=0;i<n;i++)
{
a[i].wt=a[i].wt-a[i].at;
a[i].tt=a[i].tt-a[i].at;
aw+=a[i].wt;
at+=a[i].tt;
}


printf("\nAverage waiting time=%f",(aw/n));
  printf("\nAverage turn around time=%f\n",(at/n));
      }
else
printf("\nTime quantum is wrong");
break;
default:
printf("\nWrong Choice\n");  
break;
   }
}


FCFS and SJF program

 This is a program for the implementation of First Come First Serve and Shortest Job First Scheduling. If you have any doubts, please let me know.

#include<stdio.h>
#include<string.h>
struct process
{
   int arr,bur,wait,turn,t;
   char pid[5];
}p[100],temp;

int n;
void chart()
{
            int i,j,k;
            printf("0");
            for(j=0;j<n;j++)
            {
                        i=0;
                        while(i!=p[j].bur)
                        {
                                    printf("=");
                                    i++;
                        }
            printf("%d",p[j].t);
            }
            printf("\n");
            i=0;
            printf("p%s",p[0].pid);
           
            for(j=0,k=1;j<n-1;j++,k++)
            {
                        while(i!=p[j].t)
                        {
                                    printf(" ");
                                    i++;
                        }
            printf("p%s",p[k].pid);
            }
            printf("\n");

}

void avg()
    {
        int i;
        float avgw=0,avgt=0;
        for(i=0;i<n;i++)
       {
        avgw=avgw+p[i].wait;
        avgt=avgt+p[i].turn;
      }
    avgw=avgw/n;
    avgt=avgt/n;

    printf("\nAverage waiting time :%d\nAverage turnaround time :%d\n",avgw,avgt);
 }


void disp()
{
         printf("\npid\tburst\tarrival\twait\tturnaround\n");
         int i;
         for(i=0;i<n;i++)
            {
            printf("%s\t%d\t%d\t%d\t%d\n",p[i].pid,p[i].bur,p[i].
arr,p[i].wait,p[i].turn);
        }
    avg();
    chart();

}

void fcfs()
{
        int i,j;
         for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
               {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
             }
        }
        p[0].wait=0;
        p[0].turn=p[0].wait+p[0].bur;
                        p[0].t=p[0].bur;

        for(i=1;i<n;i++)
       {
            p[i].t=p[i-1].t+p[i].bur;
            p[i].wait=p[i-1].t-p[i].arr;
            p[i].turn=p[i].wait+p[i].bur;
      }
                        disp();
}


void sjf()
{
            int i,j;
            for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
            }
            for(i=1;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].bur>p[j].bur)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
          }

            p[0].wait=0;
            p[0].turn=p[0].wait+p[0].bur;
            p[0].t=p[0].bur;
            for(i=1;i<n;i++)
            {
               p[i].t=p[i-1].t+p[i].bur;
               p[i].wait=p[i-1].t-p[i].arr;
               p[i].turn=p[i].wait+p[i].bur;
            }
                disp();
}
void main()
{
    int j,i,ch;
    printf("Enter number of processess : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
            printf("Enter pid : ");
            scanf("%s",p[i].pid);
            printf("enter arrival time : ");
            scanf("%d",&p[i].arr);
            printf("Enter burst time : ");
            scanf("%d",&p[i].bur);
        }


    printf("\nMENU\n1.FCFS\n2.SJF\n3.exit\nEnter your choice : ");
    scanf("%d",&j);
    switch(j)
    {
    case 1:fcfs();
              break;
    case 2:  sjf();
              break;
    case 3:
          break;
    default : printf("Wrong Choice");
    }
}