在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 1890|回复: 0

Example ofsynchronizationthrough binarysemaphore

[复制链接]
发表于 2007-6-8 10:36:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

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);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

小黑屋| 手机版| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-11-17 09:56 , Processed in 0.012469 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表