本例中应该创建什么任务?

发布于 2024-10-10 17:46:16 字数 1243 浏览 4 评论 0原文

对于普通的二进制信号量, 任务尝试同步到 外部事件创建一个空的 信号量......第二个任务 控制同步事件 当没有信号量时给出信号量 需要更长的时间。

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尴尬癌患者 2024-10-17 17:46:16

示例中有一个错误。 initialize 中的第一行应分配给 syncExampleSem

第二个任务“控制”同步,因为任务 1 在任务 2“给出”信号量之前无法继续。信号量在哪里创建并不重要,只要保证在任一任务尝试提供或获取信号量之前创建信号量即可。

由于这些特定任务是并行运行的,因此它是在 initialize 中创建的,因为如果它是由任务 2 创建的,那么您将面临任务 1 在信号量存在之前等待信号量的风险,反之亦然。由任务 1 创建,您将面临任务 2 在信号量存在之前就给出信号量的风险。

There's a bug in the example. The first line in initialize should be assigning to syncExampleSem.

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.

我喜欢麦丽素 2024-10-17 17:46:16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文