Certainly! Below is a detailed C program that implements a dynamic load balancer on multi-core systems using priority round-robin scheduling. This program simulates a multi-core CPU where tasks with different priorities are scheduled and balanced dynamically across cores.
Program Overview
Task Structure : Represents a task with an ID, priority, burst time, and remaining time.
Core Structure : Represents a CPU core with its own task queue.
Scheduler : Implements priority round-robin scheduling.
Load Balancer : Dynamically redistributes tasks among cores to balance the load.
Code
c Copy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 50
#define TIME_QUANTUM 2
#define MAX_PRIORITY 5
#define NUM_CORES 4
// Task structure
typedef struct {
int id;
int priority;
int burst_time;
int remaining_time;
} Task;
// Node for task queue
typedef struct Node {
Task task;
struct Node* next;
} Node;
// Core structure with its own task queue
typedef struct {
int id;
Node* task_queue;
} Core;
// Function prototypes
void enqueue_task(Node** head, Task task);
Task dequeue_task(Node** head);
int is_empty(Node* head);
void print_cores(Core cores[]);
void dynamic_load_balancer(Core cores[]);
void scheduler(Core cores[]);
void add_task(Core cores[], Task task);
// Main function
int main() {
// Initialize cores
Core cores[NUM_CORES];
for (int i = 0; i < NUM_CORES; i++) {
cores[i].id = i;
cores[i].task_queue = NULL;
}
// Sample tasks
Task tasks[MAX_TASKS];
int num_tasks = 10; // Number of tasks
for (int i = 0; i < num_tasks; i++) {
tasks[i].id = i;
tasks[i].priority = rand() % MAX_PRIORITY + 1;
tasks[i].burst_time = rand() % 10 + 1;
tasks[i].remaining_time = tasks[i].burst_time;
add_task(cores, tasks[i]);
}
printf("Initial task distribution:\n");
print_cores(cores);
// Run the scheduler
scheduler(cores);
return 0;
}
// Function to add a task to the least loaded core
void add_task(Core cores[], Task task) {
int min_load = INT_MAX;
int target_core = 0;
// Find the core with the least number of tasks
for (int i = 0; i < NUM_CORES; i++) {
int load = 0;
Node* temp = cores[i].task_queue;
while (temp) {
load++;
temp = temp->next;
}
if (load < min_load) {
min_load = load;
target_core = i;
}
}
enqueue_task(&cores[target_core].task_queue, task);
}
// Enqueue task to the tail of the queue
void enqueue_task(Node** head, Task task) {
Node* new_node = (Node*)malloc(sizeof(Node));
if (!new_node) {
printf("Memory allocation error!\n");
exit(1);
}
new_node->task = task;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
} else {
Node* temp = *head;
while (temp->next)
temp = temp->next;
temp->next = new_node;
}
}
// Dequeue task from the head of the queue
Task dequeue_task(Node** head) {
if (*head == NULL) {
printf("Queue is empty!\n");
exit(1);
}
Node* temp = *head;
Task task = temp->task;
*head = (*head)->next;
free(temp);
return task;
}
// Check if the queue is empty
int is_empty(Node* head) {
return head == NULL;
}
// Print task distribution among cores
void print_cores(Core cores[]) {
for (int i = 0; i < NUM_CORES; i++) {
printf("Core %d: ", cores[i].id);
Node* temp = cores[i].task_queue;
while (temp) {
printf("[T%d P%d RT%d] -> ", temp->task.id, temp->task.priority, temp->task.remaining_time);
temp = temp->next;
}
printf("NULL\n");
}
printf("\n");
}
// Dynamic load balancer to redistribute tasks
void dynamic_load_balancer(Core cores[]) {
// Calculate the average load
int total_load = 0;
int core_loads[NUM_CORES];
for (int i = 0; i < NUM_CORES; i++) {
int load = 0;
Node* temp = cores[i].task_queue;
while (temp) {
load += temp->task.remaining_time;
temp = temp->next;
}
core_loads[i] = load;
total_load += load;
}
int average_load = total_load / NUM_CORES;
// Redistribute tasks from overloaded cores to underloaded cores
for (int i = 0; i < NUM_CORES; i++) {
while (core_loads[i] > average_load + TIME_QUANTUM) {
// Move a task to the least loaded core
Task task = dequeue_task(&cores[i].task_queue);
core_loads[i] -= task.remaining_time;
// Find the least loaded core
int min_load = INT_MAX;
int target_core = i;
for (int j = 0; j < NUM_CORES; j++) {
if (core_loads[j] < min_load && j != i) {
min_load = core_loads[j];
target_core = j;
}
}
enqueue_task(&cores[target_core].task_queue, task);
core_loads[target_core] += task.remaining_time;
}
}
}
// Scheduler implementing priority round-robin scheduling
void scheduler(Core cores[]) {
int all_done;
int time = 0;
do {
all_done = 1;
// Dynamic load balancing
dynamic_load_balancer(cores);
for (int i = 0; i < NUM_CORES; i++) {
if (!is_empty(cores[i].task_queue)) {
all_done = 0;
// Find the highest priority task
Node* temp = cores[i].task_queue;
Node* highest_priority_node = temp;
while (temp) {
if (temp->task.priority < highest_priority_node->task.priority) {
highest_priority_node = temp;
}
temp = temp->next;
}
// Execute task for a time quantum
int exec_time = (highest_priority_node->task.remaining_time < TIME_QUANTUM) ? highest_priority_node->task.remaining_time : TIME_QUANTUM;
printf("Time %d: Core %d executing Task %d (Priority %d) for %d units\n", time, cores[i].id, highest_priority_node->task.id, highest_priority_node->task.priority, exec_time);
time += exec_time;
highest_priority_node->task.remaining_time -= exec_time;
// If task is completed, remove it from the queue
if (highest_priority_node->task.remaining_time <= 0) {
// Remove the task from the queue
Node** indirect = &cores[i].task_queue;
while (*indirect != highest_priority_node) {
indirect = &(*indirect)->next;
}
*indirect = highest_priority_node->next;
free(highest_priority_node);
}
}
}
} while (!all_done);
printf("\nAll tasks have been executed.\n");
}
Explanation
Structures Used
Task : Represents a task with:
id: Unique identifier for the task.
priority: Priority level (lower value means higher priority).
burst_time: Total time required by the task.
remaining_time: Time left for the task to complete.
Node : Represents a node in the task queue linked list.
task: The task data.
next: Pointer to the next node.
Core : Represents a CPU core.
id: Unique identifier for the core.
task_queue: Pointer to the head of the core's task queue.
Functions
enqueue_task: Adds a task to the end of a core's task queue.
dequeue_task: Removes and returns the task at the front of a core's task queue.
is_empty: Checks if a core's task queue is empty.
print_cores: Prints the current task distribution among cores.
dynamic_load_balancer: Balances the load among cores by redistributing tasks based on their remaining execution times.
scheduler: Simulates the execution of tasks using priority round-robin scheduling.
Scheduling Algorithm
The scheduler runs in a loop until all tasks are completed.
At each iteration:
Calls the dynamic_load_balancer to redistribute tasks if necessary.
For each core:
Checks if the task queue is not empty.
Selects the highest priority task in the queue.
Executes the task for a fixed TIME_QUANTUM or until completion, whichever is shorter.
Updates the remaining_time of the task.
If the task is completed (remaining_time <= 0), it is removed from the queue.
Load Balancing Algorithm
Calculates the average load across all cores.
For cores with load greater than the average load plus a threshold (TIME_QUANTUM), tasks are moved to less loaded cores.
Tasks are moved one at a time from overloaded cores to the least loaded cores to balance the load.
Sample Output
When you run the program, you will see output similar to:
code Copy code
Initial task distribution:
Core 0: [T0 P3 RT5] -> [T4 P2 RT6] -> NULL
Core 1: [T1 P1 RT8] -> [T5 P4 RT2] -> NULL
Core 2: [T2 P5 RT3] -> [T6 P2 RT4] -> NULL
Core 3: [T3 P2 RT7] -> [T7 P3 RT5] -> NULL
Time 0: Core 0 executing Task 4 (Priority 2) for 2 units
Time 2: Core 1 executing Task 1 (Priority 1) for 2 units
Time 4: Core 2 executing Task 6 (Priority 2) for 2 units
Time 6: Core 3 executing Task 3 (Priority 2) for 2 units
...
All tasks have been executed.
How to Compile and Run
Save the code in a file, e.g., dynamic_load_balancer.c.
Open a terminal and navigate to the directory containing the file.
Compile the program using a C compiler, e.g.:
bash Copy code
gcc dynamic_load_balancer.c -o dynamic_load_balancer
Run the executable:
bash Copy code
./dynamic_load_balancer
Notes
Random Task Generation : The tasks are generated with random priorities and burst times for simulation purposes.
Priority Levels : Priority levels range from 1 (highest priority) to MAX_PRIORITY (lowest priority).
Time Quantum : The time quantum is set to a fixed value (TIME_QUANTUM) for the round-robin scheduler.
Dynamic Load Balancing : The load balancer moves tasks between cores to ensure that no single core is overburdened compared to others.
Improvements
User Input : Modify the program to accept user input for tasks instead of generating them randomly.
Preemption : Implement preemptive scheduling where a running task can be preempted by a higher priority task that arrives.
Task Arrival Time : Introduce arrival times for tasks to simulate a more realistic scheduling environment.
Threading : Use threads to simulate actual parallel execution on multiple cores.
This program provides a basic simulation of dynamic load balancing with priority round-robin scheduling on a multi-core system in C. Feel free to modify and expand upon it to suit more specific requirements or to add more features.