Task Scheduling System in C++

In this exercise, you will implement a C++ program that simulates a task scheduling system. The system will allow tasks to be scheduled at specific intervals and execute them based on predefined timings. The program will utilize a timer or delay mechanism to simulate the scheduling of tasks, and each task will be executed at the scheduled time. This will help you understand how to manage time-based task execution in a system.

Group

Real-Time Programming in C++

Objective

Write a C++ program that simulates a task scheduling system. The program should allow tasks to be scheduled at specific intervals and execute them accordingly. You can simulate different tasks, such as displaying messages or performing simple actions, and schedule them to run at specified times or intervals. Use a loop to monitor the task schedule and execute tasks when their scheduled time arrives.

Implement a task scheduling system.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include the input-output stream library
#include <thread>     // Include the thread library for time management
#include <chrono>     // Include the chrono library for handling time intervals

using namespace std;

// A simple task function to simulate a scheduled task
void task(string taskName) {
    cout << taskName << " task executed!" << endl;  // Print a message indicating the task is executed
}

// Function to schedule and manage tasks
void scheduleTasks() {
    // Schedule tasks with different time intervals
    this_thread::sleep_for(chrono::seconds(3));  // Wait for 3 seconds
    task("Task 1");  // Execute Task 1 after 3 seconds
    
    this_thread::sleep_for(chrono::seconds(2));  // Wait for 2 seconds
    task("Task 2");  // Execute Task 2 after 2 more seconds
    
    this_thread::sleep_for(chrono::seconds(4));  // Wait for 4 seconds
    task("Task 3");  // Execute Task 3 after 4 more seconds
    
    this_thread::sleep_for(chrono::seconds(1));  // Wait for 1 second
    task("Task 4");  // Execute Task 4 after 1 more second
}

int main() {
    // Start the task scheduling system
    cout << "Scheduling tasks..." << endl;
    scheduleTasks();  // Call the scheduleTasks function to manage task execution

    return 0;
}

 Output

Scheduling tasks...
Task 1 task executed!
Task 2 task executed!
Task 3 task executed!
Task 4 task executed!

Share this C++ Exercise


More C++ Programming Exercises of Real-Time Programming in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.