π Introduction #
Semaphores are one of the most important synchronization mechanisms in VxWorks.
They are used for:
- Task synchronization β coordinating execution order between tasks.
- Resource protection β ensuring only one task uses a resource at a time.
VxWorks provides three main types of semaphores:
- Binary Semaphores
- Counting Semaphores
- Mutexes (Mutual Exclusion Semaphores)
π§© Types of Semaphores in VxWorks #
1. Binary Semaphores #
- Simple lock/unlock mechanism (0 or 1).
- Great for signaling events between tasks.
2. Counting Semaphores #
- Keep track of multiple resources.
- Useful when several identical resources are available (e.g., buffer pool).
3. Mutexes #
- Special semaphores for mutual exclusion.
- Provide priority inheritance to avoid priority inversion problems.
π» Example: Using Semaphores in VxWorks #
Weβll demonstrate:
- A Binary Semaphore for task synchronization.
- A Counting Semaphore for managing resource pool.
- A Mutex for protecting a shared resource.
Code Example #
#include <vxWorks.h>
#include <semLib.h>
#include <taskLib.h>
#include <stdio.h>
SEM_ID binSem;
SEM_ID countSem;
SEM_ID mutexSem;
// Task waiting for binary semaphore
void taskA()
{
while (1)
{
semTake(binSem, WAIT_FOREVER);
printf("Task A: Received signal from Task B\n");
}
}
// Task signaling binary semaphore
void taskB()
{
while (1)
{
taskDelay(100);
semGive(binSem);
printf("Task B: Signaled Task A\n");
}
}
// Counting semaphore example
void resourceUser(int id)
{
semTake(countSem, WAIT_FOREVER);
printf("Task %d: Acquired resource\n", id);
taskDelay(200);
printf("Task %d: Released resource\n", id);
semGive(countSem);
}
// Mutex example (shared counter)
int sharedCounter = 0;
void counterTask(int id)
{
while (1)
{
semTake(mutexSem, WAIT_FOREVER);
sharedCounter++;
printf("Task %d: Incremented counter to %d\n", id, sharedCounter);
semGive(mutexSem);
taskDelay(50);
}
}
void usrAppInit(void)
{
// Binary semaphore (initially empty)
binSem = semBCreate(SEM_Q_PRIORITY, SEM_EMPTY);
// Counting semaphore with 2 resources
countSem = semCCreate(SEM_Q_PRIORITY, 2);
// Mutex semaphore
mutexSem = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE);
// Binary semaphore demo
taskSpawn("tA", 100, 0, 4000, (FUNCPTR)taskA, 0,0,0,0,0,0,0,0,0,0);
taskSpawn("tB", 110, 0, 4000, (FUNCPTR)taskB, 0,0,0,0,0,0,0,0,0,0);
// Counting semaphore demo
taskSpawn("tRes1", 120, 0, 4000, (FUNCPTR)resourceUser, 1,0,0,0,0,0,0,0,0,0);
taskSpawn("tRes2", 130, 0, 4000, (FUNCPTR)resourceUser, 2,0,0,0,0,0,0,0,0,0);
taskSpawn("tRes3", 140, 0, 4000, (FUNCPTR)resourceUser, 3,0,0,0,0,0,0,0,0,0);
// Mutex demo
taskSpawn("tCnt1", 150, 0, 4000, (FUNCPTR)counterTask, 1,0,0,0,0,0,0,0,0,0);
taskSpawn("tCnt2", 160, 0, 4000, (FUNCPTR)counterTask, 2,0,0,0,0,0,0,0,0,0);
}
π Explanation of the Code #
-
Binary Semaphore
- Task B signals (
semGive), Task A waits (semTake). - Ensures Task A only runs when Task B signals.
- Task B signals (
-
Counting Semaphore
- Allows two tasks to acquire the resource simultaneously.
- Third task waits until one releases.
-
Mutex
- Protects the
sharedCounter. - Ensures only one task updates it at a time.
- Prevents priority inversion with
SEM_INVERSION_SAFE.
- Protects the
β‘ What Youβll See #
Sample output may look like:
Task B: Signaled Task A
Task A: Received signal from Task B
Task 1: Acquired resource
Task 2: Acquired resource
Task 3: Waiting...
Task 1: Released resource
Task 3: Acquired resource
Task 1: Incremented counter to 1
Task 2: Incremented counter to 2
Task 1: Incremented counter to 3
...
π Key Takeaways #
- Binary Semaphores β great for signaling between tasks.
- Counting Semaphores β manage multiple identical resources.
- Mutexes β protect shared resources with priority inversion handling.
β Wrap-Up #
In this tutorial, you learned:
- How to use binary semaphores for task synchronization.
- How to use counting semaphores for resource management.
- How to use mutexes for safe shared resource access.
In the next blog, weβll explore Message Passing with Pipes in VxWorks β another powerful IPC mechanism.
π Stay tuned for Blog 14: βMessage Passing with Pipes in VxWorks.β