Skip to content Skip to sidebar Skip to footer

Srtf Scheduling Program In C With Gantt Chart

Srtf Scheduling Program In C

Introduction

Computer operating systems utilize different scheduling algorithms to manage processes efficiently. The Shortest Remaining Time First (SRTF) scheduling algorithm is a pre-emptive scheduling algorithm that schedules processes based on their remaining execution time. In this article, we will develop an SRTF scheduling program in C that uses a Gantt chart to visualize the scheduling process.

SRTF Scheduling Algorithm

The SRTF scheduling algorithm is similar to the Shortest Job First (SJF) scheduling algorithm, but it is pre-emptive. When a new process arrives, the scheduler checks its execution time, and if it is shorter than the remaining time of the current process, it pre-empts the current process and schedules the new process. The SRTF algorithm ensures that the process with the shortest remaining execution time always runs first.

Srtf Algorithm

Gantt Chart

A Gantt chart is a graphical representation of a schedule that shows the beginning and end of each task, as well as their duration. In the context of operating systems, a Gantt chart shows the execution of processes over time.

Gantt Chart

SRTF Scheduling Program in C

Let's develop an SRTF scheduling program in C that uses a Gantt chart to visualize the scheduling process. We will assume that the processes arrive in a random order and have a random execution time. We will also assume that there is only one processor available.

First, we will define a Process structure that contains the process ID, arrival time, and execution time.

struct Process {int pid;int arrival_time;int execution_time;};

Next, we will define a function that generates a random process with a random arrival time and execution time.

void generate_process(struct Process* p) {static int pid = 0;p->pid = pid++;p->arrival_time = rand() % 10;p->execution_time = rand() % 10 + 1;}

We will use the rand() function to generate random numbers. The arrival time and execution time are limited to a maximum of 10 to keep the program simple.

Next, we will define a function that compares two processes based on their remaining execution time.

int compare_processes(const void* a, const void* b) {struct Process* p1 = (struct Process*)a;struct Process* p2 = (struct Process*)b;return p1->execution_time - p2->execution_time;}

We will use this function to sort the processes based on their remaining execution time.

Next, we will define the main function that simulates the scheduling process. We will use an array of processes and sort them based on their arrival time. We will use a queue to store the processes that have arrived but have not yet started execution. We will use another queue to store the processes that are currently executing. We will use a Gantt chart to visualize the scheduling process.

int main() {srand(time(NULL));const int n = 5;struct Process processes[n];for (int i = 0; i < n; i++) {generate_process(&processes[i]);}qsort(processes, n, sizeof(struct Process), compare_processes);int current_time = 0;int remaining_time = 0;int i = 0;queue arrival_queue;queue execution_queue;while (!execution_queue.empty() || i < n) {while (i < n && processes[i].arrival_time <= current_time) {arrival_queue.push(processes[i]);i++;}if (remaining_time == 0 && !arrival_queue.empty()) {struct Process p = arrival_queue.top();arrival_queue.pop();execution_queue.push(p);remaining_time = p.execution_time;}if (remaining_time > 0) {remaining_time--;printf("%d ", execution_queue.top().pid);} else {printf("IDLE ");}current_time++;}printf("\n");return 0;}

The program generates five random processes and sorts them based on their remaining execution time. It then simulates the scheduling process by iterating over time intervals. It checks if any new processes have arrived and pushes them onto the arrival queue. It then checks if the current process has finished executing and removes it from the execution queue. It then checks if there are any processes waiting in the arrival queue and starts executing the one with the shortest remaining execution time. It uses the printf() function to print the process ID or "IDLE" if no process is executing.

Gantt Chart Visualization

To visualize the scheduling process, we will use a Gantt chart. We will modify the program to print the Gantt chart as follows:

int main() {// ...printf(" ");for (int i = 0; i < current_time; i++) {printf("-");}printf("\n");int start_time = 0;while (!execution_queue.empty()) {struct Process p = execution_queue.top();execution_queue.pop();for (int i = 0; i < p.execution_time; i++) {printf("|%d", p.pid);}start_time += p.execution_time;printf("|");}printf("\n ");for (int i = 0; i < start_time; i++) {printf("-");}printf("\n");// ...}

This code prints the Gantt chart by iterating over the execution queue and printing a vertical bar for each time interval that a process is executing. It uses the printf() function to print the process ID inside each vertical bar. It also prints a horizontal bar for each time interval that no process is executing.

Conclusion

In this article, we developed an SRTF scheduling program in C that uses a Gantt chart to visualize the scheduling process. We used a queue to store the processes that have arrived but have not yet started execution. We used another queue to store the processes that are currently executing. We used a Gantt chart to visualize the scheduling process. The program can be modified to include more advanced features such as priority scheduling and multiple processors.

Related video of SRTF Scheduling Program in C with Gantt Chart