Developing a Task Scheduling System in C Language
Introduction
In software development, a task scheduling system is a very important component. It is responsible for managing and executing multiple tasks, ensuring that they run efficiently and in an orderly manner. This article will guide you through implementing a simple task scheduling system using the C language. We will create a scheduler based on the Round Robin algorithm.
Objectives
The main objectives of this project include:
- Creating a basic task structure.
- Implementing functions to add, delete, and run tasks.
- Using a circular queue to manage the order of tasks.
Environment Setup
To compile and run our code, you need to install a C language compilation environment, such as GCC or Clang. Additionally, it is recommended to use any text editor, such as Visual Studio Code or Notepad++, for writing the code.
Code Implementation
Next, we will build our scheduling system step by step.
1. Define Data Structures
First, define a <span>Task</span>
structure to store information about each task, including ID, name, and execution time parameters. Also, define a node type <span>Node</span>
to organize them in a queue format.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 10 // Maximum number of tasks supported
#define TIME_SLICE 2 // Time slice allocated for each task (in seconds)
typedef struct {
int id; // Task ID
char name[20]; // Task name
int remaining_time; // Remaining execution time
} Task;
// Queue node structure
typedef struct Node {
Task task;
struct Node* next;
} Node;
Node* front = NULL; // Front pointer of the queue
Node* rear = NULL; // Rear pointer of the queue
2. Add and Delete Operations
Next, we define functions to add new processes to the queue and to delete specific processes by ID.
// Add new job to the end of the queue
void enqueue(Task task) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->task = task;
new_node->next = NULL;
if (rear == NULL) { // If the queue is empty, update front and rear pointers
front = rear = new_node;
return;
}
rear->next = new_node;
rear = new_node;
}
// Delete job from the queue by ID
void dequeue(int id) {
Node *temp = front, *prev_temp = NULL;
while (temp != NULL && temp->task.id != id) {
prev_temp = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Job not found\n");
return;
}
if (prev_temp == NULL) {
front = temp->next;
} else {
prev_temp->next = temp->next;
}
free(temp);
}
3. Execute Job Function
Now, implement a method to run all jobs sequentially within the specified time and update their status. When a job’s remaining time is insufficient, remove that job and provide feedback.
void run_tasks() {
while (front != NULL) { // Loop while there are jobs to process
Node *current_task = front;
int execution_time = current_task->task.remaining_time > TIME_SLICE ?
TIME_SLICE : current_task->task.remaining_time;
printf("Running %s (ID: %d), execution time: %d seconds\n",
current_task->task.name, current_task->task.id, execution_time);
current_task->task.remaining_time -= execution_time;
if (current_task->task.remaining_time <= 0) {
dequeue(current_task->task.id);
printf("%s has completed!\n", current_task->task.name);
} else {
enqueue(current_task->task);
dequeue(current_task->task.id);
}
}
}
4. Example Main Function
Finally, create some example applications in the main function for users to test the program’s functionality, including adding, deleting, and running simulated process operations.
int main() {
Task t1 = {1, "Task_One", 5};
Task t2 = {2, "Task_Two", 8};
Task t3 = {3, "Task_Three", 6};
enqueue(t1);
enqueue(t2);
enqueue(t3);
run_tasks();
return 0;
}
Conclusion
This article demonstrates how to build a basic process scheduling system in C language through a simple example. This fully functional tool not only helps in understanding basic concepts but also lays the foundation for more complex scheduling algorithms. After grasping these core concepts, I hope everyone can further expand this program, for example, by introducing priorities or different strategies to improve efficiency and responsiveness!
I hope this article is helpful to you, and let’s explore more topics related to computer science together!