Understanding Basic RTOS Functions in VxWorks: A Practical Guide for Engineers
VxWorks, developed by Wind River, is a widely adopted real-time operating system (RTOS) in mission-critical embedded systems. This article walks through the foundational functions of VxWorks, including task control, inter-process communication (IPC), signals, and virtual devices, offering practical code examples throughout.
Why VxWorks? #
VxWorks provides a Unix-like multitasking environment with:
- Hard real-time performance
- Modular and scalable architecture
- POSIX compliance
- SMP/AMP support
- Rich networking and file system APIs
- Support for modern processor families (ARM, Intel, MIPS, etc.)
It uses a host-target model: development happens on a host (e.g., Linux/Windows), with deployment to an embedded target.
System Initialization and Configuration #
Typical startup involves setting system clock, initializing device drivers, and spawning initial tasks.
#include <vxWorks.h>
#include <sysLib.h>
#include <taskLib.h>
void sysInit()
{
sysClkRateSet(100); // Set system tick rate to 100 Hz
}
Task Management #
VxWorks tasks are lightweight threads with distinct states: ready, running, suspended, etc.
Create and Start a Task #
void myWorker()
{
while (1)
{
printf("Task running\n");
taskDelay(100); // 1 second if tick rate is 100 Hz
}
}
void startTask()
{
int tid = taskSpawn("tWorker", 100, 0, 8192, (FUNCPTR)myWorker, 0,0,0,0,0,0,0,0,0,0);
if (tid == ERROR)
perror("taskSpawn failed");
}
Suspend and Resume a Task #
void pauseTask(int tid)
{
taskSuspend(tid);
}
void resumeTask(int tid)
{
taskResume(tid);
}
Inter-Process Communication (IPC) #
Semaphores #
Semaphores are used for mutual exclusion and synchronization.
Binary Semaphore Example #
#include <semLib.h>
SEM_ID sem;
void initSem()
{
sem = semBCreate(SEM_Q_PRIORITY, SEM_EMPTY);
}
void taskUsingResource()
{
semTake(sem, WAIT_FOREVER);
// critical section
semGive(sem);
}
Mutex (Priority Inheritance) #
SEM_ID mutex = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE);
Message Queues #
Queues support structured, FIFO or priority-based message passing.
#include <msgQLib.h>
MSG_Q_ID msgQ;
void setupQueue()
{
msgQ = msgQCreate(10, sizeof(int), MSG_Q_PRIORITY);
}
void producer()
{
int value = 123;
msgQSend(msgQ, (char*)&value, sizeof(int), WAIT_FOREVER, MSG_PRI_NORMAL);
}
void consumer()
{
int rcv;
msgQReceive(msgQ, (char*)&rcv, sizeof(int), WAIT_FOREVER);
printf("Received: %d\n", rcv);
}
Signal Handling (Software Interrupts) #
Signals are used for asynchronous notification.
#include <signal.h>
#include <sigLib.h>
void signalHandler(int sigNum)
{
printf("Signal %d received\n", sigNum);
}
void setupSignal()
{
sigset(SIGUSR1, signalHandler);
kill(taskIdSelf(), SIGUSR1); // Send signal to self
}
Virtual Devices #
Pipes and network sockets act like file descriptors.
Pipe as I/O Channel #
#include <ioLib.h>
#include <pipeDrv.h>
void setupPipe()
{
pipeDevCreate("/pipe/test", 1024, 1024);
int fd = open("/pipe/test", O_RDWR, 0);
write(fd, "hello", 5);
char buffer[6] = {0};
read(fd, buffer, 5);
printf("Received: %s\n", buffer);
}
Networking Basics #
VxWorks supports Berkeley Sockets API with IPv4/IPv6:
#include <sockLib.h>
#include <inetLib.h>
#include <netinet/in.h>
void openSocket()
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
// bind, listen, connect, etc.
}
File System API Example #
Using RAM disk:
#include <ramDrv.h>
#include <dosFsLib.h>
#include <ioLib.h>
void setupRamDisk()
{
ramDevCreate("/ram0", 512, 100); // 100 blocks of 512 bytes
dosFsVolFormat("/ram0", DOS_OPT_BLANK, NULL);
int fd = open("/ram0/file.txt", O_CREAT | O_RDWR, 0666);
write(fd, "VxWorks", 7);
close(fd);
}
Common Header Files #
Header File | Description |
---|---|
vxWorks.h |
Core definitions |
taskLib.h |
Task control functions |
semLib.h |
Semaphore support |
msgQLib.h |
Message queues |
sigLib.h |
Signal handling |
pipeDrv.h |
Pipe virtual device interface |
inetLib.h |
IP address and networking utilities |
Safety Features #
- Priority Inheritance in mutexes
- Task deletion protection
- Watchdog timers
- Virtual memory and MMU support
- Power management APIs
Summary #
VxWorks gives engineers precise control over task execution, synchronization, and inter-task communication. Key takeaways:
- Use
taskSpawn
,taskSuspend
,taskResume
for managing execution. - Apply semaphores and queues for IPC.
- Leverage virtual devices for modular I/O.
- Use signals for exception-like events.
- Extend systems with power management and network stacks.
Mastering these APIs empowers developers to design robust and responsive embedded applications.
References #
- Wind River VxWorks Documentation
- VxWorks 6 on-line Documents