Task Scheduling Algorithms in C: Round Robin and Priority Scheduling

In operating systems, task scheduling is a crucial concept. It determines how multiple processes or threads share CPU resources. In this article, we will introduce two common task scheduling algorithms: Round Robin Scheduling and Priority Scheduling. We will demonstrate the implementation of these two algorithms through C language code examples.

1. Round Robin Scheduling

1.1 Overview

Round Robin scheduling is a simple and widely used time-sharing algorithm. In this method, each process is assigned a fixed-length time slice, and when the time slice expires, the process is suspended and placed at the end of the queue, then the CPU switches to the next process. This approach ensures that each process fairly receives CPU time.

1.2 Implementation

Below is an example code implementing Round Robin scheduling in C:

#include <stdio.h>
#define MAX_PROCESSES 10

typedef struct {    int id;    int burst_time;} Process;

void round_robin(Process processes[], int n, int time_quantum) {    int remaining_time[MAX_PROCESSES];
    for (int i = 0; i < n; i++) {        remaining_time[i] = processes[i].burst_time;    }
    int time = 0;    while (1) {        int done = 1; // Used to check if all processes are completed        for (int i = 0; i < n; i++) {            if (remaining_time[i] > 0) { // If the process has remaining execution time                done = 0; // At least one is not completed                if (remaining_time[i] > time_quantum) {                    time += time_quantum;                    remaining_time[i] -= time_quantum;                } else {                     // Complete the process                    time += remaining_time[i];                    printf("Process %d completed at %d\n", processes[i].id, time);                    remaining_time[i] = 0;                }            }        }        if (done == 1) break; // All processes are completed    }}

int main() {    Process processes[] = {{1, 10}, {2, 5}, {3, 8}};    int n = sizeof(processes) / sizeof(processes[0]);
    printf("Round Robin Scheduling:\n");    round_robin(processes, n, 3); // Time slice is 3
    return 0;}

Example Explanation

  • We defined a <span>Process</span> data structure to represent each process, including its ID and required execution time.
  • <span>round_robin</span> function takes an array of processes, the number of processes, and the time slice size as parameters.
  • In the loop, we check if each process has remaining execution time and update its status accordingly.
  • When all processes are completed, the program ends.

2. Priority Scheduling

2.1 Overview

Priority scheduling is a method of task management based on priority. Each process has an associated priority, and the system always selects the process with the highest priority (usually a lower number indicates a higher priority) for processing. If two or more processes have the same priority, other strategies, such as FCFS (First-Come, First-Served), can be used.

2.2 Implementation

Below is an example code implementing priority scheduling in C:

#include <stdio.h>
#include <stdlib.h>

typedef struct {    int id;    int burst_time;    int priority;} Process;

int compare(const void *a, const void *b) {   return ((Process *)a)->priority - ((Process *)b)->priority; }

void priority_scheduling(Process processes[], int n) {   qsort(processes, n, sizeof(Process), compare); // Sort by priority
   printf("Priority Scheduling:\n");   for(int i=0; i<n; ++i){       printf("Process %d with Burst Time %d and Priority %d is executed.\n",               processes[i].id,              processes[i].burst_time,              processes[i].priority);   }}

int main() {   Process processes[] = {{1,10,2}, {2,5,3}, {3,8,1}};   int n = sizeof(processes)/sizeof(processes[0]);
   priority_scheduling(processes,n);
   return EXIT_SUCCESS;}

Example Explanation

  • We defined a <span>Process</span> data structure that includes ID, required execution time, and priority.
  • Using the <span>qsort</span> function, we sort the array by priority to ensure that high-priority processes are handled first.
  • In the main function, we create a set of test data and call the <span>priority_scheduling</span> function to display the results.

Conclusion

This article introduced two fundamental yet important task scheduling algorithms: Round Robin and Priority Scheduling. Through simple and understandable C language code examples, we demonstrated how to implement these algorithms and their working principles. This knowledge is essential for understanding multitasking in operating systems, and we hope it is helpful to you!

Leave a Comment