Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to count semaphores by Task in vxworks

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the vxworks Task how to count semaphores, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

It is mentioned that binary semaphores are used to solve synchronization problems. Let's look at an example of synchronization

This code is very simple, roughly meaning that every time you successfully apply for a semaphore, you will print a sentence.

Start a task (T1) to call this function:

You can see that T1 is blocked on the semaphore semId. Release a semaphore directly to it.

Task (T1) printed a sentence indicating that a semaphore was received

Next, try releasing the semaphore twice, using the Shell command repeat ()

Task (T1) also printed two sentences indicating that two semaphores were received.

Next, try several times.

As you can see, when the number of repeat is greater than 2, the task (T1) can only receive semaphores twice.

Let's look at the operation flow of semGive ().

As you can see from the figure above, when repeat () first releases the semaphore, it sets T1 of the blocking state to the ready state. On the second release, there is no task blocking, so the semaphore is set to valid (0-> 1), and then released, the semaphore is set to valid (1-> 1). Until the execution of repeat () is finished, T1 in the ready state begins to perform subsequent operations, and the first print occurs. Then you can successfully apply for a semaphore (1-> 0), and you will have a second print. After that, the semaphore is invalid again, and T1 enters the blocking state again.

This is the characteristic of binary semaphores, which are used to indicate whether the event has occurred, but not the number of times the event has occurred.

What if you need to record the number of events that occur? Try raising the priority of T1.

But VxWorks specifically provides semaphores for counting: counting semaphores.

SemCCreate () is used to dynamically create counting semaphores, and semCInit () is used to initialize statically assigned semaphores.

InitCount represents the initial value of the count semaphore. Because it is a signed integer value, its value range is 0-2147483647 (0x0-0x7fffffff).

And the specific use is very similar to binary semaphores.

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.

Unlike binary semaphores, the value of a count semaphore does not vary between 0 and 1, but uses a count to record the specific value. And the current value of count can exceed 2147483647 (0x7fffffff)

After that, semGive () and semTake () can still operate normally. It's just that during the show () operation, you can only see a value of 31 bits lower.

As you can see from the source code, it will overflow only if the count exceeds 4294967295 (0xffffffff). It seems that this should be a little bug of VxWorks.

Anyway, in practical applications, the value of count is unlikely to be that large. Let's go back to the starting position and change the example of testSemB.

At this time, release the semaphore many times, and the task (T1) can receive it many times.

At the same time, the counting semaphore also supports the semFlush () operation, that is, it can also be used for multitasking synchronization.

Finally, run an example of static initialization.

MySem allocates space at compile time, so there is no need to apply for memory dynamically in semCInit ().

Thank you for reading this article carefully. I hope the article "how to count semaphores in vxworks" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report