"Turn" vxworks signal volume analysis

Source: Internet
Author: User

There are three types of binary semaphores, counting semaphores, and mutexes in the wind core, and the POSIX (Portable Operating system interface) Semaphore is also available in order to make the application portable. In VxWorks, the signal volume is the main means to achieve the task synchronization, but also the best choice to solve the task synchronization.

About mutex implementations:

It is easy to implement mutual exclusion using binary semaphores, which means that multiple tasks have exclusivity when accessing critical resources. In order for multiple tasks to be mutually exclusive access to the critical resource, you only need to set a semaphore for the resource, which is equivalent to a token, and that task has the right to use the resource when it gets the token. Set the semaphore to be available, then place the critical code for the task of the required resource between Semtake () and semgive ().

Indicate:
1. The relationship between the semaphore and the task priority in the mutex: task scheduling or task priority, but when using critical resources, only one task to obtain the semaphore, that is, the task priority to obtain the signal volume to access resources. Only the task that currently uses the resource releases the semaphore semgive (), and the other tasks receive the semaphore in priority order.

2, the parameters of the semaphore attribute are: sem_q_priority. And the semaphore must be set to full sem_full when creating the semaphore. The semaphore is available.

The basic implementation of the mutex model:

1 sem_id Semmutex;
2
3 Semmutex = Sembcreate (sem_q_priority, sem_full);
4
5 Task (void)
6 {
7
8 Semtake (Semmutex, wait_forever);//Get a semaphore, which is equivalent to getting a token of the resource being used
9
10//critical section, one task can only be accessed at a time
11
Semgive (Semmutex);
13
14}


About the implementation of task synchronization

Synchronization is the task in a certain sequence of execution, in order to achieve tasks A and B synchronization, only need to let tasks A and B share a semaphore, and set the initial value is empty, that is not available, semgive () placed after task A, and before task B insert Semtake ().

Description
1, the relationship between discussion and priority. Because the semaphore is initialized to NULL, it is not available, so it is possible to reverse the priority, that is, high-priority task B is waiting for low-priority task A to release the semaphore. Task B Gets the semaphore to execute only after the semaphore release statement semgive () is executed.

2, the attribute parameter is set to Sem_q_fifo,sem_empty;

Implementing Model Reference

1 sem_id Semsync;
2
3 Semsync = Sembcreate (Sem_q_fifo, sem_empty);
4
5 Taska (void)
6 {
7
8
9 semgive (Semsync); Signal volume release, effective
10}
TASKB (void)
12 {
13
Semtake (Semsync, wait_forever); Wait for the semaphore
15
16.
17}

Precautions for using semaphores:

1, the use of different, semaphore properties and initial values are different.

2, mutually exclusive access to resources, Semtake () and semgive () must appear in pairs, and the order cannot be reversed.

3. Avoid deleting the semaphore that other tasks are requesting.

Application:

1. Ensure task priority does not reverse

1 sem_id Semfs;
2 sem_id SEMFSS;
3 sem_id Semfex;
4
5 Semfs = Sembcreate (Sem_q_fifo, sem_empty);
6 SEMFSS = Sembcreate (Sem_q_fifo, sem_empty);
7 Semfex = Sembcreate (Sem_q_fifo, sem_empty);
8
9 void T_imaget (void)
10 {
printf ("a");
Semgive (SEMFS); Release semaphore
13}
14
void T_imajud (void)
16 {
Semtake (Semfs, wait_forever);//Ensure priority is not reversed
18
printf ("JJ");
Semgive (SEMFSS);
21}
22
T_imapro void (void)
24 {
Semtake (SEMFSS, wait_forever);
-printf ("RR");
Semgive (SEMFEX);
28}
29
void T_imaexc (void)
31 {
Semtake (Semfex, wait_forever);
printf ("Y");
34}
35
$ void Start (void)
37 {
Tgetid int, Tjudid, tproid, texcid;
Tgetid = Taskspawn ("Tpget", 0, 1, 0, 0, 0, 0, 0, 0, 0, 0);
Tjudid = Taskspawn ("Tpjud", 201,0,1000, (funcptr) t_imajud,3,0,0,0,0,0,0,0,0,0);
Tproid = Taskspawn ("Tppro", 202,0,1000, (funcptr) t_imapro,3,0,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,0);
43
44}

"Turn" vxworks signal volume analysis

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.