开发者

What task is supposed to be created in this example?

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


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.


SemTake and SemGive are returning errors (since the semaphore does not exist). It is valuable to check the return codes on system calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜