Skip to main content

Watchdog Timers in VxWorks: Monitoring Task Health

·457 words·3 mins
VxWorks RTOS Watchdog Timers Task Monitoring Embedded Systems Programming Tutorial
Table of Contents
VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 16: This Article

๐Ÿš€ Introduction
#

In embedded systems, ensuring that tasks stay alive and responsive is critical.
VxWorks provides watchdog timers to detect when a task is not behaving as expected and trigger corrective action.

A watchdog works like this:

  1. You create and start a watchdog timer.
  2. If the timer is not reset (or โ€œkickedโ€) within a time period, it expires.
  3. The expiration runs a callback function to handle recovery.

๐Ÿงฉ Why Use Watchdog Timers?
#

  • Detect stalled tasks โ†’ restart or reset them.
  • Increase reliability in safety-critical applications.
  • Fail-safe behavior in case of deadlocks or infinite loops.

๐Ÿ’ป Example: Using Watchdog Timers in VxWorks
#

Weโ€™ll demonstrate:

  1. A task monitored by a watchdog.
  2. A watchdog callback that runs when the task โ€œhangs.โ€

Code Example
#

#include <vxWorks.h>
#include <wdLib.h>
#include <taskLib.h>
#include <stdio.h>

WDOG_ID wdId;
int monitoredTid;

// Watchdog callback function
void wdCallback(int arg)
{
    printf("Watchdog expired! Restarting monitored task...\n");

    // Restart monitored task
    taskRestart(monitoredTid);
}

// Task that is being monitored
void monitoredTask()
{
    int counter = 0;

    while (1)
    {
        printf("Monitored task running, counter=%d\n", counter++);

        // Simulate hang condition
        if (counter == 5)
        {
            printf("Task is stuck! Watchdog should trigger soon...\n");
            while (1); // infinite loop โ†’ hang
        }

        taskDelay(100); // ~1s delay
    }
}

// Watchdog management task
void watchdogTask()
{
    wdId = wdCreate();

    if (wdId == NULL)
    {
        printf("Failed to create watchdog\n");
        return;
    }

    // Spawn monitored task
    monitoredTid = taskSpawn("tMon", 100, 0, 4000, (FUNCPTR)monitoredTask,
                             0,0,0,0,0,0,0,0,0,0);

    // Periodically restart watchdog
    while (1)
    {
        wdStart(wdId, 200, (FUNCPTR)wdCallback, 0); // expire after ~2s
        printf("Watchdog started/reset\n");

        taskDelay(150); // Reset before timeout (unless task hangs)
    }
}

void usrAppInit(void)
{
    taskSpawn("tWd", 90, 0, 4000, (FUNCPTR)watchdogTask,
              0,0,0,0,0,0,0,0,0,0);
}

๐Ÿ“ Explanation of the Code
#

  1. Watchdog Setup

    • wdCreate() creates a watchdog object.
    • wdStart() arms it with a timeout and callback.
  2. Monitored Task

    • Prints a counter until it intentionally hangs at counter = 5.
  3. Watchdog Callback

    • Prints a warning and restarts the hung task using taskRestart().
  4. Watchdog Management Task

    • Periodically restarts the watchdog.
    • If the task hangs and watchdog isnโ€™t reset โ†’ callback executes.

โšก What Youโ€™ll See
#

Expected console output:

Monitored task running, counter=0
Watchdog started/reset
Monitored task running, counter=1
Watchdog started/reset
...
Task is stuck! Watchdog should trigger soon...
Watchdog expired! Restarting monitored task...
Monitored task running, counter=0
Watchdog started/reset

๐Ÿ” Key Takeaways
#

  • Watchdog timers help ensure task responsiveness.
  • If a task hangs, the watchdog recovers the system automatically.
  • Essential for safety-critical and real-time applications.

โœ… Wrap-Up
#

In this tutorial, you learned:

  • How to create and manage watchdog timers in VxWorks.
  • How to detect and recover from a hung task.
  • How watchdogs improve system reliability.

In the next blog, weโ€™ll cover Task Priorities and Scheduling in VxWorks to better understand how the RTOS manages execution order.

VxWorks Programming Tutorial for Beginners - This article is part of a series.
Part 16: This Article

Related

Timers in VxWorks: Periodic and One-Shot Timers
·356 words·2 mins
VxWorks RTOS Timers Watchdog Embedded Systems Programming Tutorial
Inter-Task Communication with Message Queues in VxWorks
·459 words·3 mins
VxWorks RTOS IPC Message Queues Embedded Systems Programming Tutorial
UDP Networking in VxWorks: Sending and Receiving Packets
·553 words·3 mins
VxWorks RTOS Networking Sockets UDP Embedded Systems Programming Tutorial