Semaphore

Hi,

I'm working on a project,using VxWorks to create a real-time embedded system,I have a task that creates 3 other tasks using taskSpawn,and to share resources between them,I opted for binary semaphore, but that has proven to be very challenging.

see sample code attached below

#include "vxWorks.h"
#include "sysLib.h"
#include "taskLib.h"
#include "stdio.h"
#include "stdlib.h"
#include "semLib.h"

void Task1();
void Task2();

SEM_ID semID;

STATUS main ()
{

int task1id;
int task2id;

task1id = taskSpawn("Task 1", 101, 0, 20000, (FUNCPTR)Task1,0,0,0,0,0,0,0,0,0,0);
task2id = taskSpawn("Task 2", 110, 0, 20000, (FUNCPTR)Task2,0,0,0,0,0,0,0,0,0,0);
taskDelay(30 * sysClkRateGet());
taskDelete(task1id);
taskDelete(task2id);

}

void Task1()
{


semID = semBCreate(SEM_Q_FIFO, SEM_EMPTY)


while(1)
{
condition = either situation1 or situation2

if (situation1 is true)
{
pause for x secs,
}
if (situation2 is true)
{
semGive(semID);
break;
}
}

}

void Task2()
{
semTake(semID, WAIT_FOREVER);
pause for x secs;
do something;
pause for x secs);
do something;;
}


In the above code, main() creates 2 tasks(Task1 and Task2)
Task1() creates a semaphore and give semaphore to Task2 if one of the condition is true
Task2() takes semaphore and do task .
All this happens once,I would like my code to continue doing the activity






closed account (S6k9GNh0)
Please learn how to use tags: http://cplusplus.com/articles/firedraco1/

Until you do, I (personally) will not reply with help.
Put the thread code in loops. Think about when Task2 can release the semaphore and when Task1 needs to aquire it.

Please use the Source Code tags to format your code.
Topic archived. No new replies allowed.