VxWorks semaphore Analysis

Source: Internet
Author: User

The wind kernel has three types of semaphores: Binary semaphores, Count semaphores, and mutex semaphores. In order to make the application portable, the POSIX semaphores are also provided. In VxWorks, semaphores are the main means for task synchronization and the best choice for task synchronization.

Mutual Exclusion implementation:

The use of binary semaphores can easily achieve mutual exclusion. mutex means that multiple tasks are exclusive when accessing critical resources. To allow multiple tasks to access critical resources mutex, you only need to set a semaphore for the resource, which is equivalent to a token. The task has the right to use the resource after obtaining the token. Set the semaphore to available, and then place the critical code of the required resource task between semtake () and semgive.

Note:
1. Relationship between semaphores in mutex and task priority: task scheduling is performed based on task priority, but only one task obtains semaphores when using critical resources, that is to say, the semaphore is obtained based on the task priority to access resources. Only tasks that are currently using resources release semaphores semgive (). Other tasks obtain semaphores based on their priorities.

2. sem_q_priority is a parameter in the semaphore attribute. When creating a semaphores, you must set the semaphores to full sem_full. That is, the semaphore is available.

Basic implementation 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); // obtain the semaphore, which is equivalent to obtaining the token for using the resource.
9
10 // In the critical section, only one task can be accessed at a time point.
11
12 semgive (semmutex );
13
14}

Implementation of task synchronization

Synchronization means that tasks are executed in a certain order. To synchronize tasks a and B, you only need to share a semaphore with tasks A and B and set the initial value to null, which is unavailable, place semgive () after task a, and insert semtake () before Task B.

Note:
1. The relationship between the discussion and the priority is also discussed. Because the semaphore Initialization is empty and unavailable, priority may be reversed, that is, high-priority task B is waiting for low-priority task a to release the semaphore. Only after the semaphores release statement semgive () is executed can Task B obtain the semaphores.

2. Set the attribute parameters to sem_q_fifo and sem_empty;

Implementation 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); // semaphore release, valid
10}
11 taskb (void)
12 {
13
14 semtake (semsync, wait_forever); // wait for the semaphore
15
16.
17}

Considerations for using semaphores:

1. the semaphore attributes and initial values are different for different purposes.

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

3. avoid deleting semaphores that are being requested by other tasks.

Application:

1. Ensure that the task priority is not reversed

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 {
11 printf ("");
12 semgive (semfs); // release the semaphore
13}
14
15 void t_imajud (void)
16 {
17 semtake (semfs, wait_forever); // ensure that the priority is not reversed
18
19 printf ("JJ ");
20 semgive (semfss );
21}
22
23 void t_imapro (void)
24 {
25 semtake (semfss, wait_forever );
26 printf ("RR ");
27 semgive (semfex );
28}
29
30 void t_imaexc (void)
31 {
32 semtake (semfex, wait_forever );
33 printf ("Y ");
34}
35
36 void start (void)
37 {
38 int tgetid, tjudid, tproid, texcid;
39 tgetid = taskspawn ("tpget", 200, 0, 1000, (funcptr) t_imaget, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
40 tjudid = taskspawn ("tpjud", ,,0, 1000, (funcptr) t_imajud );
41 tproid = taskspawn ("tppro", 202,0, 1000, (funcptr) t_imapro );
42 texcid = taskspawn ("tpexc", 203,0, 1000, (funcptr) t_imaexc );
43
44}

The above example sets the priority of each task, but the semaphore can be used for synchronization, and the priority inversion is prevented.

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.