Skip to main content

Multi-Task-Based Network Communication under the VxWorks Real-Time Operating System

·752 words·4 mins
VxWorks RTOS Networking Embedded Systems
Table of Contents

Multi-Task-Based Network Communication under the VxWorks Real-Time Operating System

Abstract
#

This paper presents a practical approach to implementing network communication under the VxWorks real-time operating system. It first introduces the multi-task programming model provided by VxWorks, followed by an overview of BSD Socket–based client–server communication. Based on these foundations, a multi-task server architecture is proposed, and representative C-language code examples are provided to illustrate task creation, socket initialization, and inter-task coordination in real-time embedded environments.

1. Introduction
#

With the widespread adoption of high-performance embedded processors, embedded operating systems have become essential in communications, defense systems, industrial control, and medical equipment. VxWorks, as one of the most mature and widely deployed real-time operating systems, integrates a full TCP/IP protocol stack and provides strong support for multitasking and real-time scheduling.

By combining VxWorks multitasking mechanisms with BSD Socket programming, developers can implement reliable network communication between embedded targets and external systems such as PCs or workstations. This not only enables real-time data exchange but also improves system configurability, remote debugging, and runtime monitoring capabilities.

2. Task Management and Socket Programming in VxWorks
#

VxWorks uses a priority-based preemptive scheduling model. Each task is identified by a task ID and assigned a priority between 0 (highest) and 255 (lowest). Tasks with higher priority can preempt lower-priority tasks at any time.

To allow fair execution among tasks with equal priority, round-robin scheduling can be enabled during system initialization:

kernelTimeSlice(10);   /* Enable time slicing (ticks) */

2.1 Task Creation Example
#

Tasks are typically created using taskSpawn(), which both creates and starts a task:

int serverTaskId;

serverTaskId = taskSpawn(
    "tServer",
    100,                    /* priority */
    VX_FP_TASK,             /* options */
    8192,                   /* stack size */
    (FUNCPTR)serverMain,    /* entry point */
    0,0,0,0,0,0,0,0,0,0
);

This model allows each functional component—such as connection handling or data processing—to execute independently.

2.2 BSD Socket Support
#

VxWorks provides full support for BSD Sockets. The programming model closely follows standard UNIX socket APIs, making it easy to port existing network code to VxWorks.

3. Multi-Task Server Architecture
#

A multi-task server implementation is well suited to VxWorks due to its real-time scheduling and task isolation. In the proposed design, the server is decomposed into multiple cooperating tasks, including:

  • Initialization task
  • Connection acceptance task
  • Message sending task
  • Message receiving task
  • Network monitoring task

Each task has a clear responsibility, reducing complexity and improving system reliability.

4. Server-Side Implementation with Code Examples
#

4.1 Initialization and Listening Socket
#

The initialization task creates a listening socket and spawns the connection acceptance task.

#define SERVER_PORT 5000

int listenSock;

void initTask(void)
{
    struct sockaddr_in serverAddr;

    listenSock = socket(AF_INET, SOCK_STREAM, 0);
    if (listenSock < 0)
    {
        perror("socket");
        return;
    }

    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port   = htons(SERVER_PORT);
    serverAddr.sin_addr.s_addr = INADDR_ANY;

    if (bind(listenSock, (struct sockaddr *)&serverAddr,
             sizeof(serverAddr)) < 0)
    {
        perror("bind");
        close(listenSock);
        return;
    }

    listen(listenSock, 5);

    taskSpawn("tAccept", 90, 0, 8192,
              (FUNCPTR)acceptTask,
              0,0,0,0,0,0,0,0,0,0);

    taskDelete(0);   /* Initialization task exits */
}

4.2 Connection Acceptance Task
#

The acceptance task waits for incoming connections and spawns communication tasks for each client.

void acceptTask(void)
{
    int clientSock;
    struct sockaddr_in clientAddr;
    int addrLen = sizeof(clientAddr);

    while (1)
    {
        clientSock = accept(listenSock,
                            (struct sockaddr *)&clientAddr,
                            &addrLen);
        if (clientSock < 0)
            continue;

        taskSpawn("tRecv", 80, 0, 8192,
                  (FUNCPTR)recvTask,
                  clientSock,0,0,0,0,0,0,0,0,0);

        taskSpawn("tSend", 85, 0, 8192,
                  (FUNCPTR)sendTask,
                  clientSock,0,0,0,0,0,0,0,0,0);
    }
}

4.3 Message Receiving Task
#

The receiving task continuously reads data from the socket and processes client messages.

void recvTask(int sock)
{
    char buf[256];
    int n;

    while ((n = recv(sock, buf, sizeof(buf) - 1, 0)) > 0)
    {
        buf[n] = '\0';
        printf("Received: %s\n", buf);

        if (strcmp(buf, "quit") == 0)
            break;
    }

    close(sock);
    taskDelete(0);
}

4.4 Message Sending Task
#

The sending task reads keyboard input and transmits it to the client.

void sendTask(int sock)
{
    char buf[256];

    while (1)
    {
        if (fgets(buf, sizeof(buf), stdin) == NULL)
            continue;

        send(sock, buf, strlen(buf), 0);

        if (strncmp(buf, "quit", 4) == 0)
            break;
    }

    taskDelete(0);
}

4.5 Network Monitoring and Cleanup
#

In a complete system, an additional monitoring task supervises task termination, socket closure, and error recovery. This task ensures that system resources are released correctly and that the server can safely return to a listening state after a client disconnects.

5. Conclusion
#

This paper demonstrated how multi-task programming and BSD Socket communication can be combined under VxWorks to implement a robust network server. By decomposing functionality into independent tasks and using standard socket APIs, the system achieves high real-time responsiveness, improved modularity, and easier maintenance. The provided code examples illustrate a practical foundation for developing networked embedded applications based on VxWorks.

Related

UDP Networking in VxWorks: Sending and Receiving Packets
·553 words·3 mins
VxWorks RTOS Networking Sockets UDP Embedded Systems Programming Tutorial
Handling Multiple Clients in VxWorks with select
·648 words·4 mins
VxWorks RTOS Networking Sockets Select() Multi-Client Embedded Systems Programming Tutorial
Networking with VxWorks: Socket Programming Basics
·578 words·3 mins
VxWorks RTOS Networking Sockets TCP/IP Embedded Systems Programming Tutorial