|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
#include "vxWorks.h"
#include "semLib.h"
#define T_PRIORITY 50
SEM_ID syncExampleSem; // named semaphore object
void initialize (void)
{
// set up FIFO queue with emtpy binary semaphore
syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);
// create task1
taskSpawn ("task1", T_PRIORITY, 0, 10000, task1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// create task2
taskSpawn ("task2", T_PRIORITY, 0, 10000, task2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
void task1 (void)
{
// stay here until semaphore becomes available
semTake (syncExampleSem, WAIT_FOREVER);
// do something
}
void task2 (void)
{
// do something
// now let task1 execute
semGive (synExampleSem);
}
Example ofresource lockoutthrough mutualexclusionsemaphore
#include "vxWorks.h"
#include "semLib.h"
#define T_HI_PRIORITY 20
#define T_LO_PRIORITY 200
SEM_ID semMutex; // named semaphore object
char alphabet[27]; // memory resource to have exclusive access
void initialize (void)
{
//create binary semaphore which is initially full
semMutex = semBCreate (SEM_Q_PRIORITY, SEM_FULL);
// spawn high priority task
taskSpawn ("hi_priority_task", T_HI_PRIORITY, 10000, tHiPriorityTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// spawn low priority task
taskSpawn ("lo_priority_task", T_LO_PRIORITY, 10000, tLoPriorityTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
void tHiPriorityTask (void)
{
int i;
// enter critical region - any other tasks wanting access to alphabet[] should
// wait for available semaphore
semTake (semMutex, WAIT_FOREVER);
// write alphabet to global array
for(i=0;i<26; i++)
alphabet = 'A' + i;
alphabet = '\0';
// leave critical region
semGive (semMutex);
}
void tLoPriorityTask (void)
{
// enter critical region
semTake (semMutex, WAIT_FOREVER);
// array members guaranteed stable while being
printf ("alphabet= %s ", alphabet);
// leave critical region
semGive (semMutex);
} |
|