New Android apps UniqueKey

Wednesday, 6 May 2015

FIFO, SJF, Priority Scheduling, Round Robin Scheduling Algorithms

Scheduling algorithms are very important concept in operating system to decide which process has to allocate CPU next. there are many types of Scheduling algorithms e.g. FIFO (first in first out), SJF (Shortest job first), Priority scheduling, RR (round robin), Multilevel queue, Multilevel feedback queue. 
following program describes FIFO, SJF, RR and Priority scheduling algorithms.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

struct templet
{
    char name[8];
    int at,st,pt,ft,tat,wt,priority,remain,flag;
}p[20],temp;


int n,Bu[20],Twt,Ttt,A[10],Wt[10],w,wait[50],turn[50];
float Awt,Att;
char pname[20][20],c[20][20];

void Getdata();
void Gantt_chart();
void Calculate();
void chart();
void fcfs();
void sjf();
void rr();
void prio();
//srand(time(NULL));
int main()
{
    int ch;
    do{
    printf("\n1.FCFS\n2.SJF\n3.Round Robin\n4.Priority\n5.exit");
    printf("\nEnter choice : ");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1: Getdata();
          fcfs();
                break;
        case 2:sjf();
                break;
        case 3: rr();
                break;
        case 4: prio();
                break;
    }}while(ch!=5);
}
void fcfs()
{
 int i,j,temp, temp1;
 Twt=0;
 Ttt=0;
 printf("\n\n FIRST COME FIRST SERVED ALGORITHM\n");
 for(i=1;i<=n;i++)
 {
  for(j=i+1;j<=n;j++)
  {
   if(A[i]>A[j])
   {
    temp=Bu[i];
    temp1=A[i];
    Bu[i]=Bu[j];
    A[i]=A[j];
    Bu[j]=temp;
    A[j]=temp1;
    strcpy(c[i],pname[i]);
    strcpy(pname[i],pname[j]);
    strcpy(pname[j],c[i]);
   }
  }
 }
 Calculate();
 Gantt_chart();
 chart();
}

void sjf()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\n\nEnter process name : ");
        scanf("%s",p[i].name);
        p[i].at=rand()%50;
  printf("\nArrival time is %d ",p[i].at);
        p[i].pt=rand()%50;
        printf("\nEnter Burst time is %d ",p[i].pt);
        
        p[i].remain=p[i].pt;
        p[i].flag=0;
    }
    for(i=0;i<no;i++)
    {
        for(j=0;j<no;j++)
        {
            if(p[i].at<p[j].at)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    printf("\n\t Grant Chart : \n\t\t");
   
    i=0;
    scnt=p[i].at;
    while(1)
    {
        if(p[i].flag==0)
        {
            printf("%s--->",p[i].name);
            p[i].remain--;
            scnt++;
            if(p[i].remain>0)
            {
                for(j=0;j<no;j++)
                {
                    if(p[j].at<=scnt && p[j].flag==0)
                    {
                        if(p[i].remain>p[j].remain)
                        {
                            i=j;
                            printf("- ");
                        }
                    }
                }
            }
            else
            {
                p[i].wt=scnt-p[i].pt-p[i].at;
                p[i].tat=scnt-p[i].at;
                p[i].flag=1;
                fcnt++;
                printf("+");
                if(fcnt==no)
                    break;
                if(i<no-1)
                    i++;
                else
                    i=0;
                for(j=i;j>0;j--)
                        if(p[j].remain < p[i].remain && p[j].flag==0)
                            i=j;
            }
        }
        else
            i++;
    }
   
    printf(" Finish\n");
    fcnt=0,scnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n-------------------------------------------------------------------");
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}

void rr()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0,interval;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\nEnter process name : ");
        scanf("%s",p[i].name);
        //p[i].pt=rand()%50;
  printf("\nenter Burst time : ");
        scanf("%d",&p[i].pt);
        p[i].remain=p[i].pt;
        p[i].flag=0;
    }
    //interval=rand()%10;
 printf("\nenter time quantum : ");
 scanf("%d",&interval);
    
   
    printf("\n\t Grant Chart : \n\t\t");
   
    i=0;
    while(1)
    {
        for(i=0;i<no;i++)
        {
        if(p[i].flag==0)
        {
            if(p[i].remain<=interval)
            {
                printf("-- %s (C)--",p[i].name);
                scnt+=p[i].remain;
                p[i].wt=scnt-p[i].pt;
                p[i].tat=scnt;
                p[i].flag=1;
                fcnt++;
            }
            else
            {
                printf("-- %s --",p[i].name);
                p[i].remain-=interval;
                scnt+=interval;
            }
        }
        }
        if(fcnt==no)
            break;
    }
   
    printf("Finish");
    fcnt=0,scnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n-----------------------------------------------------------------\n");
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}

void prio()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\nEnter process name : ");
        scanf("%s",p[i].name);
        p[i].pt=rand()%50;
  printf("\nBurst time  is %d ",p[i].pt);
        p[i].priority=rand()%10;
        printf("\npriority is %d ",p[i].priority);
        
    }
    for(i=0;i<no;i++)
    {
        for(j=0;j<no;j++)
        {
            if(p[i].priority<p[j].priority)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    printf("\n\t Grant Chart : \n\t\t");
    for(i=0;i<no;i++)
    {
        p[i].st=fcnt;
        fcnt+=p[i].pt;
        p[i].wt=fcnt-p[i].pt;
        p[i].tat=fcnt;
        printf("%s -->",p[i].name);
    }
    printf("Finish");
    fcnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}
void Getdata()
{
 srand(time(0));
 int i;
 printf("\n Enter the number of processes: ");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  fflush(stdin);
  printf("\n Enter the process name: ");
  scanf("%s",&pname[i]);
  Bu[i] = rand()%50;
  printf(" burst time for process %s is %d \n",pname[i],Bu[i]);
  A[i] = rand()%50;
  printf(" Arrival time for process %s is %d \n",pname[i],A[i]);
 }
}

void Gantt_chart()
{
 int i;
 printf("\n\n\t\t\tGANTT CHART\n");
 printf("\n-----------------------------------------------------------\n");
 for(i=1;i<=n;i++)
  printf("|\t%s\t",pname[i]);
 printf("|\t\n");
 printf("\n-----------------------------------------------------------\n");
 printf("\n");
 for(i=1;i<=n;i++)
 printf("%d\t\t",Wt[i]);
 printf("%d",Wt[n]+Bu[n]);
 //turn[i]=Wt[n]+Bu[n];
 printf("\n-----------------------------------------------------------\n");
 printf("\n");
 }

void Calculate()
{
 int i;
 Wt[1]=0;
 wait[1]=0;
 turn[1]=Bu[1];
 for(i=2;i<=n;i++)
 {
  Wt[i]=Bu[i-1]+Wt[i-1];
  wait[i]=Wt[i];
  turn[i]=Wt[i]+Bu[i];
 }
 for(i=1;i<=n;i++)
 {
  Twt=Twt+(Wt[i]-A[i]);
  Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
 }
 Att=(float)Ttt/n;
 Awt=(float)Twt/n;
 printf("\n\n Average Turn around time=%3.2f ms ",Att); 
 printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
}

void chart(){
 int k;
 printf("name\t burst time\t Arrival time \t Waiting time  turnaround time\n");
 for (k=1;k<=n;k++)
 {
  printf("%s\t   %d\t\t %d\t\t\t %d\t\t%d\n",pname[k],Bu[k],A[k],wait[k],turn[k]);
 }
}

No comments:

Post a Comment