SRTF Shortest Remaining Time First 
CPU  Scheduling 
1. AIM
To simulate SRTF CPU Scheduling using structures and compute:

Completion Time

Turnaround Time

Waiting Time

Average Waiting Time

for a set of processes with given arrival and burst times.

2. THEORY
Shortest Remaining Time First (SRTF)
It is the preemptive version of SJF.

At every time unit, the scheduler picks the process with the shortest remaining time.

If a new process arrives with shorter remaining burst time → preemption happens.

Produces optimal waiting time but is difficult to implement.

3. ALGORITHM
Input n processes with arrival time and burst time.

Create structure array storing each process’s information.

At each time unit:

Select process with smallest remaining time that has arrived.

Decrement remaining time.

If remaining time = 0 → mark completion.

After all processes finish:

Compute Turnaround Time

Compute Waiting Time

Compute Average Waiting Time.

4. C PROGRAM – SRTF Using Structures
#include <stdio.h>
#include <limits.h>

struct Process {
    int pid;
    int at;      // Arrival Time
    int bt;      // Burst Time
    int rt;      // Remaining Time
    int ct;      // Completion Time
    int tat;     // Turnaround Time
    int wt;      // Waiting Time
};

int main() {
    int n, completed = 0, time = 0;
    float totalWT = 0;

    printf("Enter number of processes: ");
    scanf("%d", &n);

    struct Process p[n];

    // Input process details
    for (int i = 0; i < n; i++) {
        p[i].pid = i + 1;
        printf("Enter Arrival Time of P%d: ", i + 1);
        scanf("%d", &p[i].at);
        printf("Enter Burst Time of P%d: ", i + 1);
        scanf("%d", &p[i].bt);
        p[i].rt = p[i].bt;   // Initialize remaining time
    }

    // SRTF Simulation
    while (completed != n) {
        int shortest = -1;
        int minRT = INT_MAX;

        // Find process with minimum remaining time
        for (int i = 0; i < n; i++) {
            if (p[i].at <= time && p[i].rt > 0 && p[i].rt < minRT) {
                minRT = p[i].rt;
                shortest = i;
            }
        }

        // If no process available
        if (shortest == -1) {
            time++;
            continue;
        }

        // Execute for 1 time unit
        p[shortest].rt--;

        // If process finishes
        if (p[shortest].rt == 0) {
