微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > FPGA,CPLD和ASIC > Example ofsynchronizationthrough binarysemaphore

Example ofsynchronizationthrough binarysemaphore

时间:10-02 整理:3721RD 点击:
#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);
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top