Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
2.7 kB
1
Indexable
Never
#include <stdio.h>

struct process 
{
     int pid, arrival, burst, wait, turn;
};

void sort(struct process *p, int n) 
{
    for (int i = 0; i < n - 1; i++) 
	{
        int min = i;
        for (int j = i + 1; j < n; j++) 
            {
            if (p[min].arrival > p[j].arrival) 
            {
                min = j;
            }
            }
        if (i != min) 
        {
            struct process temp = p[i];
            p[i] = p[min];
            p[min] = temp;
        }
        }
}

void fcfs(struct process *p, int n) 
    {
    p[0].wait = 0;
    p[0].turn = p[0].burst;
    int sum = p[0].arrival;
    for (int i = 1; i < n; i++) 
        {
        sum += p[i - 1].burst;
        p[i].wait = sum - p[i].arrival;
        if (p[i].wait < 0) 
        {
            p[i].wait = 0;
        }
        p[i].turn = p[i].wait + p[i].burst;
        }
    }

void gantt_chart(struct process *p, int n) 
    {
    printf("\n\n");
    for (int i = 0; i < p[n - 1].turn; i++) {
        printf(" ");
    }
    printf("GANTT Chart\n\n");
    int i, j;
    for (i = 0; i < n; i++) {
        for (j = 0; j < p[i].burst; j++) {
            printf("--");
            printf(" ");
        }
    }

    printf("\n|");
    for (i = 0; i < n; i++) {
        for (j = 0; j < p[i].burst - 1; j++) {
            printf(" ");
        }
        printf("|");
    }
    printf("\n%d", p[0].arrival);
    for (i = 0; i < n; i++) {
        printf(" ");
        if (p[i].turn + p[i].arrival > 9) {
            printf("\b");
        }
        printf("%d", p[i].turn + p[i].arrival);
    }
    printf("\n");
   } 

void average(struct process *p, int n) {
    float sumw = 0, sumt = 0;
    for (int i = 0; i < n; i++) {
        sumw += p[i].wait;
        sumt += p[i].turn;
    }
    printf("\n\nAverage Waiting Time: %0.3f", sumw / n);
    printf("\nAverage Turnaround Time: %0.3f\n", sumt / n);
}

int main() {
    int n;
    printf("Enter the number of processes:");
    scanf("%d", &n);
    struct process p[10];
    printf("Enter the arrival times of processes:");
    for (int i = 0; i < n; i++) {
        scanf("%d", &p[i].arrival);
    }
    printf("Enter the burst times of processes:");
    for (int i = 0; i < n; i++) {
        scanf("%d", &p[i].burst);
        p[i].pid = i + 1;
    }
    sort(p, n);
    fcfs(p, n);
    printf("\n-----------------------------------------------------\n");
    printf("Processes Arrival time Burst time Waiting time TurnAround Time\n");
    for (int i = 0; i < n; i++) {
        printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival, p[i].burst, p[i].wait, p[i].turn);
    }
    printf("-----------------------------------------------------");
    gantt_chart(p, n);
    average(p, n);
    return 0;
}

Leave a Comment