|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +// Structure to hold each process's details |
| 5 | +struct Process { |
| 6 | + int id, bt, at, start, comp, remaining_bt; |
| 7 | +}; |
| 8 | + |
| 9 | +// Comparison function for sorting processes by arrival time |
| 10 | +int comp(const void* a, const void* b) { |
| 11 | + struct Process* p1 = (struct Process*)a; |
| 12 | + struct Process* p2 = (struct Process*)b; |
| 13 | + if (p1->at == p2->at) |
| 14 | + return p1->id - p2->id; // If arrival times are the same, sort by ID |
| 15 | + return p1->at - p2->at; // Sort by arrival time otherwise |
| 16 | +} |
| 17 | + |
| 18 | +// Function to calculate the response time of each process |
| 19 | +int* find_response(struct Process* input, int n) { |
| 20 | + int* rt = (int*)malloc(n * sizeof(int)); |
| 21 | + for (int i = 0; i < n; i++) { |
| 22 | + rt[i] = input[i].start - input[i].at; |
| 23 | + } |
| 24 | + return rt; |
| 25 | +} |
| 26 | + |
| 27 | +// Function to calculate the turnaround time of each process |
| 28 | +int* find_tat(struct Process* input, int n) { |
| 29 | + int* tat = (int*)malloc(n * sizeof(int)); |
| 30 | + for (int i = 0; i < n; i++) { |
| 31 | + tat[i] = input[i].comp - input[i].at; |
| 32 | + } |
| 33 | + return tat; |
| 34 | +} |
| 35 | + |
| 36 | +// Function to calculate the waiting time of each process |
| 37 | +int* find_wt(struct Process* input, int* tat, int n) { |
| 38 | + int* wt = (int*)malloc(n * sizeof(int)); |
| 39 | + for (int i = 0; i < n; i++) { |
| 40 | + wt[i] = tat[i] - input[i].bt; |
| 41 | + } |
| 42 | + return wt; |
| 43 | +} |
| 44 | + |
| 45 | +// Function to simulate the SRTF Scheduling Algorithm |
| 46 | +void schedule(struct Process* input, int n) { |
| 47 | + qsort(input, n, sizeof(struct Process), comp); // Sort processes by arrival time |
| 48 | + |
| 49 | + int last_time = 0; // Keeps track of the current time |
| 50 | + int remaining_processes = n; // Number of remaining processes |
| 51 | + int completed = 0; |
| 52 | + |
| 53 | + while (completed < n) { |
| 54 | + int min_bt = __INT_MAX__; |
| 55 | + int idx = -1; |
| 56 | + |
| 57 | + // Find the process with the shortest remaining burst time |
| 58 | + for (int i = 0; i < n; i++) { |
| 59 | + if (input[i].at <= last_time && input[i].remaining_bt > 0 && input[i].remaining_bt < min_bt) { |
| 60 | + min_bt = input[i].remaining_bt; |
| 61 | + idx = i; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (idx != -1) { |
| 66 | + if (input[idx].remaining_bt == input[idx].bt) { |
| 67 | + input[idx].start = last_time; // Set start time for the first execution |
| 68 | + } |
| 69 | + |
| 70 | + // Execute the process for 1 unit of time |
| 71 | + input[idx].remaining_bt--; |
| 72 | + last_time++; |
| 73 | + |
| 74 | + // If the process is completed |
| 75 | + if (input[idx].remaining_bt == 0) { |
| 76 | + input[idx].comp = last_time; // Set completion time |
| 77 | + completed++; |
| 78 | + } |
| 79 | + } else { |
| 80 | + last_time++; // If no process is ready, increment time |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Main program to test the SRTF algorithm |
| 86 | +int main() { |
| 87 | + int n; |
| 88 | + printf("Enter the number of processes: \n"); |
| 89 | + scanf("%d", &n); |
| 90 | + |
| 91 | + struct Process* input = (struct Process*)malloc(n * sizeof(struct Process)); |
| 92 | + for (int i = 0; i < n; i++) { |
| 93 | + printf("Enter the id, burst time and arrival time of process %d:\n", i + 1); |
| 94 | + scanf("%d %d %d", &input[i].id, &input[i].bt, &input[i].at); |
| 95 | + input[i].remaining_bt = input[i].bt; // Initialize remaining burst time |
| 96 | + } |
| 97 | + |
| 98 | + schedule(input, n); |
| 99 | + |
| 100 | + int* response = find_response(input, n); |
| 101 | + int* turntime = find_tat(input, n); |
| 102 | + int* waittime = find_wt(input, turntime, n); |
| 103 | + |
| 104 | + printf("%8s %8s %8s %8s %12s %10s %12s %8s\n", "Process", "Burst", "Arrival", "Start", "Completion", "Response", "Turn Around", "Waiting"); |
| 105 | + for (int i = 0; i < n; i++) { |
| 106 | + printf("%8s%d %8d %8d %8d %12d %10d %12d %8d\n", |
| 107 | + "P", input[i].id, input[i].bt, input[i].at, |
| 108 | + input[i].start, input[i].comp, response[i], |
| 109 | + turntime[i], waittime[i]); |
| 110 | + } |
| 111 | + |
| 112 | + free(input); |
| 113 | + free(response); |
| 114 | + free(turntime); |
| 115 | + free(waittime); |
| 116 | + return 0; |
| 117 | +} |
0 commit comments