This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

有关sys/bios中的Semaphore信号量



大家好 我对下面程序的工作流程不太清楚 尤其是:

sem = Semaphore_create(1, NULL, NULL);

sem = Semaphore_create(0, NULL, NULL);

这两个有什么区别?程序中把1改为0后 程序运行结果区别很大!

Void task1(UArg arg0, UArg arg1);
Void task2(UArg arg0, UArg arg1);

Int resource = 0;

Semaphore_Handle sem;
Task_Handle tsk1;
Task_Handle tsk2;

Int finishCount = 0;

/*
* ======== main ========
*/
Int main()
{
Task_Params taskParams;

/* Create a Semaphore object to be use as a resource lock */
sem = Semaphore_create(1, NULL, NULL);

/* Create two tasks that share a resource*/
Task_Params_init(&taskParams);
taskParams.priority = 1;
tsk1 = Task_create (task1, &taskParams, NULL);

Task_Params_init(&taskParams);
taskParams.priority = 2;
tsk2 = Task_create (task2, &taskParams, NULL);

BIOS_start(); /* does not return */
return(0);
}

/*
* ======== task1 ========
*/
Void task1(UArg arg0, UArg arg1)
{
UInt32 time;

for (;;)
{
System_printf("Running task1 function\n");

if (Semaphore_getCount(sem) == 0)
{
System_printf("Sem blocked in task1\n");
}

/* Get access to resource */
Semaphore_pend(sem, BIOS_WAIT_FOREVER);

/* do work by waiting for 2 system ticks to pass */
time = Clock_getTicks();
while (Clock_getTicks() <= (time + 1))
{
;
}

/* do work on locked resource */
resource += 1;
/* unlock resource */

Semaphore_post(sem);

Task_sleep(10);
}
}

/*
* ======== task2 ========
*/
Void task2(UArg arg0, UArg arg1)
{
for (;;)
{
System_printf("Running task2 function\n");

if (Semaphore_getCount(sem) == 0)
{
System_printf("Sem blocked in task2\n");
}

/* Get access to resource */
Semaphore_pend(sem, BIOS_WAIT_FOREVER);

/* do work on locked resource */
resource += 1;
/* unlock resource */

Semaphore_post(sem);

Task_sleep(10);

finishCount++;
if (finishCount == 5)
{
System_printf("Calling BIOS_exit from task2\n");
BIOS_exit(0);
}
}
}

  • 看一下函数原型:
    Semaphore_Handle Semaphore_create(
    Int count,
    Semaphore_Params *attrs
    Error_Block *eb );
    count的含义:The semaphore count is initialized to count when it is created. In general, count is set to the number of resources that the semaphore is synchronizing.

    Semaphore_pend和Semaphore_post操作会改变计数值,这与任务的挂起还是执行是相关联的。
    具体可以查看sys/bios user guide的相关内容。
    http://www.ti.com/lit/ug/spruex3u/spruex3u.pdf

  • 谢谢 大体明白了

    信号量可分互斥信号量和计数信号量。默认计数信号量。

    计数信号量通过设置一个计数值,如果计数值大于0,则任务请求该信号量时是可用。

    Semaphore_pend和Semaphore_post操作会改变计数值  

    例程中 初值是1 所以遇到Semaphore_pend(sem, BIOS_WAIT_FOREVER);不会被阻塞 在Task_sleep(10);处会阻塞 我猜是这样

  • 是的,可以这样理解。
  • 你好 我确认下

    Task_sleep(10);表示阻塞 还是表示延迟等待 看函数原型:

    Name: Task_sleep

    Prototype: Void Task_sleep( UInt nticks );

    Description:

    Delay execution of the current task 延迟当前任务的执行

    看手册 又是表示阻塞

    Semaphore_pend对应的是Semaphore_post

    如果是Task_sleep造成的阻塞 那任务如何恢复了???是不需要相应函数 先去执行别的任务 延时够了以后 再继续执行Task_sleep后面的???

  • 可以理解为让当前任务延迟执行,Task_sleep(ticks)中设置的tick值时间到了就会继续执行当前任务,这个时间可以根据需要自行配置。
  • 好的 谢了
    明白了 不管Task_sleep 还是Semaphore_pend 都是从阻塞点处继续执行原先任务