C Program for Priority CPU Scheduling Algorithm

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<conio.h>
void main()
{
  char p[10][5],temp[5];
  int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
  float avgwt;
  clrscr();
  printf("enter no of processes:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
  printf("enter process%d name:",i+1);
  scanf("%s",&p[i]);
  printf("enter process time:");
  scanf("%d",&pt[i]);
  printf("enter priority:");
  scanf("%d",&pr[i]);
  }
  for(i=0;i<n-1;i++)
  {
 for(j=i+1;j<n;j++)
  {
   if(pr[i]>pr[j])
   {
   temp1=pr[i];
   pr[i]=pr[j];
   pr[j]=temp1;
   temp1=pt[i];
   pt[i]=pt[j];
   pt[j]=temp1;
   strcpy(temp,p[i]);
   strcpy(p[i],p[j]);
   strcpy(p[j],temp);
   }
  }
  }
  wt[0]=0;
  for(i=1;i<n;i++)
  {
   wt[i]=wt[i-1]+et[i-1];
   totwt=totwt+wt[i];
   }
   avgwt=(float)totwt/n;
   printf("p_name\t p_time\t priority\t w_time\n");
    for(i=0;i<n;i++)
    {
   printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
   }
   printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
   getch();
   }

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

44 Responses to “C Program for Priority CPU Scheduling Algorithm”

  1. Pawan Satpute

    #include
    struct process
    {
    int pid;
    int btime;
    int wtime;
    int ttime;
    int prtime;
    } p[10];
    main()
    {
    int i,j,k,n,ttur,twat,temppro,tempburst,temppir;
    float awat,atur;
    printf(“Enter no. of process :”);
    scanf(“%d”, &n);
    for(i=0; i<n; i++)
    {
    printf("\nBurst time for process P%d (in ms) :",(i+1));
    scanf("\t%d", &p[i].btime);
    printf("\n Enter priority\t");
    scanf("%d",&p[i].prtime);
    p[i].pid = i+1;
    }
    for(i=0;i<n;i++)
    {
    for(j=i+1;jp[j].prtime)
    {
    tempburst=p[i].btime;
    p[i].btime=p[j].btime;
    p[j].btime=tempburst;
    temppro=p[i].pid;
    p[i].pid=p[j].pid;
    p[j].pid=temppro;
    temppir=p[i].prtime;
    p[i].prtime=p[j].prtime;
    p[j].prtime=temppir;

    }

    }

    }

    p[0].wtime = 0;
    for(i=0; i<n; i++)
    {
    p[i+1].wtime = p[i].wtime + p[i].btime;
    p[i].ttime = p[i].wtime + p[i].btime;
    }
    ttur = twat = 0;
    for(i=0; i<n; i++)
    {
    ttur+=p[i].ttime;
    twat+=p[i].wtime;
    }
    awat = (float)twat/n;
    atur = (float)ttur/n;

    printf("\n priority Scheduling ALGORITHM \n\n");
    for(i=0; i<38; i++)
    printf("-");
    printf("\n Process B-Time T-Time W-Time Priority\n");
    for(i=0; i<38; i++)
    printf("-");
    for(i=0; i<n; i++)
    printf("\n P%d\t%4d\t%3d\t%2d\t%d",p[i].pid,p[i].btime,p[i].ttime,p[i].wtime,p[i].prtime);
    printf("\n");
    for(i=0;i<38;i++)
    printf("-");
    printf("\n\nGANTT Chart\n");
    printf("-");
    for(i=0; i<(p[n-1].ttime + 2*n);i++)
    printf("-");
    printf("\n");
    printf("|");
    for(i=0;i<n;i++)
    {
    k = p[i].btime/2;
    for(j=0; j<k;j++)
    printf(" ");
    printf("P%d",p[i].pid);
    for(j=k+1;j<p[i].btime;j++)
    printf(" ");
    printf("|");
    }
    printf("\n");
    printf("-");
    for(i=0;i<(p[n-1].ttime + 2*n);i++)
    printf("-");
    printf("\n");
    printf("0");
    for(i=0;i<n;i++)
    {
    for(j=0;j<p[i].btime;j++)
    printf(" ");
    printf("%2d",p[i].ttime);
    }
    printf("\n\nAverage waiting time: %5.2fms", awat);
    printf("\nAverage turn around time : %5.2fms\n", atur);
    }

    OUTPUT:–
    Enter no. of process :5

    Burst time for process P1 (in ms) :10

    Enter priority 4

    Burst time for process P2 (in ms) :12

    Enter priority 8

    Burst time for process P3 (in ms) :5

    Enter priority 8

    Burst time for process P4 (in ms) :14

    Enter priority 6

    Burst time for process P5 (in ms) :17

    Enter priority 9

    priority Scheduling ALGORITHM

    ————————————–
    Process B-Time T-Time W-Time Priority
    ————————————–
    P1 10 10 0 4
    P4 14 24 10 6
    P3 5 29 24 8
    P2 12 41 29 8
    P5 17 58 41 9
    ————————————–

    GANTT Chart
    ———————————————————————
    | P1 | P4 | P3 | P2 | P5 |

    ———————————————————————
    0 10 24 29 41 58

    Average waiting time: 20.80ms
    Average turn around time : 32.40ms

    Reply

Leave a Reply