π Introduction #
Signals in VxWorks are a lightweight asynchronous communication mechanism.
They allow one task to notify another task (or itself) that an event has occurred.
Think of signals as software interrupts:
- Sent to tasks at any time.
- Trigger a signal handler (callback function).
- Great for asynchronous events like timers, I/O, or inter-task alerts.
π§© Key Concepts of Signals #
- Signal Numbers β Each signal is identified by a number (
SIGUSR1,SIGUSR2, etc.). - Handlers β Tasks can register a function to handle signals.
- Delivery β A signal can be queued or replace existing ones depending on settings.
π» Example: Using Signals in VxWorks #
Weβll demonstrate:
- A signal handler function.
- A task that waits for signals.
- A task that sends signals to another.
Code Example #
#include <vxWorks.h>
#include <taskLib.h>
#include <signal.h>
#include <sigLib.h>
#include <stdio.h>
#include <unistd.h>
int targetTid;
// Signal handler
void mySignalHandler(int signo)
{
printf("Task %d: Received signal %d\n", taskIdSelf(), signo);
}
// Task that registers a signal handler
void signalReceiverTask()
{
struct sigaction sa;
sa.sa_handler = (void (*)(int))mySignalHandler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
// Register handler for SIGUSR1
sigaction(SIGUSR1, &sa, NULL);
printf("Receiver task ready (TID=%d). Waiting for signals...\n", taskIdSelf());
while (1)
{
taskDelay(200); // Idle loop
}
}
// Task that sends signals
void signalSenderTask()
{
while (1)
{
if (targetTid != 0)
{
kill(targetTid, SIGUSR1);
printf("Sender: Sent SIGUSR1 to TID=%d\n", targetTid);
}
taskDelay(100); // ~1 second
}
}
void usrAppInit(void)
{
// Spawn receiver
targetTid = taskSpawn("tRecv", 100, 0, 4000, (FUNCPTR)signalReceiverTask,
0,0,0,0,0,0,0,0,0,0);
// Spawn sender
taskSpawn("tSend", 110, 0, 4000, (FUNCPTR)signalSenderTask,
0,0,0,0,0,0,0,0,0,0);
}
π Explanation of the Code #
-
Signal Handler Registration
sigaction()installs a handler forSIGUSR1.- Any time
SIGUSR1is sent,mySignalHandler()executes.
-
Receiver Task
- Registers the handler and enters an idle loop.
-
Sender Task
- Uses
kill()to sendSIGUSR1to the receiverβs TID.
- Uses
β‘ What Youβll See #
Expected output:
Receiver task ready (TID=1234). Waiting for signals...
Sender: Sent SIGUSR1 to TID=1234
Task 1234: Received signal 16
Sender: Sent SIGUSR1 to TID=1234
Task 1234: Received signal 16
...
π Key Takeaways #
- Signals are lightweight asynchronous notifications in VxWorks.
- Use them for event-driven programming.
- Great for timers, I/O completions, or inter-task alerts.
β Wrap-Up #
In this tutorial, you learned:
- How to register and handle signals in VxWorks.
- How one task can send signals to another.
- How signals enable asynchronous communication.
In the next blog, weβll explore Watchdog Timers in VxWorks: Monitoring Task Health.