Monday, 2 September 2013

PRIORITY BASED CPU SCHEDULING C PROGRAM



/*CSEMATTERBLOGSPOT.IN

Write a program in C to implement priority based CPU scheduling algo
calculate avg. waiting time & average turn around time*/

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
    int gd=DETECT,gm,i,j,k,n,b,count=0,arr[20],prio[20],bt[20],wt[20],tat[20];
    float avg_wt,avg_tat,sum_wt=0.0,sum_tat=0.0;
    initgraph(&gd,&gm,"C:\TC\BIN");
    printf("Enter the no. of processes  ");
    scanf("%d",&n);
    printf("Enter the burst time of processes \n");
    for(i=0;i<n;i++)
        scanf("%d",&bt[i]);
    printf("Enter priority of processes \n");
    for(i=0;i<n;i++)
    {    scanf("%d",&prio[i]);
        arr[i]=prio[i];}
    for(i=0;i<=(n-2);i++)
    {for(j=0;j<=((n-2)-i);j++)
      {   if(arr[j]>arr[j+1])
          {
              b=arr[j];
              arr[j]=arr[j+1];
              arr[j+1]=b;
          }} }
    wt[0]=0;
    tat[0]=0;
    for(i=0;i<n;i++)
    {   for(j=0;j<n;j++)
        {      if(arr[i]!=prio[j])
               count=count+1;
           else
               break;}
        wt[i+1]=bt[count]+wt[i];
        tat[i+1]=bt[count]+tat[i];
        count=0; }
    for(i=0;i<n;i++)
    sum_wt=sum_wt+wt[i];
    for(i=0;i<(n+1);i++)
    sum_tat=sum_tat+tat[i];
    avg_wt=(sum_wt/(float)n);
    avg_tat=(sum_tat/(float)n);
    printf("Process_no. Burst_Time Priority Waiting_Time Turn_Around_Time \n");
    for(i=0;i<n;i++)
    {
    printf("P%d\t\t%d\t%d\t\t%d\t\t%d\n",i,bt[i],prio[i],wt[i],tat[i+1]);}
    printf("Average waiting time is: %f \n",avg_wt);
    printf("Average turn around time is: %f",avg_tat);
    printf("\n\n \t GANTT CHART \n");
    for(i=0;i<n;i++)
        printf("P%d \t",i);
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d \t",wt[i]);
    }
    rectangle(2,300,270,320);
    getch();
    closegraph();
}
/* OUTPUT
Enter the no. of processes 5
Enter the burst time of processes
12 8 4 6 10
Enter priority of processes
5 1 2 4 3

Process_no.  Burst_Time  Priority  Waiting_Time  Turn_Around_Time
P1 8    1 0 8
P2 4    2 8 12
P4 10    3 12 22
P3 6    4 22 28
P0 12    5 28 40

Average waiting time is 14.00000
Average turn around time is 22.00000

GANTT CHART
 ||P1|| ||P2||  ||P4||   ||P3||   ||P0||
0      8       12 22 28  40*/

No comments:

Post a Comment