VxWorks - Semaphore

VxWorks study notes - semaphore

One. The concept of semaphore

It is the main mechanism to achieve mutual exclusion and synchronization of tasks. The semaphore provided by VxWorks has been highly optimized and has the fastest speed among all inter-task communication mechanisms.

 

two. Classification of semaphores

Binary Semaphores (binary): The best way to complete mutually exclusive and synchronous operations; the fastest and the most commonly used.

Mutual Exclusion Semaphores (mutual exclusion): A special binary semaphore optimized for mutual exclusion operations.

Counting Semaphores : Similar to binary semaphores, it can record the number of times the semaphore is released, and can monitor multiple instances on the same resource.

 

 

three. Binary Semaphores ( Binary Semaphores )

    Tasks often wait for events or need to acquire resources. Polling is not allowed in the RealTime system in principle. It is best to use Pending to wait for events or resources.

State diagram:

 

Description:

1. Call semBCreate() for a resource to create a binary semaphore and specify:

SEM_Full (resource is available) SEM_Empty (resource is not available).

2. When the resource is unavailable, Task calls semTake() to enter Pending until the semaphore is Given

 

related functions:

SEM_ID semBCreate ( int options, /* semaphore options */

SEM_B_STATE initialState  /* initial semaphore state */

)

STATUS semTake
(
SEM_ID semId, /* The semaphore ID to be obtained */

int timeout /* 超时时间(tick)/no-wait/forever */

)

 

ISR (interrupt service routine) cannot call semTake() operation!

 

 

STATUS semGive (SEM_ID semId /* ID of the semaphore to be released */)

 

 

semFlush()

 

Application direction:

1. Mutually exclusive operation : It means that different tasks can use semaphores to mutually exclusive access critical resources. This mutually exclusive access method is better than interrupt disable and priority locking

(Preemptive locks) The two mutually exclusive methods have a more precise granularity.

The initial state is set to (SEM_FULL) available for mutual exclusion operation. In the same Task, semTake() and semGive() are called in pairs and sequentially.

 

2. Synchronous operation : refers to a task that can use the semaphore to control its own execution progress and synchronize itself to a set of external events. The initial state is set to (SEM_EMPTY) unavailable during synchronization operation. Separately call semTake() and semGive() in different tasks.

 

 

four. Mutual Exclusion Semaphores (mutual exclusion semaphores)

    Mutually exclusive semaphore is a special binary semaphore, which is designed for some problems that exist when binary semaphores are used for mutual exclusion operations. Mutex semaphores mainly increase the processing of priority inversion, deletion security, and recursive access.

State diagram:

related functions:

SEM_ID semMCreate ( int options /* mutex semaphore options */ )

the difference:

1. Mutually exclusive semaphores can only be used for mutually exclusive operations.

2. It can only be released by the task that has acquired the mutex semaphore.

3. The interrupt service routine (ISR) cannot release (semGive()) the mutually exclusive semaphore.

4. Mutex semaphores do not support the semFlush() operation.

 

Application direction:

1. Avoid Priority Inversion :

In the above figure, task2 is waiting for the resources of task1 , so it is in the Pend state. At this time, a medium-priority task comes in and preempts the CPU of task1 . At this time, the performance is that the low-priority task is executed before the high-priority task2 . . This phenomenon is the first inversion.

 

Use semId = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE); to avoid inversion.

At this time, the priority of task1 is increased the same as task2 , until task2 is executed.

SEM_INVERSION_SAFE cannot be paired with SEM_Q_FIFO !

 

2.Deletion Safety ( secure deletion )

Use: semId = semMCreate(SEM_Q_FIFO | SEM_DELETE_SAFE); can be safely deleted.

The essence is: before Task executes the semTake() operation on the mutex semaphore and successfully occupies the semaphore, it implicitly executes the taskSafe() operation; after executing the semGive() operation, it implicitly executes the taskUnsafe() operation.

If a task task1 tries to delete a task task2 that has been protected, task1 will be blocked until task2 is unprotected (releasing the mutex semaphore with deletion protection) to complete the deletion.

 

3. Recursive access

InitFun () {sem_ID = semMCreate (…); }

funB ()
{ semTake (sem_ID, SEM_FOREVER);

/*访问临界资源*/

semGive(sem_ID);

}

funA ()
{ semTake (sem_ID, SEM_FOREVER);

/*访问临界资源*/

funB();  //递归访问, 而不会死锁

semGive(sem_ID);

}

 

Fives. Semaphores a Counting (counting semaphores)

Both counting semaphores and binary semaphores can be used for synchronization and mutual exclusion between tasks. The difference is that counting the semaphore can record the number of times the semaphore is released, which can be used to monitor the use of a certain resource.

State diagram:

related functions:

SEM_ID semCCreate ( int options, /* semaphore option modes */

int initialCount          /* initial count */

)

 

VxWorks study notes - semaphore

One. The concept of semaphore

It is the main mechanism to achieve mutual exclusion and synchronization of tasks. The semaphore provided by VxWorks has been highly optimized and has the fastest speed among all inter-task communication mechanisms.

 

two. Classification of semaphores

Binary Semaphores (binary): The best way to complete mutually exclusive and synchronous operations; the fastest and the most commonly used.

Mutual Exclusion Semaphores (mutual exclusion): A special binary semaphore optimized for mutual exclusion operations.

Counting Semaphores : Similar to binary semaphores, it can record the number of times the semaphore is released, and can monitor multiple instances on the same resource.

 

 

three. Binary Semaphores ( Binary Semaphores )

    Tasks often wait for events or need to acquire resources. Polling is not allowed in the RealTime system in principle. It is best to use Pending to wait for events or resources.

State diagram:

 

Description:

1. Call semBCreate() for a resource to create a binary semaphore and specify:

SEM_Full (resource is available) SEM_Empty (resource is not available).

2. When the resource is unavailable, Task calls semTake() to enter Pending until the semaphore is Given

 

related functions:

SEM_ID semBCreate ( int options, /* semaphore options */

SEM_B_STATE initialState  /* initial semaphore state */

Guess you like

Origin blog.csdn.net/mid_Faker/article/details/111314674