Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.2 kB
2
Indexable
Never
#include<stdio.h>
#include<stdlib.h>

struct Process
{
    int pid, arr, burst, turn, wait;
    int remain, starts, flag,exit;
};

void TimeSlice(struct Process p[], int n, int quant)
{
    int i;
    int current = 0;
    int complete = 0;

    while (complete < n)
    {
        for (i = 0; i < n; i++)
        {
            if (p[i].remain <= 0 || p[i].arr > current)
                continue;

            if (p[i].remain > quant)
            {
                current += quant;
                p[i].flag+=1;
                p[i].remain -= quant;
            }

            else
            {
                p[i].starts = current;
                current += p[i].remain;
                p[i].exit = current;
                p[i].remain = 0;
                p[i].turn = current - p[i].arr;
                p[i].wait = p[i].turn - p[i].burst;
                complete++;
            }
        }
    }
}

void sorting(struct Process p[], int n)
{
    struct Process temp;
    int i, j;

    for (i = 0; i < n - 1; i++)
    {
        for (j = 0; j < n - i - 1; j++)
        {
            if (p[j].arr > p[j + 1].arr)
            {
                temp = p[j];
                p[j] = p[j + 1];
                p[j + 1] = temp;
            }
        }
    }
}

int main()
{
    int n, i, quant;
    struct Process p[10];
    float avgwait = 0, avgturn = 0;

    printf("Enter the number of process: ");
    scanf("%d", &n);
    printf("Enter the process id, arrival time, burst time:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d%d%d", &p[i].pid, &p[i].arr, &p[i].burst);
        p[i].remain = p[i].burst;
        p[i].flag =0;
        p[i].starts =0;
    }
    printf("Enter the Quantum size: ");
    scanf("%d", &quant);

    sorting(p, n);

    TimeSlice(p, n, quant);

    printf("ProcessId ArrivalTime BurstTime     WaitTime   TurnaroundTime  ExitTime\n");
    for (i = 0; i < n; i++)
    {
        printf("p[%d]\t\t%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arr, p[i].burst, p[i].wait, p[i].turn,p[i].exit);
    }

    for (i = 0; i < n; i++)
    {
        avgwait += p[i].wait;
        avgturn += p[i].turn;
    }

    printf("Average WaitingTime = %.2f\nAverage TurnaroundTime = %.2f\n", avgwait / n, avgturn / n);

    return 0;
}
Leave a Comment