Research on the Improvement and Application of Real-Time Operating System μC/OS-II
2026-04-06 06:39:50··#1
Traditional embedded system designs mostly employ a single-task sequential mechanism, where the application runs an infinite loop and all events are executed sequentially. Time-sensitive events are handled by timed interrupts, resulting in poor system stability and real-time performance. This weakness is particularly pronounced when the system is complex and has stringent real-time requirements. This paper introduces μC/OS-II, a multi-tasking real-time kernel primarily providing task management functionality. In real-time systems, the priority order of multiple tasks must be determined, requiring task scheduling algorithms to dynamically prioritize ready tasks. To meet increasingly stringent real-time requirements while avoiding frequent changes to ready task priorities, this paper improves the scheduling algorithm based on an analysis of the μC/OS-II source code. 1. μC/OS-II Overview μC/OS-II is a complete, portable, firmware-enabled, and customizable preemptive real-time multitasking kernel; it supports 56 user tasks and common inter-process communication mechanisms such as semaphores, mailboxes, and message queues; it is suitable for various microcontrollers and microprocessors; all code is written in ANSI C, making the program highly readable and highly portable, and it has been ported to various processor architectures and widely used in certain fields with strict real-time requirements. 1.1 Working Principle The core working principle of μC/OS-II is to approximately keep the highest priority ready task in a running state. First, the MCU is initialized, followed by operating system initialization, mainly completing the initialization of the Task Control Block (TCB), TCB priority table, TCB linked list, Event Control Block (ECB) linked list, and the creation of empty tasks. Then, new tasks are created, and other new tasks can be created within newly created tasks. Finally, the OSStart() function is used to start multitasking scheduling. After multi-task scheduling begins, a clock tick source starts timing. This tick source provides the system with periodic clock interrupt signals to achieve delay and timeout confirmation. 1.2 Task Scheduling The operating system performs task scheduling under the following conditions: interrupts (system-occupied time slice interrupt OSTimeTick(), user-used interrupts) and API function calls (user-initiated calls). One method is that when a clock interrupt occurs, the system suspends the currently executing task, saves the context, performs interrupt handling, and determines whether any task has expired. If no other task has entered the ready state, the context is restored and the original task continues to execute. Another scheduling method is task-level scheduling, i.e., calling API functions (user-initiated calls), which can be done by sending software interrupt commands or by relying on the processor to schedule during task execution. When no task enters the ready state, an empty task is executed. 2 Improvement of Scheduling Algorithms 2.1 Scheduling Strategies for Real-Time Systems In the design of multi-task scheduling algorithms for operating systems, the scheduling strategy must be determined according to the specific needs of the system. Real-time scheduling strategies can be divided into different categories according to different methods: static/dynamic, priority-based/non-priority-based, preemptive/non-preemptive, single-processor/multi-processor. In dynamic priority scheduling, the priority remains constant throughout the task's lifecycle, determined when the task is created by the system. Dynamic priority scheduling, on the other hand, involves adjusting the task's priority level at any time during its lifecycle to adapt to changes in the system's operating environment and conditions. The μC/OS-II system employs a static priority allocation strategy, where the user assigns a priority to each task. Although the task's priority can be changed using the `OSTaskChangePrio()` function, this function is simple, merely replacing the task's current priority with the user-specified new priority. With the development of real-time embedded technology, the real-time requirements for embedded systems are becoming increasingly stringent, making diverse scheduling methods a trend. This paper discusses the improvement of the earliest deadline first algorithm (ECF) in dynamic priority scheduling and its implementation in μC/OS-II. 2.2 Improvement of the Scheduling Algorithm The ECF is the optimal algorithm in dynamic priority scheduling. In the ECF algorithm, the system assigns a priority to each task based on its deadline. The earlier the deadline, the higher the priority, and vice versa. Therefore, in the improvement of the ECF algorithm described in this paper, the items listed in Table 1 need to be added to the μC/OS-II system. In the earliest-to-deadline (ETD) algorithm, the user needs to specify the deadline for each task. In this improvement, the number INT8U Prio in OSTaskCreate() and OSTaskCreateExt() is changed to INT8U deadline, and a local variable INT8U Prio is defined within the function to record the priority assigned to the task. This algorithm improvement also requires adding the OSTaskPrioCreate() function to the system, where priority is assigned based on the task's deadline. The module flow is shown in Figure 1. When adjusting the priority of ready tasks, this module first adjusts the priority of tasks in the array and records the adjustment. After executing this function, the priority of tasks in the ready task queue may change; therefore, the prio_adjust() function needs to be added to the μC/OS-II system. This function uses the original μC/OS-II function OSTaskChangePrio() to update the ready tasks, as shown in the code below. To prevent multiple tasks from calling the OSTaskPrioCreate() function simultaneously and causing confusion, this code should be treated as a critical resource; interrupts need to be disabled before the call and re-enabled afterward. 3. Application and Evaluation 3.1 System Structure In the hydraulic measurement and control HPMC module, the system is required to complete real-time acquisition, processing, and display of data from sensors at 7 positions and the user keyboard within 18ms; and for different acquired measurement data, the system is required to make real-time responses with different priorities according to the urgency of the task. The system structure is shown in Figure 2. It is divided into three layers from the outside in: the hardware circuit layer, the task layer, and the operating system layer. The hardware circuit layer mainly includes the HPMC module, user operation module, and microcontroller control module. Their main functions are as follows: the HPMC module mainly completes the real-time acquisition of sensor data; the user module mainly completes the user operation; and the microcontroller control module is used to control data reception, processing, transmission, and short message sending and receiving. The task layer has 10 parallel tasks, each consisting of the following three parts: application program, task stack, and task control block, mainly responsible for dynamically setting task priorities and transitioning task states. The operating system layer mainly involves porting μC/OS-II to the microcontroller. This system uses Atmel's MCS-51 series compatible microcontroller, and completes the specific programming of each task. 3.2 Algorithm Evaluation The choice between dynamic and static scheduling is crucial, as it profoundly impacts the system. Static scheduling is well-suited for time-triggered systems, while dynamic scheduling is better suited for event-triggered systems. Static scheduling requires careful prior design and significant effort in selecting various parameters; dynamic scheduling, on the other hand, requires less prior work, making decisions dynamically during execution. In the HPMC module, real-time processing of field-acquired measurement data places high demands on system real-time performance. The μC/OS-II static priority scheduling algorithm proves ineffective when task priorities change. However, application in a hydraulic measurement control system demonstrates a significant improvement in real-time performance after the improvement. Conclusion This paper improves the μC/OS-II static scheduling algorithm, implementing a deadline-first scheduling algorithm in the system. Application in a hydraulic measurement control system shows that this improvement significantly enhances system real-time performance; however, the improved algorithm places higher demands on system memory and CPU, exhibiting certain limitations.