RTT review

1. Essential knowledge

①,: 多线程In a multi-threaded system, according to the function of the program, we divide the main body of the program into independent small programs that loop infinitely and cannot return. We call this small program a thread. Each thread is independent, does not interfere with each other, and has its own priority, which is scheduled and managed by the operating system.

②, each thread needs to allocate an independent stack space-a pre-defined global array or dynamically allocated section of memory space, stored in RAM

③. For the smoothness of the system **调度线程**, each thread is additionally defined ** 线程控制块**——"ID Card". The operation of the system on the thread can be realized through the control block

④ After the thread is created, join it **就绪列表**, indicating that the system can be scheduled at any time.

⑤ The scheduler is the core of the operating system- **实现线程的切换**.
Steps: initialization and startup.
System scheduling: Find the ready thread with the highest priority in the ready list, and then execute it.

Critical section -a section of code that cannot be interrupted during execution.
(Treatment of critical section protection: directly turn off all interrupts, except for NMI FAULT and hard FAULT)
说明: Critical section is a section of code that exclusively accesses some shared resources, and only one thread is allowed to access shared resources at any time. If there are multiple threads trying to access the critical section at the same time, all other threads trying to access this critical section will be suspended after one thread enters, and continue until the thread that enters the critical section is opened. After the critical section is released, other threads can continue to preempt and use this to achieve the purpose of operating shared resources in an atomic manner.

Objects : use the kernel object management system to access/manage all kernel objects.
RT-Thread kernel objects include: threads, semaphores, mutexes, events, mailboxes, message queues and timers, memory pools, device drivers, etc. The object container contains information about each type of kernel object, including object type, size, and so on. The object container assigns a linked list to each type of kernel object, and all kernel objects are linked to the linked list, as shown in the following figure of the RT-Thread kernel object container and linked list:
Insert picture description here

⑧. Container -Whenever a user creates an object, put the object in the container to facilitate management
. Whether the members of the container are valid or not is determined by the macro definition
Insert picture description here

⑨. Idle thread and blocking delay
-blocking delay-that is, when the thread needs to be delayed, the thread will give up the right to use the CPU, and the CPU can do other things. When the thread delay time expires, it will regain the CPU usage Right, the thread continues to run,
Insert picture description here

⑩, multi-
priority threads of the same priority will be inserted into the same linked list (time slice).
Different priorities are inserted into the thread priority table according to the priority.
Insert picture description here

11. Timer, each thread will have a built-in timer
Insert picture description here

12. Support time slice
-when there are two or more threads under the same priority, the thread supports the time slice function, that is, we can specify the time for the thread to continue to run once, the unit is tick.

2. Application knowledge

(1) Create thread -static or dynamic.
Insert picture description here

Remap the serial port to the rt_kprintf function.

(2) Start-up process
Method one: 将所有线程一次性创建后再调度。
Insert picture description here
Method two: initialize the hardware and RTOS system in the main function first, and then the 创建一个启动线程后就启动调度器,然后在启动线程里面创建各种应用线程created threads will have different priorities, which will be dealt with according to the specific situation. When all threads are created successfully, Start the thread to delete itself.
(Refer to: https://blog.csdn.net/qq_41070511/article/details/103179499)
Insert picture description here

(3) Thread management .
Insert picture description here
The principle of thread scheduling : once the thread state changes, and the currently running thread priority is less than the highest priority of the thread in the priority queue group, the thread switch is immediately performed (unless the current system is in the interrupt handler or thread switching is prohibited status).
Thread state
Insert picture description here
thread migration
Insert picture description here

(Iv), the message queue --- for communication between threads of the data structure , i.e., between the threads and the thread, and the interrupt transfer information between threads.
The blocking mechanism of the message queue -purpose: in order to protect each thread from the process of normal completion of read and write operations on the message queue without interference from subsequent threads,

(5) Semaphore
-a mechanism to achieve communication between threads, to achieve synchronization between threads or mutually exclusive access to critical resources, and is often used to assist a group of competing threads to access critical resources.
The semaphore is like a key, which locks a critical section, and only allows the thread with the key to access it: the thread is allowed to enter the critical section after it has the key; and after leaving, the key is passed to the waiting thread in the queue. , Let subsequent threads enter the critical section in turn.

(Vi) Mutex
-the interlock to protect resources, the priority inheritance algorithm can be used to reduce the impact of the priority inversion problem

(Vii) Event
-Event is a mechanism to realize communication between threads, which is mainly used to realize synchronization between threads, but event communication can only be event type communication without data transmission.
The event is used for event type communication, no data transmission.
——Events can be used as flag bits to determine whether certain events have occurred, and then deal with them according to the results

(8) Software timer
-The software timer uses the system tick period as its timing unit. Starting from the specified moment, a specified time elapses, and then a timeout event is triggered.

(Ix) Mailbox
-Mailboxes can be between threads. Message transfer between interrupts and threads.

(X) Memory management
- manage the use of memory by users and the system through the application and release operations of the memory, so as to optimize the utilization and efficiency of the memory, and to solve the problem of memory fragmentation in the system to the greatest extent.
——Divided into: static memory management (memory pool) and dynamic memory management (heap memory management module).

Guess you like

Origin blog.csdn.net/qq_41070511/article/details/106633865