• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Basic Concepts<a name="EN-US_TOPIC_0000001124066587"></a>
2
3-   [Working Principles](#section1794010261861)
4    -   [Semaphore control block](#section11372149164815)
5    -   [Working Principles](#section139726510491)
6
7
8Semaphore is a mechanism for implementing inter-task communication. It implements synchronization between tasks or exclusive access to shared resources.
9
10In the data structure of a semaphore, there is a value indicating the number of shared resources available. The value can be:
11
12-   **0**: The semaphore is unavailable. Tasks waiting for the semaphore may exist.
13-   Positive number: The semaphore is available.
14
15The semaphore for synchronization is different from the semaphore for mutex:
16
17-   Semaphore used for exclusive access: The initial semaphore counter value is not 0, indicating the number of shared resources available. The semaphore counter value must be acquired before a shared resource is used, and released after the resource is used. When all shared resources are used, the semaphore counter is reduced to 0 and the tasks that need to obtain the semaphores will be blocked. This ensures exclusive access to shared resources. In addition, when the number of shared resources is 1, a binary semaphore \(similar to the mutex mechanism\) is recommended.
18-   Semaphore used for synchronization: The initial semaphore counter value is 0. Task 1 cannot acquire the semaphore and is blocked. Task 1 enters Ready or Running state only when the semaphore is released by task 2. In this way, task synchronization is implemented.
19
20## Working Principles<a name="section1794010261861"></a>
21
22### Semaphore control block<a name="section11372149164815"></a>
23
24```
25/**
26 * Data structure of the semaphore control block
27 */
28typedef struct {
29    UINT16            semStat;          /* Semaphore status */
30    UINT16            semType;          /* Semaphore type*/
31    UINT16            semCount;         /* Semaphore count*/
32    UINT16            semId;            /* Semaphore index*/
33    LOS_DL_LIST       semList;          /* Mount the task blocked by the semaphore.*/
34} LosSemCB;
35```
36
37### Working Principles<a name="section139726510491"></a>
38
39Semaphore initialization: The system allocates memory for the semaphores configured \(you can configure the number of semaphores using the  **LOSCFG\_BASE\_IPC\_SEM\_LIMIT**  macro\), initializes all semaphores to be unused semaphores, and adds them to a linked list for the system to use.
40
41Semaphore creation: The system obtains a semaphore from the linked list of unused semaphores and assigns an initial value to the semaphore.
42
43Semaphore request: If the counter value is greater than 0, the system allocates a semaphore, decreases the value by 1, and returns a success message. Otherwise, the system blocks the task and adds the task to the end of a task queue waiting for semaphores. The wait timeout period can be set.
44
45Semaphore release: When a semaphore is released, if there is no task waiting for it, the counter is increased by 1. Otherwise, the first task in the wait queue is woken up.
46
47Semaphore deletion: The system sets a semaphore in use to unused state and inserts it to the linked list of unused semaphores.
48
49Semaphore allows only a specified number of tasks to access a shared resource at a time. When the number of tasks accessing the resource reaches the limit, other tasks will be blocked until the semaphore is released.
50
51**Figure  1**  Semaphore working mechanism<a name="fig467314634214"></a>
52![](figure/semaphore-working-mechanism.png "semaphore-working-mechanism")
53
54