"Time Analysis of Embedded Software" reading activity: 3 Chapter 3 Reading Notes - Operating Systems
[Copy link]
Chapter 3 explains the knowledge related to embedded operating systems, but is limited to schedulers.
The chapters are as follows:
第3章 操作系统 33
3.1 无操作系统: 无限循环加中断 33
- 3.1.1 周期中断的实现示例 33
- 3.1.2 轮询——无中断地实现 34
- 3.1.3 可扩展性 36
3.2 OSEK/VDX 36
- 3.2.1 任务 36
- 3.2.2 中断 39
- 3.2.3 ErrorHook 39
- 3.2.4 基本调度策略 39
3.3 多任务: 协作与抢占 41
- 3.3.1 两种追踪的图示说明(示例 1) 41
- 3.3.2 堆栈消耗(示例 2) 43
- 3.3.3 确保数据一致性 45
- 3.3.4 协作式多任务处理的限制 45
- 3.3.5 为减少OS_Schedule()调用而可实施的优化 47
- 3.3.6 总结 47
3.4 POSIX 47
- 3.4.1 进程 49
- 3.4.2 线程 49
- 3.4.3 POSIX 线程状态图 51
- 3.4.4 调度策略 52
3.5 总结 52
1. No operating system
As long as the system scheduling is very simple and there is no explicit requirement for the operating system, an operating system can be omitted. The operating system will consume additional resources (memory, runtime, stack, storage, etc.).
There are two ways to not use an operating system:
- Periodic interrupt implementation: A timer interrupt ISR is used to perform periodic tasks, which are executed in the interrupt handler. The while(1) infinite loop in main() is used to perform other tasks that do not need to be executed periodically.
- Polling-non-interrupt implementation: A timer interrupt is required, but a timer interrupt handler is not required. The interrupt overflow flag is queried in while(1) in main(). If the interrupt overflows, it means that the timer time has expired, and then the periodic task is started.
The periodic task in the second method mentioned above may not be executed in time, because there are other codes in while(1) that do not need to be executed periodically, which may affect the real-time execution of the periodic code. The difference between the actual execution time and the planned execution time is called jitter.
2. AXIS/VDX
OSEK/VDX is an open system and corresponding interface standard used in the field of automotive electronics.
The following figure shows the operating system status defined in the OSEK/VDX specification:
3. Multitasking: Collaboration and Preemption
In real-time operating systems, what OSEK/VDX achieves by supporting preemptible and non-preemptible attributes is often referred to as preemptive and cooperative multitasking.
Here is my summary of cooperative and preemptive multitasking:
- Collaborative: Tasks will not preempt each other. High-priority tasks will not interrupt low-priority tasks that are being executed. Instead, they will wait for low-priority tasks to complete or actively give up the CPU before executing.
- Preemptive: There is preemption between tasks. A high-priority task will interrupt the executing low-priority task. When the high-priority task is completed, it will return to the previous low-priority task to continue execution.
3.1. Stack consumption
The book explains the stack consumption of cooperative multitasking and preemptive multitasking through specific examples. Since there is no preemption in cooperative multitasking, the stack consumption is much less than that of preemptive multitasking. Moreover, for preemptive multitasking, it is difficult to calculate the stack because it is unknown when a task will be preempted.
My understanding is that the consumption of the stack by preemptive multitasking is somewhat similar to function nesting. When there are many preemptive tasks, the nesting of each level leads to huge stack consumption, so the execution time and cycle of the tasks need to be strictly designed to avoid this situation.
Data Consistency
Preemptive multitasking: Additional runtime and RAM space are required to ensure data consistency between tasks, because data copies need to be created and synchronized at task start and node count.
Collaborative multitasking: There is no task preemption, so there is no need to consider data consistency, which can save a lot of running time and RAM space.
3.3. Advantages and Disadvantages
Advantages of cooperative multitasking:
- Since a task change is performed, all typical real-time problems are avoided.
- When configured correctly, this can eliminate much of the work of ensuring data consistency, reducing execution time and memory requirements.
- Less stack usage.
Although collaborative multitasking has many advantages, it also has a fatal disadvantage, which is that the real-time performance of the tasks cannot be guaranteed. It is possible that high-priority tasks cannot be executed in a timely manner. This problem needs to be solved through reasonable system design, which places relatively high requirements on system design.
Advantages of preemptive multitasking:
- When starting a task with a higher priority, the latency is shorter and the jitter is much smaller than that of cooperative multitasking.
- Since there is no need to limit the execution time of a function, there is no need to split functions due to long execution time (which is required for the collaborative approach).
4. POSIX
The POSIX standard is a set of IEEE standards that, at its core, describe the interface between applications and operating systems .
POSIX has several versions, PSE51, PSE52, PSE53, and PSE54. This book explains the features of each version. You can find the relevant description in the book (page 48). Here is a brief explanation:
- PSE51: Minimal-real-time system framework;
- PSE52: Real-time controller-system framework;
- PSE53: Dedicated - Real-time system framework;
- PSE54: A general-purpose real-time systems framework.
4.1. Processes and Threads
process:
A process is a program that is executed using its own data and the data of the operating system required for execution, including status information, access permission information, etc.
I am still confused after reading this sentence in the book. I found a good article on the Internet that explains the LINUX process. You can take a look at: https://www.bookstack.cn/read/understand_linux_process/process_basic-what_is_process.md
PSE53 and PSE54 allow multiple processes (programs) to be executed simultaneously. Each process has a virtual memory area that cannot be accessed by other processes. In addition, a process can create new processes called "child processes".
Threads:
The processing of machine instructions in a program is like a thread, or more precisely, like a single thread, and the instructions will be executed in order. Each process starts with a single thread or main thread. If you want to perform activities in parallel, that is, split the program flow, you must create more threads, which is called multithreading. These threads can access the virtual memory of the process.
Data consistency issues in threads:
Data consistency issues also exist in threads, and shared data can be protected by mutexes .
4.2. Thread State Diagram
- When the program starts, a process is created and the associated main thread is set to the "new" state.
- After the operating system performs basic initialization, the thread changes to the "Ready" state, where the thread waits for its code to execute.
- When it begins execution, a thread is assigned the "Running" state.
- There may be several reasons that cause its state to change to "waiting" (sleeping or blocking).
- The highest priority thread waiting to be processed will be restored to the "Running" state.
- When a thread finishes, it changes to the "Completed" state.
Found possible typos in the book:
4.3. Multithreaded Scheduling Strategy
When multiple threads are in the "Ready" state, the scheduling policy defines a set of rules for determining which of the available threads will be executed for processing. Here are only the most important ones:
- Select by priority: Similar to preemptive OSEK/VDX, the highest priority thread is executed first.
- Time slice process (i.e. polling): Each thread will process for a defined period of time before it is the turn of the next thread.
- First In, First Out (FIFO): In this context, "First In, First Out" means that threads are processed in the order they switch to the Ready state.
5. Conclusion
This chapter first introduces the advantages of not using an operating system and the advantages of using an operating system. It also introduces two operating systems with rules, OSEK/VDX and POSIX, which are used in the automotive industry. It analyzes the pros and cons of cooperative and preemptive multitasking, and finally explains processes and threads in POSIX.
The introduction in this chapter is relatively simple, because learning a system cannot be explained clearly in a short chapter. The author only finds out the most important parts of the system and analyzes and explains them. To further understand the system, you need to specifically read the relevant documents.
|