VxWorks interprocess communication 2--signal volume

Source: Internet
Author: User
Tags mutex semaphore

VxWorks interprocess communication 2--signal Volume one. The concept of signal volume

is the main mechanism to realize the mutual exclusion and synchronous operation of tasks. The VxWorks provides a highly optimized semaphore that is the fastest in all communication mechanisms between tasks.

Two The classification of Signal volume

Binary semaphores(binary): The best way to complete mutual exclusion, synchronous operation, the fastest, most commonly used.

Mutual exclusion semaphores(mutex): A special binary semaphore optimized specifically for mutex operations.

counting semaphores(count): Similar to binary semaphores, records the number of times a semaphore is released and can monitor multiple instances on the same resource.

Three Binary semaphores (binary semaphore)

Task often waits for an event or needs to obtain resources. Queries (polling) are not allowed in principle in realtime systems, preferably with pending, waiting for events or resources.

State diagram:

Description

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

Sem_full (Resource available) Sem_empty (resource unavailable).

2. When the resource is unavailable, the task calls Semtake () into pending until Semaphore is given

Related functions:

[C-sharp] View plaincopy

    1. sem_id sembcreate

    2. (

    3. int options,/* Semaphore options */

    4. Sem_b_state initialstate/* Initial semaphore state * *

    5. )

    6. STATUS Semtake

    7. (

    8. sem_id Semid,/* The semaphore ID to get/*

    9. int timeout/* Timeout time (tick)/no-wait/forever */

    10. )

The ISR(Interrupt service program) cannot invoke the semtake () operation!

[C-sharp] View plaincopy

    1. STATUS semgive

    2. (

    3. SEM_ID semid/* semaphore ID required to be released */

    4. )

[C-sharp] View plaincopy

    1. Semflush ()

Application direction:

1. Mutex operation : refers to different tasks can use the semaphore to access the critical resources mutually exclusive. This mutex is more accessible than interrupt (interrupt disable) with priority locking

(preemptive locks) two mutually exclusive ways with more granular granularity.

The initial state is set to (Sem_full) when a mutex operation is available. Semtake (), semgive () are called in the same task in pairs, sequentially.

2. Synchronous Operation : A task can use semaphores to control its own execution progress and synchronize itself to a set of external events. The initial state set to (Sem_empty) is not available when synchronizing operations. Semtake (), semgive () are called separately in different tasks.

Four Mutual exclusion semaphores (mutex semaphore)

The mutex is a special binary semaphore, which is designed for some problems that exist when using binary semaphore for mutex operation. The mutex semaphore mainly increases the processing of priority inversion, deletion security, and recursive access.

State diagram:

Related functions:

[C-sharp] View plaincopy

    1. sem_id semmcreate

    2. (

    3. int Options/* Mutex semaphore options */

    4. )

Difference:

1. The mutex semaphore can only be used for mutex operations.

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

3. The Interrupt Service Program (ISR) can not release (Semgive ()) the mutex semaphore.

4. The mutex semaphore does not support the Semflush () operation.

Application direction:

1. Avoid priority inversion (inversion):

In, Task2 waits for Task1 resources, and then in Pend state, when a medium-priority task comes in and grabs the TASK1 CPU, at which point the low-priority task executes before the high-priority task2. This phenomenon is the first level inversion.

Use Semid = semmcreate (sem_q_priority | Sem_inversion_safe); You can avoid inversion.

At this point, the TASK1 has the same priority as TASK2, until the Task2 execution is complete.

Sem_inversion_safe cannot be paired with Sem_q_fifo !

2.Deletion Safety (Safe to remove)

Use:Semid = semmcreate (Sem_q_fifo | Sem_delete_safe); Safe removal is possible.

The essence of this is that the Tasksafe () operation is implicitly performed before the task performs a semtake () operation on the mutex and succeeds in occupying the semaphore, and the Taskunsafe () operation is implicitly performed after the semgive () operation is performed.

If a task Task1 attempts to delete a task that has already been protected, Task2,task1 will be blocked until Task2 is de-secured (releasing the mutex with delete protection) to complete the deletion.

3. Recursive access

[C-sharp] View plaincopy

    1. Initfun ()

    2. {

    3. sem_id = Semmcreate (...);

    4. }

    5. Funb ()

    6. {

    7. Semtake (sem_id, sem_forever);

    8. /* Access critical resources */

    9. Semgive (sem_id);

    10. }

    11. Funa ()

    12. {

    13. Semtake (sem_id, sem_forever);

    14. /* Access critical resources */

    15. Funb (); Recursive access without deadlock

    16. Semgive (sem_id);

    17. }

Five. Counting semaphores(count semaphore)

Both the count Semaphore and the binary semaphore can be used for synchronization and mutual exclusion between tasks. The difference is that the count semaphore can record the number of times a semaphore is released and can be used to monitor the usage of a resource.

State diagram:

Related functions:

[C-sharp] View plaincopy

    1. sem_id semccreate

    2. (

    3. int options,/* semaphore option Modes */

    4. int initialcount/* Initial count */

    5. )

VxWorks interprocess communication 2--signal volume

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.