SRTF Shortest Remaining Time First - CPU Scheduling

 

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

  1. Input n processes with arrival time and burst time.

  2. Create structure array storing each process’s information.

  3. At each time unit:

    • Select process with smallest remaining time that has arrived.

    • Decrement remaining time.

    • If remaining time = 0 → mark completion.

  4. After all processes finish:

    • Compute Turnaround Time

    • Compute Waiting Time

  5. 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) { completed++; p[shortest].ct = time + 1; p[shortest].tat = p[shortest].ct - p[shortest].at; p[shortest].wt = p[shortest].tat - p[shortest].bt; totalWT += p[shortest].wt; } time++; } // Print the result table printf("\nSRTF Scheduling Results:\n"); printf("PID\tAT\tBT\tCT\tTAT\tWT\n"); for (int i = 0; i < n; i++) { printf("P%d\t%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt); } printf("\nAverage Waiting Time = %.2f\n", totalWT / n); return 0; }

5. SAMPLE INPUT

Enter number of processes: 4 Enter Arrival Time of P1: 0 Enter Burst Time of P1: 8 Enter Arrival Time of P2: 1 Enter Burst Time of P2: 4 Enter Arrival Time of P3: 2 Enter Burst Time of P3: 2 Enter Arrival Time of P4: 3 Enter Burst Time of P4: 1

6. SAMPLE OUTPUT

SRTF Scheduling Results: PID AT BT CT TAT WT P1 0 8 15 15 7 P2 1 4 7 6 2 P3 2 2 4 2 0 P4 3 1 5 2 1 Average Waiting Time = 2.50

7. RESULT

The SRTF (Shortest Remaining Time First) scheduling algorithm was implemented using structures.
The program successfully computed:

  • Completion Time

  • Turnaround Time

  • Waiting Time

  • Average Waiting Time

for all processes based on preemptive shortest remaining time scheduling.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command