Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.2 kB
1
Indexable
Never
#include <stdio.h>
struct process{
    int pid; 
    int btime; 
    int pri; 
    int wtime; 
    int ttime;
}

p[10], temp;

int main(){
    int k,n,ttur,twat;
    float awat,atur;
    printf("How many processes? ");
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        printf("Burst time for process P%d is ", (i+1));
        scanf("%d", &p[i].btime);
        printf("Priority for process P%d is ", (i+1));
        scanf("%d", &p[i].pri);
        p[i].pid = i+1;
    }
    for(int i=0; i<n-1; i++){
      for(int j=i+1; j<n; j++){
         if((p[i].pri > p[j].pri) || (p[i].pri == p[j].pri && p[i].pid > p[j].pid))
               temp = p[i]; p[i] = p[j]; p[j] = temp;
      }
    }
    p[0].wtime = 0;
    for(int 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 = 0;
    twat = 0;
    for(int i=0; i<n; i++){
       ttur += p[i].ttime;
       twat += p[i].wtime;
    }
    awat = (float)twat/n;
    atur = (float)ttur/n;
    printf("\n\t Priority Scheduling\n\n");
    for(int i=0; i<38; i++)
        printf("-");
    printf("\nProcess B-Time Priority T-Time  W-Time\n");
    for(int i=0; i<38; i++)
        printf("-");
    for(int i=0; i<n; i++)
        printf("\n  P%-4d\t%4d\t%3d\t%4d\t%4d", p[i].pid,p[i].btime,p[i].pri,p[i].ttime,p[i].wtime);
    printf("\n");
    for(int i=0; i<38; i++)
        printf("-");
    printf("\n\nGANTT Chart\n");
    printf("-");
    for(int i=0; i<(p[n-1].ttime + 2*n); i++)
        printf("-"); 
    printf("\n|"); 
    for(int i=0; i<n; i++){
        k = p[i].btime/2;
        for(int j=0; j<k; j++)
            printf(" "); 
        printf("P%d",p[i].pid); 
        for(int j=k+1; j<p[i].btime; j++)
            printf(" ");
        printf("|");
    }
    printf("\n-");
    for(int i=0; i<(p[n-1].ttime + 2*n); i++)
        printf("-"); printf("\n0"); 
    for(int i=0; i<n; i++){
        for(int 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);
}