VxWorks semaphore analysis (reposted)

xiaoxiao2021-03-19  186

There are three types of binary semaphors, count semaphors, and mutual exclusive quantities in the Wind kernel. In order to use programs, it also provides a POSIX (portable operating system interface) semaphore. In VxWorks, the signal is the primary means of implementing task synchronization, but also the best choice to resolve task synchronization.

Realization of mutual exclusion:

It is easy to implement mutual exclusion with a binary selection, and mutual exclusion means that multitasking is exclusive when accessing critical resources. In order to enable multiple tasks to mutually exclusive access to critical resources, just set up a semaphore for this resource, which is equivalent to a token, that task gets the token, which is entitled to use this resource. Set the quantity to be available, then place the critical code of the task of the required resources to be between semtake () and semgive ().

Note: 1. The relationship between the signal and task priority of the mutual exclusion: The scheduling of the task is made according to the task priority, but only one task is available when using critical resources, that is, according to the task priority Get the amount of semaphore to access resources. It is only the task release of the current use of the resource to release the semgive (), and other tasks obtain semapcosts in accordance with priority.

2. The parameters in the signal amount attribute are: SEM_Q_PRIORITY. Moreover, the amount of signal must be set to SEM_FULL when creating the semaphore. That is, the signal is available.

Basic realization model:

SEM_ID SEMMUTEX;

Semmutex = SEMBCREATE (SEM_Q_PRIORITY, SEM_FULL);

Task (Void)

{

Semtake (Semmutex, Wait_ForeVer); // Get semaphore, that is, to obtain a token

// Cross-border, one moment can only be accessed by a task

Semgive (SEMMUTEX);

}

Implementation of mission synchronization

Synchronization, the task is executed in order, in order to achieve task A and B synchronization, just let tasks A and B share a semaphore, set the initial value empty, that is, unavailable, put semgive () after the task A And insert semtake before task B.

Description: 1 or discussions and priority relationships. Since the signal is initialized to be empty, it is not available, it may make priority reversal, that is, high priority task B is waiting for the low priority task A to release the signal. Only the semgent release statement semgive () after Semigive () is executed to execute.

2, the setting of the attribute parameters is SEM_Q_FIFO, SEM_EMPTY;

Implementation model reference

SEM_ID SEMSYNC;

SEMSYNC = SEMBCREATE (SEM_Q_FIFO, SEM_EMPTY);

Taska (Void)

{

......

Semgive (semsync); // Semicone release, effective

}

Taskb (void)

{

Semtake (Semsync, Wait_ForeVer); // Waiting for the quantity

....

}

Precautions:

1. Different uses, signal quantity attributes and initial values

2. When mutually exclusive access to resources, semtake () and semgive () must be paired, and the order cannot be reversed.

3. Avoid deleting the amount of semaphore that other tasks being requested.

application:

1, make sure the task priority does not reverse

SEM_ID SEMFS;

SEM_ID SEMFSS;

SEM_ID SEMFEX;

SEMFS = SEMBCREATE (SEM_Q_FIFO, SEM_EMPTY);

SEMFSS = SEMBCREATE (SEM_Q_FIFO, SEM_EMPTY);

SemFex = sembcreate (SEM_Q_FIFO, SEM_EMPTY);

Void T_Imaget (Void)

{

Printf ("a"); semgive (semfs); // release semaphore

}

Void t_imajud (void)

{

SEMTAKE (SEMFS, WAIT_FOREVER); / / Make sure the priority does not reverse

Printf ("jj");

Semgive (SEMFSS);

}

Void T_IMapro (Void)

{

SEMTAKE (SEMFSS, WAIT_FOREVER);

Printf ("rr");

Semgive (Semfex);

}

Void T_IMAEXC (Void)

{

SEMTAKE (Semfex, Wait_ForeVer);

Printf ("y");

}

Void Start (void)

{

Int TgetID, Tjudid, TProid, Texcid;

Tgetid = Taskspawn ("TPGET", 200, 0, 1000, (Funcptr) T_Imaget, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);

Tjudid = TasksPawn ("Tpjud", 201, 0, 1000, (Funcptr) t_imajud, 3,0,0,0,0,0,0,200,0,0);

TPROID = TasksPawn ("TPPRO", 202, 0, 1000, (Funcptr) T_Imapro, 3, 0, 0, 0, 0, 0, 0, 0, 0);

TEXCID = TasksPawn ("TPEXC", 203, 0, 1000, (Funcptr) T_IMAEXC, 3, 0, 0, 0, 0, 0, 0, 0, 0);

}

Although the above examples have set the priority of each task, plus the signal can be synchronized, and the appearance of priority reversal is prevented.

转载请注明原文地址:https://www.9cbs.com/read-130092.html

New Post(0)