What is the purpose of K_SEM_DEFINE in semaphore using zephyr rtos??
What is the purpose of K_SEM_DEFINE in semaphore using zephyr rtos??
Hi,
It is used to define and initialize a semaphore, for example:
K_SEM_DEFINE(my_sem, 0, 1);
This is the same as doing the following:
struct k_sem my_sem;
k_sem_init(&my_sem, 0, 1);
Best regards,
Marte
thanks but what is the purpose means suppose if we add this fun api in led e.g using k_no_wait it will work without adding this api function. but if u add k_forever it will work how it is possible ????
thanks but what is the purpose means suppose if we add this fun api in led e.g using k_no_wait it will work without adding this api function. but if u add k_forever it will work how it is possible ????
Hi,
A semaphore is used when you have a common resource shared between threads and you want to limit the access to it. Semaphores have a count and a limit. The count is how many times the semaphore can be taken, and the limit is the maximum value that the semaphore's count can be. So in the example in my previous reply I create a semaphore called my_sem with a count of 0 and limit of 1. This means that the semaphore is currently unavailable, since it can currently be taken 0 times. It also means that the maximum value the count can be is 1.
When I talk about taking and giving a semaphore I mean that the threads are taking or giving the semaphore. When a thread takes a semaphore the count will decrement. However, the count cannot be negative, so if the count is 0 then the thread cannot take the semaphore. This means that the resource I want to protect access to is already used by another thread, and so my thread cannot access it right now. When a thread gives a semaphore, the count will increase by one (as long as the count is not at the limit). So the thread is no longer using the common resource and is releasing it's access to it so that another thread can access it.
But you asked about k_forever specifically. When you take a semaphore with k_sem_take() you can give it a timeout. This timeout is the waiting period for the thread to take the semaphore, that is, how long the thread will wait before it tries to take the semaphore. If you use K_FOREVER instead of giving it a timeout, then the thread will wait until the semaphore is available.
Best regards,
Marte
Thank you . can u explain in detail count and limit
Hi,
What specifically are you wondering about? You can find more information in the documentation here Semaphores, and there are a lot of resources and information about semaphores available online. As I stated in my previous reply, count is how many times the semaphore can be taken. If the count is 3 then it can currently be taken 3 times, if it is 1 it can be taken 1 time, and if it is 0 then it is unavailable and cannot be taken right now. Count is a number that will change depending on how many times the semaphore can be taken right now. Taking a semaphore will make the count decrease by one (as long as the count is not zero), and giving a semaphore will make it increase by one (as long as the count is not at the limit). And limit is the maximum number that count can be. So if limit is 3, then at the maximum the semaphore is only available to 3 threads at the same time. The limit will not change, and is set when you initialize the semaphore.
Best regards,
Marte