Add a new RTOS scheduling implementation with EDF and RMS algorithms. Introduce a task structure and create task threads to simulate task execution. Optimize the Banker's Algorithm code comments for clarity. These changes enhance the understanding and functionality of the scheduling mechanisms in the system.
152 lines
4.1 KiB
C
152 lines
4.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
#include <semaphore.h>
|
|
|
|
// Define task structure
|
|
typedef struct {
|
|
int id;
|
|
int period;
|
|
int deadline;
|
|
int execution_time;
|
|
void (*task_function)(void);
|
|
int remaining_time;
|
|
} task_t;
|
|
|
|
// Define global variables
|
|
task_t tasks[3]; // Example with 3 tasks
|
|
int num_tasks = 3;
|
|
pthread_t task_threads[3];
|
|
sem_t scheduler_sem;
|
|
int current_time = 0;
|
|
|
|
// Task functions (example)
|
|
void task1_function() {
|
|
printf("Task 1 executing at time %d\n", current_time);
|
|
usleep(tasks[0].execution_time * 1000); // Simulate execution time
|
|
}
|
|
|
|
void task2_function() {
|
|
printf("Task 2 executing at time %d\n", current_time);
|
|
usleep(tasks[1].execution_time * 1000); // Simulate execution time
|
|
}
|
|
|
|
void task3_function() {
|
|
printf("Task 3 executing at time %d\n", current_time);
|
|
usleep(tasks[2].execution_time * 1000); // Simulate execution time
|
|
}
|
|
|
|
// EDF Scheduler
|
|
int edf_scheduler() {
|
|
int earliest_deadline = 100000; // Initialize with a large value
|
|
int earliest_task_index = -1;
|
|
|
|
for (int i = 0; i < num_tasks; i++) {
|
|
if (tasks[i].remaining_time > 0 && tasks[i].deadline < earliest_deadline) {
|
|
earliest_deadline = tasks[i].deadline;
|
|
earliest_task_index = i;
|
|
}
|
|
}
|
|
|
|
return earliest_task_index;
|
|
}
|
|
|
|
// Rate Monotonic Scheduler (RMS) - Simplified for Demonstration
|
|
int rms_scheduler() {
|
|
int shortest_period = 100000; // Initialize with a large value
|
|
int shortest_period_task_index = -1;
|
|
|
|
for (int i = 0; i < num_tasks; i++) {
|
|
if (tasks[i].remaining_time > 0 && tasks[i].period < shortest_period) {
|
|
shortest_period = tasks[i].period;
|
|
shortest_period_task_index = i;
|
|
}
|
|
}
|
|
|
|
return shortest_period_task_index;
|
|
}
|
|
|
|
|
|
// Task thread function
|
|
void *task_thread(void *arg) {
|
|
task_t *task = (task_t *)arg;
|
|
|
|
while (1) {
|
|
sem_wait(&scheduler_sem); // Wait for scheduler to release
|
|
|
|
if (task->remaining_time > 0) {
|
|
task->task_function();
|
|
task->remaining_time -= task->execution_time;
|
|
|
|
if (task->remaining_time <= 0) {
|
|
printf("Task %d completed at time %d\n", task->id, current_time);
|
|
}
|
|
}
|
|
|
|
}
|
|
pthread_exit(NULL);
|
|
}
|
|
|
|
int main() {
|
|
// Initialize tasks (EDF Example)
|
|
tasks[0].id = 1;
|
|
tasks[0].period = 50;
|
|
tasks[0].deadline = 50;
|
|
tasks[0].execution_time = 10;
|
|
tasks[0].task_function = task1_function;
|
|
tasks[0].remaining_time = tasks[0].execution_time;
|
|
|
|
tasks[1].id = 2;
|
|
tasks[1].period = 100;
|
|
tasks[1].deadline = 100;
|
|
tasks[1].execution_time = 15;
|
|
tasks[1].task_function = task2_function;
|
|
tasks[1].remaining_time = tasks[1].execution_time;
|
|
|
|
tasks[2].id = 3;
|
|
tasks[2].period = 200;
|
|
tasks[2].deadline = 200;
|
|
tasks[2].execution_time = 20;
|
|
tasks[2].task_function = task3_function;
|
|
tasks[2].remaining_time = tasks[2].execution_time;
|
|
|
|
// Initialize semaphore
|
|
sem_init(&scheduler_sem, 0, 0);
|
|
|
|
// Create task threads
|
|
for (int i = 0; i < num_tasks; i++) {
|
|
pthread_create(&task_threads[i], NULL, task_thread, &tasks[i]);
|
|
}
|
|
|
|
// RTOS Scheduler Loop
|
|
for (current_time = 0; current_time < 500; current_time++) {
|
|
// Choose scheduling algorithm (EDF or RMS)
|
|
int next_task_index = edf_scheduler(); // Use EDF
|
|
//int next_task_index = rms_scheduler(); // Or Use RMS
|
|
|
|
if (next_task_index != -1) {
|
|
sem_post(&scheduler_sem); // Release the semaphore to the selected task
|
|
}
|
|
|
|
usleep(1000); // Simulate 1ms time slice
|
|
|
|
// Update deadlines for EDF scheduler
|
|
for (int i = 0; i < num_tasks; i++) {
|
|
if (current_time % tasks[i].period == 0) {
|
|
tasks[i].deadline = current_time + tasks[i].period;
|
|
tasks[i].remaining_time = tasks[i].execution_time;
|
|
printf("Task %d released at time %d, deadline = %d\n", tasks[i].id, current_time, tasks[i].deadline);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// Clean up
|
|
for (int i = 0; i < num_tasks; i++) {
|
|
pthread_cancel(task_threads[i]);
|
|
}
|
|
sem_destroy(&scheduler_sem);
|
|
|
|
return 0;
|
|
}
|