Why does my RTOS MicroC project run smoothly outside tasks but fail within tasks?

Please guys @Middleware & OS , why is my RTOS MicroC project encountering issues where functions execute smoothly outside tasks but fails within tasks. Any pointers on troubleshooting this issue? Observe my cod e👇👇👇👇
// Create a semaphore
Semaphore sem = Semaphore(1);

// Task 1
void Task1(void *args)
{
while (true)
{
// Wait for semaphore
sem.wait();

// Call function
adjustTemperature();

// Release semaphore
sem.signal();

// Delay
delay(50);
}
}

// Task 2
void Task2(void *args)
{
while (true)
{
// Wait for semaphore
sem.wait();

// Call function
monitorPressure();

// Release semaphore
sem.signal();

// Delay
delay(50);
}
}
// Create a semaphore
Semaphore sem = Semaphore(1);

// Task 1
void Task1(void *args)
{
while (true)
{
// Wait for semaphore
sem.wait();

// Call function
adjustTemperature();

// Release semaphore
sem.signal();

// Delay
delay(50);
}
}

// Task 2
void Task2(void *args)
{
while (true)
{
// Wait for semaphore
sem.wait();

// Call function
monitorPressure();

// Release semaphore
sem.signal();

// Delay
delay(50);
}
}
Any insights or advice on where to begin looking would be greatly appreciated. Thank you in advance!
8 Replies
Manuel
Manuel•8mo ago
Exactly how are they failing? Is there a crash? Do they nothing, as if they where not even called? Another behavior?
Marvee Amasi
Marvee Amasi•8mo ago
@Manuel , my functions (adjustTemperature() and monitorPressure()) works correctly when called from outside the tasks, but fail to execute when called from within tasks. That is task 1 and 2 . But it seams my code is correct right ? Or is there anything I am doing wrong
Manuel
Manuel•8mo ago
I have no experience with MicroC, therefore cannot say for sure. But the code looks straight forward. Are you sure your tasks are run at all?
Marvee Amasi
Marvee Amasi•8mo ago
Yes it's actually running, except sem.wait(); is actually running an infinity loop synchronously , blocking the functions beneath not to be attended to . I don't know let me just try as much troubleshooting as I can @Manuel
Manuel
Manuel•8mo ago
What does sem.wait() do? What has to happen to end the waiting?
Marvee Amasi
Marvee Amasi•8mo ago
sem.wait() attempts to acquire the semaphore. If the semaphore is available and not throwing a zero value ,the task decrements the semaphore value and continues execution. Else if the semaphore is not available and its value is zero, the task enters a waiting state until the semaphore becomes available, I think I know where my issue is coming from , thanks for helping @Manuel
Manuel
Manuel•8mo ago
Let us know what the solution was, so we can learn from it, too. 🙂
Marvee Amasi
Marvee Amasi•8mo ago
Semosphere has to be returning a value of more than zero , that was what I was meant to check
Want results from more Discord servers?
Add your server