📄 c-basic4.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title> Basic OS </title></head><body bgcolor="FFFFFF"><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-basic.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-basic3.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-basic5.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="95697">2.4 Intertask Communications</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="93666"> </a>The complement to the multitasking routines described in the <a href="c-basic3.html#83567"><i class="title">2.3 Tasks</i></a> is the intertask communication facilities. These facilities permit independent tasks to coordinate their actions.</p><dd><p class="Body"><a name="84910"> </a>VxWorks supplies a rich set of intertask communication mechanisms, including:</p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84911"> </a><i class="term">Shared memory</i>, for simple sharing of data.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84912"> </a><i class="term">Semaphores</i>, for basic mutual exclusion and synchronization.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84913"> </a><i class="term">Message queues</i> and <i class="term">pipes</i>, for intertask message passing within a CPU.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84914"> </a><i class="term">Sockets</i> and <i class="term">remote procedure calls</i>, for network-transparent intertask communication.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84915"> </a><i class="term">Signals</i>, for exception handling.</li></ul></p></dl><dl class="margin"><dd><p class="Body"><a name="84916"> </a>The optional product, VxMP, provides intertask communication over the backplane for tasks running on different CPUs. This includes shared semaphores, shared message queues, shared memory, and the shared name database.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84918">2.4.1 Shared Data Structures</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84919"> </a>The most obvious way for tasks to communicate is by accessing shared data structures. Because all tasks in VxWorks exist in a single linear address space, sharing data structures between tasks is trivial; see <a href="c-basic4.html#84955">Figure 2-8</a>. Global variables, linear buffers, ring buffers, linked lists, and pointers can be referenced directly by code running in different contexts.<div class="frame"><h4 class="EntityTitle"><a name="84955"><font face="Helvetica, sans-serif" size="-1" class="sans">Figure 2-8: Shared Data Structures</font></a></h4><dl class="margin"><div class="Anchor"><a name="84952"> </a><img class="figure" border="0" src="images/c-basica5.gif"></div></dl></div></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84958">2.4.2 Mutual Exclusion</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84959"> </a>While a shared address space simplifies exchange of data, interlocking access to memory is crucial to avoid contention. Many methods exist for obtaining exclusive access to resources, and vary only in the scope of the exclusion. Such methods include disabling interrupts, disabling preemption, and resource locking with semaphores.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84960">Interrupt Locks and Latency</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84961"> </a>The most powerful method available for mutual exclusion is the disabling of interrupts. Such a lock guarantees exclusive access to the CPU:</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="84963"> funcA () { int lock = intLock(); . . <i class="i">critical region that cannot be interrupted</i> . intUnlock (lock); }</a></b></pre></dl><dl class="margin"><dd><p class="Body"><a name="84971"> </a>While this solves problems involving mutual exclusion with ISRs, it is inappropriate as a general-purpose mutual-exclusion method for most real-time systems, because it prevents the system from responding to external events for the duration of these locks. Interrupt latency is unacceptable whenever an immediate response to an external event is required. However, interrupt locking can sometimes be necessary where mutual exclusion involves ISRs. In any situation, keep the duration of interrupt lockouts short.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="84974">Preemptive Locks and Latency</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84976"> </a>Disabling preemption offers a somewhat less restrictive form of mutual exclusion. While no other task is allowed to preempt the current executing task, ISRs are able to execute:</p></dl><dl class="margin"><dd><pre class="Code"><b><a name="84977"> funcA () { taskLock (); . . <i class="i">critical region that cannot be interrupted </i> . taskUnlock (); }</a></b></pre></dl><dl class="margin"><dd><p class="Body"><a name="84985"> </a>However, this method can lead to unacceptable real-time response. Tasks of higher priority are unable to execute until the locking task leaves the critical region, even though the higher-priority task is not itself involved with the critical region. While this kind of mutual exclusion is simple, if you use it, make sure to keep the duration short. A better mechanism is provided by semaphores, discussed in <a href="c-basic4.html#84992"><i class="title">2.4.3 Semaphores</i></a>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84992">2.4.3 Semaphores</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84993"> </a>VxWorks semaphores are highly optimized and provide the fastest intertask communication mechanism in VxWorks. Semaphores are the primary means for addressing the requirements of both mutual exclusion and task synchronization:</p></dl><dl class="margin"><p class="listspace"><ul class="Bullet" type="disc"><li><a name="84995"> </a>For <i class="term">mutual exclusion</i>, semaphores interlock access to shared resources. They provide mutual exclusion with finer granularity than either interrupt disabling or preemptive locks, discussed in <a href="c-basic4.html#84958"><i class="title">2.4.2 Mutual Exclusion</i></a>.</li></ul></p><p class="listspace"><ul class="Bullet" type="disc"><li><a name="90663"> </a>For <i class="term">synchronization</i>, semaphores coordinate a task's execution with external events.</li></ul></p></dl><dl class="margin"><dd><p class="Body"><a name="90665"> </a>There are three types of Wind semaphores, optimized to address different classes of problems:</p></dl><dl class="margin"><dd><div class="Item"><a name="90666"> </a><i class="term">binary</i> </div><dl class="margin"><dl class="margin"><dd><div class="Indent2"><a name="87755"> </a>The fastest, most general-purpose semaphore. Optimized for synchronization or mutual exclusion.</div><br></dl></dl><dd><div class="Item"><a name="85004"> </a><i class="term">mutual exclusion</i> </div><dl class="margin"><dl class="margin"><dd><div class="Indent2"><a name="87757"> </a>A special binary semaphore optimized for problems inherent in mutual exclusion: priority inheritance, deletion safety, and recursion.</div><br></dl></dl><dd><div class="Item"><a name="85005"> </a><i class="term">counting</i> </div><dl class="margin"><dl class="margin"><dd><div class="Indent2"><a name="87759"> </a>Like the binary semaphore, but keeps track of the number of times a semaphore is given. Optimized for guarding multiple instances of a resource.</div><br></dl></dl></dl><dl class="margin"><dd><p class="Body"><a name="85006"> </a>VxWorks provides not only the Wind semaphores, designed expressly for VxWorks, but also POSIX semaphores, designed for portability. An alternate semaphore library provides the POSIX-compatible semaphore interface; see <a href="c-basic4.html#85561"><i class="title">POSIX Semaphores</i></a>.</p><dd><p class="Body"><a name="85010"> </a>The semaphores described here are for use on a single CPU. The optional product VxMP provides semaphores that can be used across processors; see <a href="c-smo.html#84368"><i class="title">6. Shared-Memory Objects</i></a>.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="85015">Semaphore Control</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="85016"> </a>Instead of defining a full set of semaphore control routines for each type of semaphore, the Wind semaphores provide a single uniform interface for semaphore control. Only the creation routines are specific to the semaphore type. <a href="c-basic4.html#85023">Table 2-13</a> lists the semaphore control routines.<p class="table"><h4 class="EntityTitle"><a name="85023"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 2-13: Semaphore Control Routines</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="85027"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Call</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="85029"> </a><font face="Helvetica, sans-serif" size="-1" class="sans"> Description </font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85036"> </a><b class="routine"><i class="routine">semBCreate</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85038"> </a>Allocate and initialize a binary semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85041"> </a><b class="routine"><i class="routine">semMCreate</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85043"> </a>Allocate and initialize a mutual-exclusion semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="85046"> </a><b class="routine"><i class="routine">semCCreate</i></b><b>( )</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="85048"> </a>Allocate and initialize a counting semaphore. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -