本例中应该创建什么任务?
对于普通的二进制信号量, 任务尝试同步到 外部事件创建一个空的 信号量......第二个任务 控制同步事件 当没有信号量时给出信号量 需要更长的时间。
#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);
}
我的问题是为什么我没有看到第一个任务创建空信号量,如所述? (看起来它只是在主函数中“一般”完成?)“尝试同步到外部事件的任务会创建一个空信号量”。
此外,我真的不明白如何第二个任务是“控制”同步?
谢谢。
请参阅:通过二进制信号量进行同步的示例
http://www.cross-comp.com/instr /pages/embedded/VxWorksTutorial.aspx#VxWorks%20Programming
For an ordinary binary semaphore, a
task attempting to synchronize to an
external event creates an empty
semaphore....A second task which
controls the synchronization event
gives the semaphore when it is no
longer needed.
#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);
}
My question is why don't I see the first task creating the empty semaphore, as described? (It looks like it is just done "generically" in the main function?) "a task attempting to synchronize to an external event creates an empty semaphore".
Also, I don't really see how the second task is "controlling" the synchronization?
Thank you.
See: Example of synchronization through binary semaphore
http://www.cross-comp.com/instr/pages/embedded/VxWorksTutorial.aspx#VxWorks%20Programming
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例中有一个错误。
initialize
中的第一行应分配给syncExampleSem
。第二个任务“控制”同步,因为任务 1 在任务 2“给出”信号量之前无法继续。信号量在哪里创建并不重要,只要保证在任一任务尝试提供或获取信号量之前创建信号量即可。
由于这些特定任务是并行运行的,因此它是在
initialize
中创建的,因为如果它是由任务 2 创建的,那么您将面临任务 1 在信号量存在之前等待信号量的风险,反之亦然。由任务 1 创建,您将面临任务 2 在信号量存在之前就给出信号量的风险。There's a bug in the example. The first line in
initialize
should be assigning tosyncExampleSem
.The second task "controls" the synchronization because task 1 can't proceed until task 2 "gives" the semaphore. It doesn't really matter where the semaphore is created, as long as it is guaranteed to be created before either task tries to either give or take it.
Since these particular tasks are running in parallel, it is created in
initialize
because if it was created by task 2 you run the risk of task 1 waiting on the semaphore before it exists, and vice versa if it is created by task 1 you run the risk of task 2 giving the semaphore before it exists.SemTake 和 SemGive 返回错误(因为信号量不存在)。
检查系统调用的返回代码很有价值。
SemTake and SemGive are returning errors (since the semaphore does not exist).
It is valuable to check the return codes on system calls.