RTOS interview questions (1)

FreeRTOS
is mainly about the system. People will ask you how to transplant the things you transplanted.
Freertos on the occurrence of interruption, processing, nesting,
said the next interruption and polling?
Framework to answer the process of an embedded system from start to finish?
How did Freertos learn?
In addition to task switching, do you know about other bottom layers of freertos?
The principle of task switching?
Talk about the principle of freertos task scheduling
How to implement a queue?
Talk about mutexes and condition variables?
Talk about the difference between a binary semaphore and a mutex semaphore?
Introduce the structure of the internship project and the application of freertos?
The underlying principle of switch_context (freertos)?
Talk about the switching scene, the implementation of the pendsv function, the logic of the pcb_current pointer in the switch_context, etc.?

uCOS
What do you think is the most important thing about ucos operating system?
How to schedule ucos tasks and how many tasks there are at most.
With the understanding of the ucos operating system, can the simple task switching function be realized independently

RTOS Common Interview Questions

  • How to realize the real-time performance of RTOS
  • How tasks communicate with each other
  • Difference between binary semaphore and mutex
  • How is task notification implemented?
  • How the RTOS kernel is scheduled
  • FreeRTOS four task states

How to realize the real-time performance of RTOS

A processor core can only run one task at a certain time, and the responsibility of the task scheduler in the operating system is to decide which task to run at a certain time.
All real-time operating systems must contain a real-time task scheduler . The biggest difference between this task scheduler and other operating systems is that it emphasizes that CPU time is allocated strictly according to priority, and time slice rotation is not a must for the real-time scheduler .

FreeRTOS is a real-time operating system that supports multi-tasking, with three scheduling methods: time slice , preemptive and cooperative .

  • Cooperative scheduling, which is mainly used on devices with limited resources, is rarely used now. For this reason, cooperative scheduling will not be removed in future FreeRTOS versions, but it will not be upgraded either.
  • Preemptive scheduling, each task has a different priority, and the task will run until it is preempted by a high-priority task or encounters a blocking API function, such as vTaskDelay.
  • Time slice scheduling, each task has the same priority, the task will run for a fixed number of time slices or encounter blocking API functions, such as vTaskDelay, to execute task switching between tasks of the same priority.

Preemptive scheduler
With preemptive scheduling, the highest priority task always gets control of the CPU once it is ready . For example, when a running task is preempted by other high-priority tasks, the CPU usage right of the current task is deprived, or suspended, and the high-priority task immediately gets control of the CPU and runs . For another example, if the interrupt service routine makes a high-priority task enter the ready state, when the interrupt is completed, the interrupted low-priority task is suspended, and the high-priority task starts running. Using a preemptive scheduler, it is known when the highest priority task can get control of the CPU and run, while optimizing task-level response time.
insert image description here

In general, the most important thing to learn about preemptive scheduling is that each task is assigned a different priority, and the preemptive scheduler will get the task with the highest priority in the ready list and run it.

Time slice scheduling is prohibited in the FreeRTOS configuration file FreeRTOSConfig.h, so each task must be configured with a different priority. When FreeRTOS multitasking is started and executed, it will basically be executed in the following way:

1. Task1, the highest priority task to be executed first, will run until it encounters system blocking API functions, such as delay, event flag waiting, semaphore waiting, Task1 task will be suspended, that is, the execution of the CPU will be released power to allow lower priority tasks to be executed.

2. The FreeRTOS operating system continues to execute Task2, the next highest priority task in the task ready list. There are two situations during the execution of Task2:

  • Task1 recovers from the suspended state to the ready state due to reasons such as the delay time and the receipt of the semaphore message. Under the action of the preemptive scheduler, the execution of Task2 will be preempted by Task1.
  • Task2 will keep running until it encounters system blocking API functions, such as delay, event flag waiting, semaphore waiting, Task2 task will be suspended, and then execute the next highest priority task in the ready list.

3. If the user creates multiple tasks and uses the preemptive scheduler, they are basically executed according to the above two. According to the preemptive scheduler, the current task is either preempted by a high-priority task, or the CPU usage right is released by calling the blocking API for low-priority tasks to execute, and idle tasks are executed when there are no user tasks to execute.

How tasks communicate with each other

All communication and synchronization mechanisms in FreeRTOS are implemented based on queues (FIFO) . But you can also use LIFO's storage buffer, that is, last-in-first-out. The queue in FreeRTOS also provides LIFO's storage buffer mechanism. 1. What is
a
queue
? FIFO: First In First Out)

2. Features of the queue:
1. Multi-task access
The queue does not belong to a specific task, any task can send messages to the queue, or extract messages from the queue.
2. Dequeue blocking
When a task tries to read a message from a queue, a blocking time can be specified. This blocking time is the time when the task is blocked when the task reads a message from the queue is invalid.
3. Enqueue blocking
Enqueue means sending a message to the queue and adding the message to the queue.

3. Queue Creation A queue
must be created before using it. There are two ways to create a queue, one is static, using the function xQueueCreateStatic() ; the other is dynamic, using the function  xQueueCreate()
1. Function xQueueCreate() This function is essentially a macro, which is used to dynamically create a queue.
2. Function xQueueCreateStatic() This function is also used to create a queue, but it uses a static method to create a queue. The memory required by the queue is allocated by the user. 3. The function xQueueGenericCreate
( ) function is used to dynamically create queues, and the memory required in the process of creating queues is allocated through the dynamic memory management function pvPortMalloc() in FreeRTOS

4. Queue initialization function
Queue initialization function prvInitialiseNewQueue() is used for queue initialization

insert image description here
5. Send a message to the queue
insert image description here
6. Read a message from the queue
insert image description here

Difference between binary semaphore and mutex

Semaphores are generally used for resource management and task synchronization. Semaphores in FreeRTOS are divided into binary semaphores , counting semaphores , mutual exclusion semaphores , and recursive mutual exclusion semaphores . Different semaphores have different application scenarios.

1. Introduction to semaphores
Semaphores are often used to control access to shared resources and task synchronization

2. Binary semaphore
Binary semaphore is usually used for mutual exclusion access or synchronization . Binary semaphore and mutex semaphore are very similar, but there are still some subtle differences. Mutual exclusion semaphore has a priority inheritance mechanism . Value semaphores have no priority inheritance . Therefore, binary signals are more suitable for synchronization ( synchronization between tasks and tasks or between tasks and interrupts ), while mutual exclusion semaphores are suitable for simple mutual exclusion access.

Like the queue, the semaphore API function allows setting a blocking time, which is the maximum number of clock ticks for the task to enter the blocking state due to the invalid semaphore when the task acquires the semaphore

A binary semaphore is actually a queue with only one queue item . This special queue is either full or empty . Isn't this just binary? Tasks and interrupts using this special queue don't care what messages are stored in the queue, they only need to know whether the queue is full or empty. This mechanism can be used to complete the synchronization between tasks and interrupts.

insert image description hereinsert image description here
3. Mutual exclusion semaphore
Mutual exclusion semaphore is actually a binary semaphore with priority inheritance . Mutual exclusion semaphore is suitable for applications that require mutually exclusive access. In mutual exclusion access, the mutex semaphore is equivalent to a key. When the task wants to use the resource, it must first obtain the key. When the resource is used up, the key must be returned, so that other tasks can hold the key. to use resources.

When a mutex semaphore is being used by a low-priority task, and a high-priority task also tries to acquire the mutex semaphore, it will be blocked. However, this high-priority task will raise the priority of the low-priority task to the same priority as itself. This process is priority inheritance. Priority inheritance minimizes the time that high-priority tasks are in the blocking state, and minimizes the impact of "priority inversion" that has occurred .

● Mutex semaphore has a priority inheritance mechanism, so it can only be used in tasks, and cannot be used in interrupt service functions.
● In the interrupt service function, the blocking time cannot be set to enter the blocking state because it needs to wait for the mutex semaphore.

4. Counting semaphore
Like a binary semaphore, the user does not need to care about what data is stored in the queue, only whether the queue is empty. Counting semaphores are usually used in the following two occasions:
1. Event counting
In this occasion, each time an event occurs, the semaphore is released in the event processing function (increasing the count value of the semaphore), and other tasks will obtain the signal Quantity
2. Resource management
In this case, the semaphore value represents the available quantity of current resources, such as the current remaining number of parking spaces in the parking lot. If a task wants to obtain the right to use resources, it must first obtain a semaphore. After the semaphore is successfully obtained, the value of the semaphore will be reduced by one. When the semaphore value is 0, it means that there are no resources. When a task finishes using resources, the semaphore must be released, and the value of the semaphore will increase by one after the semaphore is released. The initial value of the counting semaphore created in this occasion should be the number of resources. For example, there are 100 parking spaces in the parking lot, so the semaphore value should be initialized to 100 when creating the semaphore.

How is task notification implemented?

1. Introduction to task notification
Task notification is an optional function in FreeRTOS. To use task notification, you need to define the macro configUSE_TASK_NOTIFICATIONS as 1. Each task of FreeRTOS has a 32-bit notification value , and the member variable ulNotifiedValue in the task control block is the notification value. A task notification is an event. If the receiving task of a certain task notification is blocked because it is waiting for the task notification, sending the task notification to the receiving task will unblock the task. It is also possible to update the task notification value of the receiving task. The task notification can update the notification value of the receiving task through the following methods:
● Do not overwrite the notification value of the receiving task (if the last notification sent to the receiving task has not been processed).
● Override notification values ​​for receive tasks.
● Update one or more bits of the received task notification value.
● Increase the notification value of receiving tasks.

How the RTOS kernel is scheduled

FreeRTOS four task states

FreeRTOS task state (4 types)
1. Running state (Running) 2. Ready state (Ready) 3. Blocking state (Blocked) 4. Suspended state (Suspended)
Running—Running state
is called when the task is in the actual running state It is in the running state, that is, the right to use the CPU is occupied by this task.
Ready—ready state
Tasks in the ready state refer to those tasks that can run (not blocked and suspended), but are not currently running, because tasks with the same priority or higher priority are running.
Blocked—Blocked state
The state of waiting for semaphore, message queue, event flag group, etc. is called blocked state. In addition, the task call delay function will also be in blocked state.
Suspended—suspended state
Similar to the blocked state, the specified task is suspended by calling the function vTaskSuspend(). After the suspension, the task will not be executed. Only by calling the function xTaskResume() can the task be resumed from the suspended state.
insert image description here

Guess you like

Origin blog.csdn.net/u012294613/article/details/130916345
Recommended