原创 vxworks编程方法(3)

2006-10-25 12:33 4811 10 6 分类: MCU/ 嵌入式
3. 信号量(Semaphore): 信号量是解决互斥和同步协调进程最好的方法
当一个Semaphore创建时,指定了任务队列的种类
semBCreat( SEM_Q_PRIORITY, SEM_FULL), SEM_Q_PRIORITY 指明处于等待状态的任务在等待队列中以优先级的顺序排列
semBCreat(SEM_Q_FIFO,SEM_FULL), SEM_Q_FIFO指明 处于等待状态的任务在等待队列中以先进先出的顺序排列
当一个Semaphore创建时,指定了这个semaphore是用在解决互斥还是用来同步任务
semBCreat( SEM_Q_FIFO, SEM_FULL) , SEM_FULL 指明用于任务间互斥.
SEM_ID semMutex;
semMutex = semBCreate (SEM_Q_PRIORITY, SEM_FULL);
.........
semTake (semMutex, WAIT_FOREVER);
. . critical region, only accessible by a single task at a time .
semGive (semMutex);
semBCreat(SEM_Q_FIFO,SEM_EMPTY), SEM_EMPTY 指明用于任务间同步.
/* includes */
#include "vxWorks.h"
#include "semLib.h"
SEM_ID syncSem;
/* ID of sync semaphore */
init ( int someIntNum )
{ /* connect interrupt service routine */
intConnect (INUM_TO_IVEC (someIntNum), eventInterruptSvcRout, 0);
/* create semaphore */
syncSem = semBCreate (SEM_Q_FIFO, SEM_EMPTY);
/* spawn task used for synchronization. */
taskSpawn ("sample", 100, 0, 20000, task1, 0,0,0,0,0,0,0,0,0,0);
}
task1 (void)
{ ...
semTake (syncSem, WAIT_FOREVER);
/* wait for event to occur */
printf ("task 1 got the semaphore\n");
...
/* process event */
}
eventInterruptSvcRout (void)
{ ...
semGive (syncSem);
/* let task 1 process event */
...
}

文章评论0条评论)

登录后参与讨论
我要评论
0
10
关闭 站长推荐上一条 /2 下一条