tiancaigao7 发表于 2011-11-13 10:54:35

RTT系统信号量创建类型中优先级模式和fifo模式有什么区别?

请教一个问题,RTT系统中,创建信号量可以选择FIFO模式或者优先级模式,我理解优先级模式就是ucos中的信号量模式:发送信号之后会任务调度器会激活优先级最高的申请该信号量的任务。但是FIFO模式我不理解,手册上也没有详细的介绍。这个模式是如何切换任务的呢?是不是完全不按照优先级别,仅仅看那个任务先申请的信号量?如果有四个任务,ABCD,优先级AB相同,CD相同,且AB高于CD,但是申请信号量的时间是C早于A早于D早于B,那么在信号量按照FIFO模式创建后,当有一个任务释放了一个信号量,那个任务应该被激活?这点不明白,希望大家能解释一下。

ffxz 发表于 2011-11-13 11:02:58

FIFO是先进先出,谁先去申请信号量,当信号量释放时,将先拿到。

tiancaigao7 发表于 2011-11-13 15:35:19

回复【1楼】ffxz
fifo是先进先出,谁先去申请信号量,当信号量释放时,将先拿到。
-----------------------------------------------------------------------

做了一个例子实验了一下发现这两个模式没有什么区别?起码在我使用中没有区别,实现的功能都是:如果任务之间优先级不同,那么优先级最高的任务先得到信号量,如果是同优先级之间的任务,那么先申请的先拿到任务量。这个功能,这两个模式都一样。

aozima 发表于 2011-11-13 16:55:52

可以参考 /examples/kernel/semaphore_priority.c
小做一下修改
static void thread1_entry(void* parameter)
{
    rt_err_t result;

    rt_thread_delay(100);
    rt_kprintf("t1 take\r\n");

    ......
}

static void thread2_entry(void* parameter)
{
    rt_err_t result;
    rt_thread_delay(200);
    rt_kprintf("t2 take\r\n");

    while (1)
    {
      result = rt_sem_take(sem, RT_WAITING_FOREVER);
      if (result != RT_EOK)
      {
            tc_done(TC_STAT_FAILED);
            return;
      }

      t2_count ++;
      if( t2_count > 4 )
      {
            rt_thread_delay(20);
      }
      rt_kprintf("thread2: got semaphore, count: %d\n", t2_count);
    }
}

static void worker_thread_entry(void* parameter)
{
    rt_thread_delay(500); // 这里的延时应该大于 t1 和 t2 的
   
    ......
}

测试结果:
\ | /
- RT -   Thread Operating System
/ | \ 0.4.0 build Nov 13 2011
2006 - 2011 Copyright by rt-thread team
finsh>>
t1 take                        // t1低优先级 先take
t2 take                        // t2高优先级 后take
thread2: got semaphore, count: 1 // 后到的高优先级先获取到资源
thread2: got semaphore, count: 2
thread2: got semaphore, count: 3
thread2: got semaphore, count: 4 //获取4次以后开始主动延时
thread1: got semaphore, count: 1 //然后低优先级的线程就获取到了信号量
thread1: got semaphore, count: 2
thread1: got semaphore, count: 3
thread2: got semaphore, count: 5
thread1: got semaphore, count: 4
thread1: got semaphore, count: 5
thread1: got semaphore, count: 6
thread2: got semaphore, count: 6
thread1: got semaphore, count: 7
thread1: got semaphore, count: 8
thread1: got semaphore, count: 9
thread2: got semaphore, count: 7

tiancaigao7 发表于 2011-11-14 08:48:46

我也做了类似楼上的实验,建立两个线程A B用来申请信号量,建立一个线程C用来发送信号量(信号量按照FIFO模式创建)。先将A B线程启动,然后在启动C。下面是实验结果:
如果A B任务优先级相同,那么先申请信号量的任务获得信号量;
如果A的优先级高于B,但是B先于A申请信号量,那么A任务获得信号量。
因此我得到结论:信号量申请中,优先级别高的线程先获得信号量,同优先级的线程,先申请的获得信号量。
页: [1]
查看完整版本: RTT系统信号量创建类型中优先级模式和fifo模式有什么区别?