Detailed explanation of Ucos resources and shared resources

This article mainly shares the similarities and differences between resources and shared resources in ucos , and I hope it will be helpful for everyone to learn ucos .

  resource:

  Any entity occupied by a task. Such as: printers, keyboards, monitors, etc. In addition, the resource can also be a variable, a structure or an array.

  Therefore, the resources in our operating system are generalized resources. Anything that serves a task is called a resource.

  Share resource:

  A resource used by more than one task.

  For example, there is a variable here. This variable is a global variable. The global variable assumes that it is the state of an LEDFlag . In an LED , we need to modify or use the LEDFlag . In another key, we also need to Using LEDFlag , in this case, this variable is used by two tasks. First, this variable is a resource, and then this resource is used by two tasks. Such resources are called shared resources.

  When we use shared resources, we must use some means to ensure that there are no problems with shared resources. What does that mean? Assuming that the LEDFlag has just been changed to 1 in the LED , and then changed to 0 in the button, that is, the LEDFlag is changed to 1 when the task is executed to the LED , and then the state is judged. But before this, the button just changed it to 0. At this time, we see that there is a problem. We change it to 1. At this time, it is judged that we should be 1 , but in fact, due to the task of the button Function, it has become 0 , so about access to shared resources, we must meet a condition called mutual exclusion.

  Access to shared resources must satisfy mutual exclusion: (exclusive)

  Methods to satisfy mutual exclusion:

  1. Turn off the interrupt and open the interrupt.

  Before using this global variable or shared resource, we first turn off this interrupt, and when the shared resource is used up, we can turn this interrupt on again.

  2. Use the test and set command.

  Assuming that there is still an LED light task and a button task, a shared resource called LEDFlag will be used. At this time, we use a test and set it. We first add a variable bit , and we initialize the variable bit to 0 , when the LED task wants to use LEDFlag , I first judge whether the bit is equal to 0 , if the bit is equal to 0 , we set the bit to 1 , if the bit is equal to 1 , it means that our LED is in use, and then you can After using LEDFlag , we will set the bit to 0 after use, so that each task will first judge the status of the bit before using the LEDFlag . After the judgment, if it can be used, set the bit to 1, after using the shared resource, set the bit to 0 again , this method is to test and set the instruction. We found that such a test and set command can avoid our shared resource access problem very well.

  3. The task is forbidden to switch, use, and allow the task to switch.

  Because it is not switched by tasks, then at this time, we will not be interrupted by other tasks. At this time, we can use this shared resource. After using it, we can turn on the task switching function.

  4. Use semaphore.

  It is similar to our use test and assert command, that is, we first open a semaphore before we use the shared resource. We use this semaphore to restrict access to shared resources, what should we do? First of all, it is the semaphore. Assuming that the shared resource is being accessed, we set the semaphore to 1. Next, we access the shared resource. After accessing, we turn the semaphore to 0 , but here we need to Note that it has two professional terms for semaphore, one is called acquiring semaphore and the other is called releasing semaphore. That is to say, when we use a shared resource, we first acquire the semaphore. After obtaining it, we use the shared resource. After using it, we release the semaphore.

 

Original link: http://www.maiziedu.com/wiki/iot/ucosterm/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326349629&siteId=291194637