1/6
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a RTOS?
A type of operating system designed to manage hardware resources and execute tasks within strict timing constraints
Task : A set of program instructions loaded into memory
Thread : A unit of CPU utilisation with its own program counter and stack
Process : Instance of a computer program
Task Scheduling
The CPU core needs to divide its time between tasks, where a task is interrupted at a regular interval (1ms typically), this is called a time slice or tick
The OS is required to run every time slice to figure out which task to schedule next based on the task’s priority
Tasks with equal priority are executed in turn in a round-robin fashion, B and C will continue to take turns to execute as long as they need to run
Preemptive scheduling is where CPU time is taken to run a higher priority task from a lower priority task
A hardware interrupt will always have a higher priority than other software running
Task States
When a task is created, it automatically goes into a ‘Ready’ state, scheduler can choose to run that task if only they are no other higher priority tasks are ready to run
If a task is not run, because another task with equal or higher priority was chosen, the task remains in the ‘Ready’ state
When the scheduler decides to run a task, the task moves to a ‘Running’ state and remain in that state until it stops running. If the CPU has only one core, only one task at any given time can be in the ‘Running’ state
The task whilst running may call an API function (vTaskDelay, waiting for a queue or semaphore) that moves it to the ‘Blocked’ state
Tasks in the ‘Blocked’ state do not run on the processor and can not be selected to be ran, the task will move to the ‘Ready’ state when the unblocking event has occurred
A task can be put in the ‘Suspended’ state, which the task can be suspended from within the task or from another task
A task in the ‘Suspended’ state, can not be selected to run, and only an explicit call to resume the task will allow a task to move to the ‘Ready’ state
Context switching
The context of a task is the complete set of information that defines the task's current state, enabling it to resume execution from where it was paused. This includes:
Processor State:
CPU registers (e.g., general-purpose registers, program counter, stack pointer).
Status registers (e.g., condition flags or interrupt states).
Task Stack:
Stores temporary data such as local variables and return addresses.
Holds saved registers during a context switch.
Control Information:
Task-specific data stored in a Task Control Block (TCB), such as:
Task priority.
Scheduling information.
Task ID and state (e.g., ready, running, blocked).
Context switching is the process in which an operating system or RTOS saves the state (context) of a currently running task and restores the state of another task, allowing the CPU to switch between tasks efficiently.
Steps in Context Switching
Save Current Task Context:
The RTOS saves the state of the currently running task (e.g., program counter, stack pointer, and CPU registers) into its TCB or stack.
Select Next Task:
The scheduler determines the next task to run based on the RTOS's scheduling algorithm (e.g., priority-based or round-robin).
Restore Next Task Context:
The RTOS loads the state of the selected task from its TCB or stack, restoring its registers and program counter.
Resume Execution:
The CPU begins executing the selected task from where it left off.
Memory Allocation
A section of memory is allocated specifically for global and static variables, this section is called ‘static memory’, allocating memory to global or static variables is called ‘static allocation’. These variables last for the duration of the entire program
Local variables are pushed onto the ‘stack’ which can grow in size as the program runs, using a LIFO system, memory is deallocated when function returns, allocating memory to local variables is called ‘automatic allocation’
Heap (usually, dependent on processors) grows towards the stack. The heap is used for dynamic allocation (such as malloc)
RTOS Memory Allocation
When a new task is created, a portion of the heap is assigned to that task. The memory portion is divided into a Task Control Block (TCB) and a stack unique to that task
Dynamic allocation attempts to find the largest contiguous memory block available
Constantly allocation and freeing heap may cause it to fragment, which can cause to grow towards stack more quickly even if there are free spots available