In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use binary semaphores in Task. I hope you will get something after reading this article. Let's discuss it together.
In computer systems, we often encounter a scenario: task synchronization problem (Synchronization). For example, a task should apply to the system before using a system resource (such as some data). This application process is a kind of synchronization, and the code is roughly as follows
First there is request data, and if data is not valid at the current time, this task requires wait. In this case, OS needs to provide a mechanism to send events and notify the task in a timely manner after the data is valid.
Binary semaphore is such a mechanism: it can change the state of the task, let the task enter the blocking state when accessing the invalid resource, and make the blocked task return to the ready state when the resource is valid.
Let's take a look at the use of binary semaphores, starting with creating
When semBCreate () is successfully created, it returns the ID of the semaphore. Because the initial value can only be 0 or 1, it is called a binary semaphore. Of course, a binary semaphore (binary semaphore) is just a translated name, and some people also call it a binary semaphore. 0 means invalid resources, invalid data, no event, etc., and 1 means effective resources, valid data, event occurrence, etc. The queuing method indicates that when multiple tasks are blocked in the same semaphore, which task is unblocked after the resource is valid.
This creation is a dynamic behavior that allocates memory to the semaphore when it is created. There is also a method of static instantiation
PSem points to previously allocated space (such as compile time), which speeds up semaphore creation and satisfies some systems that prohibit dynamic memory
After you create the semaphore, you can use it. Using semaphores, there are two main functions
SemTake () is used to apply for semaphores. When the semaphore is invalid, it causes blocking, so it cannot be used in ISR.
SemGive () is used to release semaphores, which can be called in either the task or ISR.
If the semaphore is not used, the dynamically requested semaphore can be deleted by semDelete (), and the statically instantiated semaphore can be stopped by semTerminate (). If there are still tasks blocking the semaphore at this time, the tasks are directly unblocked, but its semTake () returns ERROR
Let's run an example.
Start two tasks to request semaphores, of which the second task has a higher priority
In Shell, you can use the show () command to view Kernel objects.
You can see that two tasks are blocked on this semaphore.
Conversely, with the w () or tw () command, you can see the semaphores blocked by the task
We release a semaphore directly in Shell.
You can see that the T1 application is successful, although it has a lower priority because the queuing method we use is SEM_Q_FIFO.
T2 can then be released again before the application is successful. Or, we just try to delete it.
You can see that T2 is also unblocked, except that semTake () returns ERROR because S_objLib_OBJ_DELETED
In this example, the Shell task is synchronized with T1 and T2, respectively. However, this is the synchronization between two tasks. In fact, binary semaphores also support multi-task synchronization.
When you replace semGive () with semFlush (), all tasks blocking the semaphore are unblocked, regardless of how the semaphore was queued when it was created.
You can see that both T1 and T2 are unblocked. But T2 has a higher priority, so it executes first. This kind of synchronization is called multi-task synchronization.
After reading this article, I believe you have a certain understanding of "how to use binary semaphores in Task". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.