OS Lab 09

Pasted Dec 1, 2026, 3:44 AM2 files134 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/os-lab-09
inlab.c
#include <stdio.h>

#define N 4

typedef struct {
    char id[3];
    int arrival;
    int burst;
    int completion;
    int turnaround;
    int waiting;
} Process;

int main(void) {
    Process p[N] = {
        {"P1", 0, 5, 0, 0, 0},
        {"P2", 1, 3, 0, 0, 0},
        {"P3", 2, 4, 0, 0, 0},
        {"P4", 4, 2, 0, 0, 0}
    };

    int currentTime = 0;
    float totalTurnaround = 0;
    float totalWaiting = 0;

    /* Sort by arrival time; retain input order if arrival times tie. */
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            if (p[j].arrival < p[i].arrival) {
                Process temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }

    for (int i = 0; i < N; i++) {
        if (currentTime < p[i].arrival) {
            currentTime = p[i].arrival;  // CPU is idle until this process arrives
        }

        currentTime += p[i].burst;
        p[i].completion = currentTime;
        p[i].turnaround = p[i].completion - p[i].arrival;
        p[i].waiting = p[i].turnaround - p[i].burst;

        totalTurnaround += p[i].turnaround;
        totalWaiting += p[i].waiting;
    }

    printf("Process\tArrival\tBurst\tCompletion\tTurnaround\tWaiting\n");
    for (int i = 0; i < N; i++) {
        printf("%s\t%d\t%d\t%d\t\t%d\t\t%d\n",
               p[i].id, p[i].arrival, p[i].burst,
               p[i].completion, p[i].turnaround, p[i].waiting);
    }

    printf("\nAverage turnaround time: %.2f\n", totalTurnaround / N);
    printf("Average waiting time: %.2f\n", totalWaiting / N);

    return 0;
}

3,493 chars · codepastes.com