Operating Systems - 8

1. ROUND ROBIN CPU SCHEDULING ALGORITHM

 

#include<stdio.h>

 

int main()

{

      int i, limit, total = 0, x, counter = 0, time_quantum;

      int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];

      float average_wait_time, average_turnaround_time;

      printf("nEnter Total Number of Processes:t");

      scanf("%d", &limit);

      x = limit;

      for(i = 0; i < limit; i++)

      {

            printf("nEnter Details of Process[%d]n", i + 1);

 

            printf("Arrival Time:t");

 

            scanf("%d", &arrival_time[i]);

 

            printf("Burst Time:t");

 

            scanf("%d", &burst_time[i]);

 

            temp[i] = burst_time[i];

      }

 

      printf("nEnter Time Quantum:t");

      scanf("%d", &time_quantum);

      printf("nProcess IDttBurst Timet Turnaround Timet Waiting Timen");

      for(total = 0, i = 0; x != 0;)

      {

            if(temp[i] <= time_quantum && temp[i] > 0)

            {

                  total = total + temp[i];

                  temp[i] = 0;

                  counter = 1;

            }

            else if(temp[i] > 0)

            {

                  temp[i] = temp[i] - time_quantum;

                  total = total + time_quantum;

            }

            if(temp[i] == 0 && counter == 1)

            {

                  x--;

                  printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);

                  wait_time = wait_time + total - arrival_time[i] - burst_time[i];

                  turnaround_time = turnaround_time + total - arrival_time[i];

                  counter = 0;

            }

            if(i == limit - 1)

            {

                  i = 0;

            }

            else if(arrival_time[i + 1] <= total)

            {

                  i++;

            }

            else

            {

                  i = 0;

            }

      }

 

      average_wait_time = wait_time * 1.0 / limit;

      average_turnaround_time = turnaround_time * 1.0 / limit;

      printf("nnAverage Waiting Time:t%f", average_wait_time);

      printf("nAvg Turnaround Time:t%fn", average_turnaround_time);

      return 0;

}

 

 2. PRIORITY CPU SCHEDULING ALGORITHM

 

#include<stdio.h>

 

int main()

{

    int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;

    printf("Enter Total Number of Process:");

    scanf("%d",&n);

 

    printf("\nEnter Burst Time and Priority\n");

    for(i=0;i<n;i++)

    {

        printf("\nP[%d]\n",i+1);

        printf("Burst Time:");

        scanf("%d",&bt[i]);

        printf("Priority:");

        scanf("%d",&pr[i]);

        p[i]=i+1;          

    }

 

    for(i=0;i<n;i++)

    {

        pos=i;

        for(j=i+1;j<n;j++)

        {

            if(pr[j]<pr[pos])

                pos=j;

        }

 

        temp=pr[i];

        pr[i]=pr[pos];

        pr[pos]=temp;

 

        temp=bt[i];

        bt[i]=bt[pos];

        bt[pos]=temp;

 

        temp=p[i];

        p[i]=p[pos];

        p[pos]=temp;

    }

 

    wt[0]=0;

    for(i=1;i<n;i++)

    {

        wt[i]=0;

        for(j=0;j<i;j++)

            wt[i]+=bt[j];

 

        total+=wt[i];

    }

 

    avg_wt=total/n;      

    total=0;

 

    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");

    for(i=0;i<n;i++)

    {

        tat[i]=bt[i]+wt[i];      

        total+=tat[i];

        printf("\nP[%d]\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);

    }

 

    avg_tat=total/n;      

    printf("\n\nAverage Waiting Time=%d",avg_wt);

    printf("\nAverage Turnaround Time=%d\n",avg_tat);

 

return 0;

}

Comments

Popular posts from this blog

Computer Architecture and Organization - 4

Design and Analysis of Algorithms - 2

Design and Analysis of Algorithms - 6

Design and Analysis of Algorithms - 1

Artificial Intelligence - 7