VxWorks Real-Time Task Management Guide
VxWorks Real-Time Task Management Guide
Basic OS
2.1 Introduction
Modern real-time systems are based on the complementary concepts of
multitasking and intertask communications. A multitasking environment allows a
real-time application to be constructed as a set of independent tasks, each with its
own thread of execution and set of system resources. The intertask communication
facilities allow these tasks to synchronize and communicate in order to coordinate
their activity. In VxWorks, the intertask communication facilities range from fast
semaphores to message queues and from pipes to network-transparent sockets.
Another key facility in real-time systems is hardware interrupt handling, because
interrupts are the usual mechanism to inform a system of external events. To get
the fastest possible response to interrupts, interrupt service routines (ISRs) in
VxWorks run in a special context of their own, outside any task’s context.
This chapter discusses the tasking facilities, intertask communication, and the
interrupt handling facilities that are at the heart of the VxWorks run-time
environment. You can also use POSIX real-time extensions with VxWorks. For
more information, see 3. POSIX Standard Interfaces.
7
VxWorks 5.5
Programmer’s Guide
NOTE: The POSIX standard includes the concept of a thread, which is similar to a
task, but with some additional features. For details, see 3.4 POSIX Threads, p.75.
2.2.1 Multitasking
8
2
Basic OS
The kernel maintains the current state of each task in the system. A task changes 2
from one state to another as a result of kernel function calls made by the
application. When created, tasks enter the suspended state. Activation is necessary
for a created task to enter the ready state. The activation phase is extremely fast,
enabling applications to pre-create tasks and activate them in a timely manner. An
alternative is the spawning primitive, which allows a task to be created and
activated with a single function. Tasks can be deleted from any state.
READY The state of a task that is not waiting for any resource other than the CPU.
PEND The state of a task that is blocked due to the unavailability of some
resource.
SUSPEND The state of a task that is unavailable for execution. This state is used
primarily for debugging. Suspension does not inhibit state transition,
only task execution. Thus, pended-suspended tasks can still unblock and
delayed-suspended tasks can still awaken.
PEND + S + T The state of a task that is both pended with a timeout value and
suspended.
Table 2-1 describes the state symbols that you see when working with Tornado
development tools. Figure 2-1 shows the corresponding state diagram of the wind
kernel states.
9
VxWorks 5.5
Programmer’s Guide
suspended
taskInit( )
10
2
Basic OS
Call Description 2
kernelTimeSlice( ) Controls round-robin scheduling.
A preemptive priority-based scheduler preempts the CPU when a task has a higher
priority than the current task running. Thus, the kernel ensures that the CPU is
always allocated to the highest priority task that is ready to run. This means that if
a task– with a higher priority than that of the current task– becomes ready to run,
the kernel immediately saves the current task’s context, and switches to the context
of the higher priority task. For example, in Figure 2-2, task t1 is preempted by
higher-priority task t2, which in turn is preempted by t3. When t3 completes, t2
continues executing. When t2 completes execution, t1 continues executing.
HIGH t3
priority
t2 t2
LOW t1 t1
time
11
VxWorks 5.5
Programmer’s Guide
Round-Robin Scheduling
A round-robin scheduling algorithm attempts to share the CPU fairly among all
ready tasks of the same priority. Round-robin scheduling uses time slicing to achieve
fair allocation of the CPU to all tasks with the same priority. Each task, in a group
of tasks with the same priority, executes for a defined interval or time slice.
Round-robin scheduling is enabled by calling kernelTimeSlice( ), which takes a
parameter for a time slice, or interval. This interval is the amount of time each task
is allowed to run before relinquishing the processor to another equal-priority task.
Thus, the tasks rotate, each executing for an equal interval of time. No task gets a
second slice of time before all other tasks in the priority group have been allowed
to run.
In most systems, it is not necessary to enable round-robin scheduling, the
exception being when multiple copies of the same code are to be run, such as in a
user interface task.
If round-robin scheduling is enabled, and preemption is enabled for the executing
task, the system tick handler increments the task’s time-slice count. When the
specified time-slice interval is completed, the system tick handler clears the
counter and the task is placed at the tail of the list of tasks at its priority level. New
tasks joining a given priority group are placed at the tail of the group with their
run-time counter initialized to zero.
Enabling round-robin scheduling does not affect the performance of task context
switches, nor is additional memory allocated.
If a task blocks or is preempted by a higher priority task during its interval, its
time-slice count is saved and then restored when the task becomes eligible for
execution. In the case of preemption, the task will resume execution once the
higher priority task completes, assuming that no other task of a higher priority is
ready to run. In the case where the task blocks, it is placed at the tail of the list of
tasks at its priority level. If preemption is disabled during round-robin scheduling,
the time-slice count of the executing task is not incremented.
Time-slice counts are accrued by the task that is executing when a system tick
occurs, regardless of whether or not the task has executed for the entire tick
interval. Due to preemption by higher priority tasks or ISRs stealing CPU time
from the task, it is possible for a task to effectively execute for either more or less
total CPU time than its allotted time slice.
Figure 2-3 shows round-robin scheduling for three tasks of the same priority: t1, t2,
and t3. Task t2 is preempted by a higher priority task t4 but resumes at the count
where it left off when t4 is finished.
12
2
Basic OS
HIGH t4
time slice
priority
LOW t1 t2 t3 t1 t2 t2 t3
time
Preemption Locks
The wind scheduler can be explicitly disabled and enabled on a per-task basis with
the routines taskLock( ) and taskUnlock( ). When a task disables the scheduler by
calling taskLock( ), no priority-based preemption can take place while that task is
running.
However, if the task explicitly blocks or suspends, the scheduler selects the next
highest-priority eligible task to execute. When the preemption-locked task
unblocks and begins running again, preemption is again disabled.
Note that preemption locks prevent task context switching, but do not lock out
interrupt handling.
Preemption locks can be used to achieve mutual exclusion; however, keep the
duration of preemption locking to a minimum. For more information, see
2.3.2 Mutual Exclusion, p.33.
When using taskLock( ), consider that it will not achieve mutual exclusion.
Generally, if interrupted by hardware, the system will eventually return to your
task. However, if you block, you lose task lockout. Thus, before you return from
the routine, taskUnlock( ) should be called.
13
VxWorks 5.5
Programmer’s Guide
When a task is accessing a variable or data structure that is also accessed by an ISR,
you can use intLock( ) to achieve mutual exclusion. Using intLock( ) makes the
operation “atomic” in a single processor environment. It is best if the operation is
kept minimal, meaning a few lines of code and no function calls. If the call is too
long, it can directly impact interrupt latency and cause the system to become far
less deterministic.
All application tasks should be priority 100 - 250. However, driver “support” tasks
(tasks associated with an ISR) can be in the range of 51-99. These tasks are crucial;
for example, if a support task fails while copying data from a chip, the device loses
that data.1 The system netTask( ) is at priority 50, so user tasks should not be
assigned priorities below that task; if they are, the network connection could die
and prevent debugging capabilities with Tornado.
The following sections give an overview of the basic VxWorks task routines, which
are found in the VxWorks library taskLib. These routines provide the means for
task creation and control, as well as for retrieving information about tasks. See the
VxWorks API Reference entry for taskLib for further information.
For interactive use, you can control VxWorks tasks from the host or target shell; see
the Tornado User’s Guide: Shell and 6. Target Tools in this manual.
The taskSpawn( ) routine creates the new task context, which includes allocating
the stack and setting up the task environment to call the main routine (an ordinary
14
2
Basic OS
subroutine) with the specified arguments. The new task begins execution at the
entry to the specified routine.
2
Table 2-3 Task Creation Routines
Call Description
Task Stack
It is hard to know exactly how much stack space to allocate, without reverse-
engineering the system configuration. To help avoid a stack overflow, and task
stack corruption, you can take the following approach. When initially allocating
the stack, make it much larger than anticipated; for example, from 20KB to up to
100KB, depending upon the type of application. Then, periodically monitor the
stack with checkStack( ), and if it is safe to make them smaller, modify the size.
When a task is spawned, you can specify an ASCII string of any length to be the
task name. VxWorks returns a task ID, which is a 4-byte handle to the task’s data
structures. Most VxWorks task routines take a task ID as the argument specifying
a task. VxWorks uses a convention that a task ID of 0 (zero) always implies the
calling task.
VxWorks does not require that task names be unique, but it is recommended that
unique names be used in order to avoid confusing the user. Furthermore, to use the
Tornado development tools to their best advantage, task names should not conflict
with globally visible routine or variable names. To avoid name conflicts, VxWorks
15
VxWorks 5.5
Programmer’s Guide
uses a convention of prefixing all task names started from the target with the
character t and task names started from the host with the character u.
You may not want to name some or all of your application’s tasks. If a NULL
pointer is supplied for the name argument of taskSpawn( ), then VxWorks assigns
a unique name. The name is of the form tN, where N is a decimal integer that is
incremented by one for each unnamed task that is spawned.
The taskLib routines listed in Table 2-4 manage task IDs and names.
Call Description
Task Options
When a task is spawned, you can pass in one or more option parameters, which are
listed in Table 2-5. The result is determined by performing a logical OR operation
on the specified options.
16
2
Basic OS
You must include the VX_FP_TASK option when creating a task that:
■ Performs floating-point operations. 2
■ Calls any function that returns a floating-point value.
■ Calls any function that takes a floating-point value as an argument.
For example:
tid = taskSpawn ("tMyTask", 90, VX_FP_TASK, 20000, myFunc, 2387, 0, 0,
0, 0, 0, 0, 0, 0, 0);
After a task is spawned, you can examine or alter task options by using the
routines listed in Table 2-6. Currently, only the VX_UNBREAKABLE option can be
altered.
Call Description
Task Information
The routines listed in Table 2-7 get information about a task by taking a snapshot
of a task’s context when the routine is called. Because the task state is dynamic, the
information may not be current unless the task is known to be dormant (that is,
suspended).
Call Description
taskRegsGet( ) Examines a task’s registers (cannot be used with the current task).
17
VxWorks 5.5
Programmer’s Guide
Call Description
taskRegsSet( ) Sets a task’s registers (cannot be used with the current task).
Tasks can be dynamically deleted from the system. VxWorks includes the routines
listed in Table 2-8 to delete tasks and to protect tasks from unexpected deletion.
Call Description
taskUnsafe( ) Undoes a taskSafe( ) (makes the calling task available for deletion).
* Memory that is allocated by the task during its execution is not freed when the task
is terminated.
! WARNING: Make sure that tasks are not deleted at inappropriate times. Before an
application deletes a task, the task should release all shared resources that it holds.
Tasks implicitly call exit( ) if the entry routine specified during task creation
returns. A task can kill another task or itself by calling taskDelete( ).
When a task is deleted, no other task is notified of this deletion. The routines
taskSafe( ) and taskUnsafe( ) address problems that stem from unexpected
deletion of tasks. The routine taskSafe( ) protects a task from deletion by other
tasks. This protection is often needed when a task executes in a critical region or
engages a critical resource.
18
2
Basic OS
NOTE: You can use VxWorks events to send an event when a task finishes
executing. For more information, see 2.4 VxWorks Events, p.57. 2
For example, a task might take a semaphore for exclusive access to some data
structure. While executing inside the critical region, the task might be deleted by
another task. Because the task is unable to complete the critical region, the data
structure might be left in a corrupt or inconsistent state. Furthermore, because the
semaphore can never be released by the task, the critical resource is now
unavailable for use by any other task and is essentially frozen.
Using taskSafe( ) to protect the task that took the semaphore prevents such an
outcome. Any task that tries to delete a task protected with taskSafe( ) is blocked.
When finished with its critical resource, the protected task can make itself available
for deletion by calling taskUnsafe( ), which readies any deleting task. To support
nested deletion-safe regions, a count is kept of the number of times taskSafe( ) and
taskUnsafe( ) are called. Deletion is allowed only when the count is zero, that is,
there are as many “unsafes” as “safes.” Only the calling task is protected. A task
cannot make another task safe or unsafe from deletion.
The following code fragment shows how to use taskSafe( ) and taskUnsafe( ) to
protect a critical region of code:
taskSafe ();
semTake (semId, WAIT_FOREVER); /* Block until semaphore available */
.
. /* critical region code */
.
semGive (semId); /* Release semaphore */
taskUnsafe ();
Deletion safety is often coupled closely with mutual exclusion, as in this example.
For convenience and efficiency, a special kind of semaphore, the mutual-exclusion
semaphore, offers an option for deletion safety. For more information, see Mutual-
Exclusion Semaphores, p.40.
Task Control
The routines listed in Table 2-9 provide direct control over a task’s execution.
VxWorks debugging facilities require routines for suspending and resuming a
task. They are used to freeze a task’s state for examination.
19
VxWorks 5.5
Programmer’s Guide
Call Description
The routine sysClkRateGet( ) returns the speed of the system clock in ticks per
second. Instead of taskDelay( ), you can use the POSIX routine nanosleep( ) to
specify a delay directly in time units. Only the units are different; the resolution of
both delay routines is the same, and depends on the system clock. For details, see
3.2 POSIX Clocks and Timers, p.73.
As a side effect, taskDelay( ) moves the calling task to the end of the ready queue
for tasks of the same priority. In particular, you can yield the CPU to any other
tasks of the same priority by “delaying” for zero clock ticks:
taskDelay (NO_WAIT); /* allow other tasks of same priority to run */
System clock resolution is typically 60Hz (60 times per second). This is a relatively
long time for one clock tick, and would be even at 100Hz or 120Hz. Thus, since
periodic delaying is effectively polling, you may want to consider using event-
driven techniques as an alternative.
20
2
Basic OS
Call Description
21
VxWorks 5.5
Programmer’s Guide
User-installed switch hooks are called within the kernel context and therefore do
not have access to all VxWorks facilities. Table 2-11 summarizes the routines that
can be called from a task switch hook; in general, any routine that does not involve
the kernel can be called.
Library Routines
vxLib vxTas( )
NOTE: For information about POSIX extensions, see 3. POSIX Standard Interfaces.
22
2
Basic OS
__errno( )that returns the address of the global variable, errno (as you might
guess, this is the single function that does not itself use the macro definition for
errno). This subterfuge yields a useful feature: because __errno( )is a function, you 2
can place breakpoints on it while debugging, to determine where a particular error
occurs.
Nevertheless, because the result of the macro errno is the address of the global
variable errno, C programs can set the value of errno in the standard way:
errno = someErrorNumber;
As with any other errno implementation, take care not to have a local variable of
the same name.
In VxWorks, the underlying global errno is a single predefined global variable that
can be referenced directly by application code that is linked with VxWorks (either
statically on the host or dynamically at load time). However, for errno to be useful
in the multitasking environment of VxWorks, each task must see its own version
of errno. Therefore errno is saved and restored by the kernel as part of each task’s
context every time a context switch occurs. Similarly, interrupt service routines (ISRs)
see their own versions of errno.
This is accomplished by saving and restoring errno on the interrupt stack as part
of the interrupt enter and exit code provided automatically by the kernel (see
2.6.1 Connecting Routines to Interrupts, p.66). Thus, regardless of the VxWorks
context, an error code can be stored or consulted with direct manipulation of the
global variable errno.
Almost all VxWorks functions follow a convention that indicates simple success or
failure of their operation by the actual return value of the function. Many functions
return only the status values OK (0) or ERROR (-1). Some functions that normally
return a nonnegative number (for example, open( ) returns a file descriptor) also
return ERROR to indicate an error. Functions that return a pointer usually return
NULL (0) to indicate an error. In most cases, a function returning such an error
indication also sets errno to the specific error code.
The global variable errno is never cleared by VxWorks routines. Thus, its value
always indicates the last error status set. When a VxWorks subroutine gets an error
23
VxWorks 5.5
Programmer’s Guide
indication from a call to another routine, it usually returns its own error indication
without modifying errno. Thus, the value of errno that is set in the lower-level
routine remains available as the indication of error type.
For example, the VxWorks routine intConnect( ), which connects a user routine to
a hardware interrupt, allocates memory by calling malloc( ) and builds the
interrupt driver in this allocated memory. If malloc( ) fails because insufficient
memory remains in the pool, it sets errno to a code indicating an insufficient-
memory error was encountered in the memory allocation library, memLib. The
malloc( ) routine then returns NULL to indicate the failure. The intConnect( )
routine, receiving the NULL from malloc( ), then returns its own error indication of
ERROR. However, it does not alter errno leaving it at the “insufficient memory”
code set by malloc( ). For example:
if ((pNew = malloc (CHUNK_SIZE)) = = NULL)
return (ERROR);
It is recommended that you use this mechanism in your own subroutines, setting
and examining errno as a debugging technique. A string constant associated with
errno can be displayed using printErrno( ) if the errno value has a corresponding
string entered in the error-status symbol table, statSymTbl. See the reference entry
errnoLib for details on error-status values and building statSymTbl.
VxWorks errno values encode the module that issues the error, in the most
significant two bytes, and uses the least significant two bytes for individual error
numbers. All VxWorks module numbers are in the range 1–500; errno values with
a “module” number of zero are used for source compatibility.
All other errno values (that is, positive values greater than or equal to 501<<16,
and all negative values) are available for application use.
See the reference entry on errnoLib for more information about defining and
decoding errno values with this convention.
Errors in program code or data can cause hardware exception conditions such as
illegal instructions, bus or address errors, divide by zero, and so forth. The
VxWorks exception handling package takes care of all such exceptions. The default
exception handler suspends the task that caused the exception, and saves the state
24
2
Basic OS
of the task at the point of the exception. The kernel and other tasks continue
uninterrupted. A description of the exception is transmitted to the Tornado
development tools, which can be used to examine the suspended task; see the 2
Tornado User’s Guide: Shell for details.
Tasks can also attach their own handlers for certain hardware exceptions through
the signal facility. If a task has supplied a signal handler for an exception, the
default exception handling described above is not performed. A user-defined
signal handler is useful for recovering from catastrophic events. Typically,
setjmp( ) is called to define the point in the program where control will be restored,
and longjmp( ) is called in the signal handler to restore that context. Note that
longjmp( ) restores the state of the task’s signal mask.
Signals are also used for signaling software exceptions as well as hardware
exceptions. They are described in more detail in 2.3.7 Signals, p.55 and in the
reference entry for sigLib.
25
VxWorks 5.5
Programmer’s Guide
taskOne (void)
{
...
myFunc();
...
}
myFunc (void)
{
...
}
taskTwo (void)
{
myFunc();
...
...
}
This may or may not be desirable, depending on the nature of the application. For
example, a packet driver can mix streams from different tasks because the packet
header identifies the destination of each packet.
The majority of VxWorks routines use the following reentrancy techniques:
– dynamic stack variables
– global and static variables guarded by semaphores
– task variables
We recommend applying these same techniques when writing application code
that can be called from several task contexts simultaneously.
NOTE: In some cases reentrant code is not preferable. A critical section should use
a binary semaphore to guard it, or use intLock( ) or intUnlock( ) if called from by
an ISR.
NOTE: Init( ) functions should be callable multiple times, even if logically they
should only be called once. As a rule, functions should avoid static variables that
keep state information. Init( ) functions are one exception, where using a static
variable that returns the success or failure of the original Init( ) is appropriate.
26
2
Basic OS
Many subroutines are pure code, having no data of their own except dynamic stack 2
variables. They work exclusively on data provided by the caller as parameters. The
linked-list library, lstLib, is a good example of this. Its routines operate on lists and
nodes provided by the caller in each subroutine call.
Subroutines of this kind are inherently reentrant. Multiple tasks can use such
routines simultaneously, without interfering with each other, because each task
does indeed have its own stack. See Figure 2-5.
taskOne ( ) ...
{ var = 1
... ...
comFunc(1);
...
}
taskTwo ( ) ...
{ comFunc (arg)
var = 2 {
... ...
comFunc(2); int var = arg;
... }
}
Some libraries encapsulate access to common data. This kind of library requires
some caution because the routines are not inherently reentrant. Multiple tasks
simultaneously invoking the routines in the library might interfere with access to
common variables. Such libraries must be made explicitly reentrant by providing
a mutual-exclusion mechanism to prohibit tasks from simultaneously executing
critical sections of code. The usual mutual-exclusion mechanism is the mutex
semaphore facility provided by semMLib and described in Mutual-Exclusion
Semaphores, p.40.
27
VxWorks 5.5
Programmer’s Guide
Task Variables
Some routines that can be called by multiple tasks simultaneously may require
global or static variables with a distinct value for each calling task. For example,
several tasks may reference a private buffer of memory and yet refer to it with the
same global variable.
To accommodate this, VxWorks provides a facility called task variables that allows
4-byte variables to be added to a task’s context, so that the value of such a variable
is switched every time a task switch occurs to or from its owner task. Typically,
several tasks declare the same variable (4-byte memory location) as a task variable.
Each of those tasks can then treat that single memory location as its own private
variable; see Figure 2-6. This facility is provided by the routines taskVarAdd( ),
taskVarDelete( ), taskVarSet( ), and taskVarGet( ), which are described in the
reference entry for taskVarLib.
current value of
globDat
Use this mechanism sparingly. Each task variable adds a few microseconds to the
context switching time for its task, because the value of the variable must be saved
and restored as part of the task’s context. Consider collecting all of a module’s task
variables into a single dynamically allocated structure, and then making all
accesses to that structure indirectly through a single pointer. This pointer can then
be the task variable for all tasks using that module.
28
2
Basic OS
With VxWorks, it is possible to spawn several tasks with the same main routine. 2
Each spawn creates a new task with its own stack and context. Each spawn can also
pass the main routine different parameters to the new task. In this case, the same
rules of reentrancy described in Task Variables, p.28 apply to the entire task.
This is useful when the same function needs to be performed concurrently with
different sets of parameters. For example, a routine that monitors a particular kind
of equipment might be spawned several times to monitor several different pieces
of that equipment. The arguments to the main routine could indicate which
particular piece of equipment the task is to monitor.
In Figure 2-7, multiple joints of the mechanical arm use the same code. The tasks
manipulating the joints invoke joint( ). The joint number (jointNum) is used to
indicate which joint on the arm to manipulate.
joint_2
joint_3
joint_1
joint
(
int jointNum
)
{
/* joint code here */
}
29
VxWorks 5.5
Programmer’s Guide
The root task is the first task executed by the kernel. The entry point of the root task
is usrRoot( )in installDir/target/config/all/usrConfig.c and initializes most
VxWorks facilities. It spawns such tasks as the logging task, the exception task, the
network task, and the tRlogind daemon. Normally, the root task terminates and is
deleted after all initialization has occurred.
The log task, tLogTask, is used by VxWorks modules to log system messages
without having to perform I/O in the current task context. For more information,
see 4.5.3 Message Logging, p.122 and the reference entry for logLib.
The exception task, tExcTask, supports the VxWorks exception handling package
by performing functions that cannot occur at interrupt level. It is also used for
actions that cannot be performed in the current task’s context, such as task suicide.
It must have the highest priority in the system. Do not suspend, delete, or change
the priority of this task. For more information, see the reference entry for excLib.
The tNetTask daemon handles the task-level functions required by the VxWorks
network. Configure VxWorks with the INCLUDE_NET_LIB component to spawn
the tNetTask task.
The target agent task, tWdbTask, is created if the target agent is set to run in task
mode. It services requests from the Tornado target server; for information about
this server, see the Tornado User’s Guide: Overview. Configure VxWorks with the
INCLUDE_WDB component to include the target agent.
The following VxWorks system tasks are created if their associated configuration
constants are defined; for more information, see the Tornado User’s Guide:
Configuration and Build.
30
2
Basic OS
tShell
If you have included the target shell in the VxWorks configuration, it is
spawned as this task. Any routine or task that is invoked from the target shell, 2
rather than spawned, runs in the tShell context. For more information, see
6. Target Tools. Configure VxWorks with the INCLUDE_SHELL component to
include the target shell.
tRlogind
If you have included the target shell and the rlogin facility in the VxWorks
configuration, this daemon allows remote users to log in to VxWorks. It
accepts a remote login request from another VxWorks or host system and
spawns tRlogInTask and tRlogOutTask. These tasks exist as long as the
remote user is logged on. During the remote session, the shell’s (and any other
task’s) input and output are redirected to the remote user. A tty-like interface
is provided to the remote user through the use of the VxWorks pseudo-
terminal driver, ptyDrv. For more information, see 4.7.1 Serial I/O Devices
(Terminal and Pseudo-Terminal Devices), p.132 and the reference entry for
ptyDrv. Configure VxWorks with the INCLUDE_RLOGIN component to
include the rlogin facility.
tTelnetd
If you have included the target shell and the telnet facility in the VxWorks
configuration, this daemon allows remote users to log in to VxWorks with
telnet. It accepts a remote login request from another VxWorks or host system
and spawns the input task tTelnetInTask and output task tTelnetOutTask.
These tasks exist as long as the remote user is logged on. During the remote
session, the shell’s (and any other task’s) input and output are redirected to the
remote user. A tty-like interface is provided to the remote user through the use
of the VxWorks pseudo-terminal driver, ptyDrv. See 4.7.1 Serial I/O Devices
(Terminal and Pseudo-Terminal Devices), p.132 and the reference entry for
ptyDrv for further explanation. Configure VxWorks with the
INCLUDE_TELNET component to include the telnet facility.
tPortmapd
If you have included the RPC facility in the VxWorks configuration, this
daemon is an RPC server that acts as a central registrar for RPC servers
running on the same machine. RPC clients query the tPortmapd daemon to
find out how to contact the various servers. Configure VxWorks with the
INCLUDE_RPC component to include the portmap facility.
31
VxWorks 5.5
Programmer’s Guide
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 Figure 2-8. Global variables,
linear buffers, ring buffers, linked lists, and pointers can be referenced directly by
code running in different contexts.
TASKS MEMORY
access
task 1 sharedData
sharedData
access
task 2 sharedData
access
task 3 sharedData
32
2
Basic OS
The most powerful method available for mutual exclusion is the disabling of
interrupts. Such a lock guarantees exclusive access to the CPU:
funcA ()
{
int lock = intLock();
.
. /* critical region of code that cannot be interrupted */
.
intUnlock (lock);
}
! WARNING: Do not call VxWorks system routines with interrupts locked. Violating
this rule may re-enable interrupts unpredictably.
33
VxWorks 5.5
Programmer’s Guide
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
2.3.3 Semaphores, p.34.
! WARNING: The critical region code should not block. If it does, preemption could
be re-enabled.
2.3.3 Semaphores
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,
as described below:
■ For mutual exclusion semaphores interlock access to shared resources. They
provide mutual exclusion with finer granularity than either interrupt
disabling or preemptive locks, discussed in 2.3.2 Mutual Exclusion, p.33.
■ For synchronization semaphores coordinate a task’s execution with external
events.
There are three types of Wind semaphores, optimized to address different classes
of problems:
binary
The fastest, most general-purpose semaphore. Optimized for
synchronization or mutual exclusion.
34
2
Basic OS
mutual exclusion
A special binary semaphore optimized for problems inherent in mutual
exclusion: priority inheritance, deletion safety, and recursion. 2
counting
Like the binary semaphore, but keeps track of the number of times a
semaphore is given. Optimized for guarding multiple instances of a
resource.
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
3.6 POSIX Semaphores, p.85.
The semaphores described here are for use on a single CPU. The optional product
VxMP provides semaphores that can be used across processors; see 11. Shared-
Memory Objects.
Semaphore Control
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.
Table 2-12 lists the semaphore control routines.
Call Description
35
VxWorks 5.5
Programmer’s Guide
Binary Semaphores
36
2
Basic OS
2
task is
no no pended for
semaphore timeout = timeout
available? NO_WAIT
value
yes yes
no no
task continues,
semaphore tasks
semaphore
available? pended?
made available
yes yes
Mutual Exclusion
SEM_ID semMutex;
37
VxWorks 5.5
Programmer’s Guide
When a task wants to access the resource, it must first take that semaphore. As long
as the task keeps the semaphore, all other tasks seeking access to the resource are
blocked from execution. When the task is finished with the resource, it gives back
the semaphore, allowing another task to use the resource.
Thus, all accesses to a resource requiring mutual exclusion are bracketed with
semTake( ) and semGive( ) pairs:
semTake (semMutex, WAIT_FOREVER);
.
. /* critical region, only accessible by a single task at a time */
.
semGive (semMutex);
Synchronization
38
2
Basic OS
init (
int someIntNum
)
{
/* connect interrupt service routine */
intConnect (INUM_TO_IVEC (someIntNum), eventInterruptSvcRout, 0);
/* create semaphore */
syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);
task1 (void)
{
...
semTake (syncSem, WAIT_FOREVER); /* wait for event to occur */
printf ("task 1 got the semaphore\n");
... /* process event */
}
eventInterruptSvcRout (void)
{
...
semGive (syncSem); /* let task 1 process event */
...
}
Broadcast synchronization allows all processes that are blocked on the same
semaphore to be unblocked atomically. Correct application behavior often requires
a set of tasks to process an event before any task of the set has the opportunity to
process further events. The routine semFlush( ) addresses this class of
synchronization problem by unblocking all tasks pended on a semaphore.
39
VxWorks 5.5
Programmer’s Guide
Mutual-Exclusion Semaphores
Priority Inversion
HIGH t1 t1
priority
t2
LOW t3 t3 t3
time
40