zoukankan      html  css  js  c++  java
  • UCOSIII互斥信号量

    互斥信号量可以解决优先级反转问题

    优化后现象
    1

    优化方法:L和H等待同一个信号量的时候,将L任务优先级提至H相同优先级

    实验举例

    void start_task(void *p_arg)
    {
        OS_CRITICAL_ENTER();    //进入临界区
    
        //创建一个互斥信号量
        OSMutexCreate((OS_MUTEX*    )&TEST_MUTEX,
                      (CPU_CHAR*    )"TEST_MUTEX",
                      (OS_ERR*      )&err);
    
        //创建HIGH任务
        OSTaskCreate((OS_TCB    * )&High_TaskTCB,       
                     (CPU_CHAR  * )"High task",         
                     (OS_TASK_PTR )high_task,           
                     (void      * )0,                   
                     (OS_PRIO     )HIGH_TASK_PRIO,     
                     (CPU_STK   * )&HIGH_TASK_STK[0],   
                     (CPU_STK_SIZE)HIGH_STK_SIZE/10,    
                     (CPU_STK_SIZE)HIGH_STK_SIZE,       
                     (OS_MSG_QTY  )0,                   
                     (OS_TICK     )0,                   
                     (void      * )0,                   
                     (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
                     (OS_ERR    * )&err);           
    
        //创建MIDDLE任务
        OSTaskCreate((OS_TCB    * )&Middle_TaskTCB,     
                     (CPU_CHAR  * )"Middle task",       
                     (OS_TASK_PTR )middle_task,             
                     (void      * )0,                   
                     (OS_PRIO     )MIDDLE_TASK_PRIO,     
                     (CPU_STK   * )&MIDDLE_TASK_STK[0], 
                     (CPU_STK_SIZE)MIDDLE_STK_SIZE/10,  
                     (CPU_STK_SIZE)MIDDLE_STK_SIZE,     
                     (OS_MSG_QTY  )0,                   
                     (OS_TICK     )0,                   
                     (void      * )0,                   
                     (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
                     (OS_ERR    * )&err);       
    
        //创建LOW任务
        OSTaskCreate((OS_TCB    * )&Low_TaskTCB,        
                     (CPU_CHAR  * )"Low task",      
                     (OS_TASK_PTR )low_task,            
                     (void      * )0,                   
                     (OS_PRIO     )LOW_TASK_PRIO,     
                     (CPU_STK   * )&LOW_TASK_STK[0],    
                     (CPU_STK_SIZE)LOW_STK_SIZE/10, 
                     (CPU_STK_SIZE)LOW_STK_SIZE,        
                     (OS_MSG_QTY  )0,                   
                     (OS_TICK     )0,                   
                     (void      * )0,                   
                     (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
                     (OS_ERR    * )&err);                    
    
        OS_CRITICAL_EXIT(); //退出临界区
    }
    
    //高优先级任务的任务函数
    void high_task(void *p_arg)
    {
        OS_ERR err;
    
        CPU_SR_ALLOC();
    
        while(1)
        {
            OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_PERIODIC,&err);     //延时500ms
            printf("high task Pend Sem
    ");
            OSMutexPend(&TEST_MUTEX,0,OS_OPT_PEND_BLOCKING,0,&err); //请求互斥信号量
            printf("high task Running!
    ");
            OSMutexPost(&TEST_MUTEX,OS_OPT_POST_NONE,&err);         //释放互斥信号量
            OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_PERIODIC,&err);     //延时500ms
        }
    }
    
    //中等优先级任务的任务函数
    void middle_task(void *p_arg)
    {   
        OS_ERR err;
    
        CPU_SR_ALLOC();
    
        while(1)
        {
            printf("middle task Running!
    ");
            OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err);   //延时1s
        }
    }
    
    //低优先级任务的任务函数
    void low_task(void *p_arg)
    {   
        static u32 times;
        OS_ERR err;
        while(1)
        {
            OSMutexPend(&TEST_MUTEX,0,OS_OPT_PEND_BLOCKING,0,&err);//请求互斥信号量
            printf("low task Running!
    ");
            for(times=0;times<5000000;times++)
            {
                OSSched();                                      //发起任务调度
            }
            OSMutexPost(&TEST_MUTEX,OS_OPT_POST_NONE,&err);     //释放互斥信号量    
            OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err);   //延时1s
        }
    }

    实验现象
    1

  • 相关阅读:
    NHibernate+MySql (erro 解决方法)
    Android详细的对话框AlertDialog.Builder使用方法
    Android调用WebService
    android连接Mysql数据库之JDBC方式
    ASP.NET MVC 过滤器Filter
    C#动态生成html页
    MVC中提示错误:从客户端中检测到有潜在危险的 Request.Form 值的详细解决方法
    Asp.Net生命周期系列三
    Asp.Net生命周期系列二
    Asp.Net生命周期系列一
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709495.html
Copyright © 2011-2022 走看看