优先级反转以及解决方法(基于SylixOS)

1.优先级反转

#include <stdio.h>
#include <pthread.h>
#include <SylixOS.h>

static LW_HANDLE  _G_Block;
static long long  _G_Count = 0;

PVOID handler1 (PVOID pvarg)
{
    int ret, i;

    for (i = 0; i < 3000; ++i) {
        printf("Low thread running.\n");
    }

    ret = Lw_SemaphoreB_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
    if (ret != ERROR_NONE) {
        return (LW_NULL);
    }

    while (1) {
        _G_Count++;
        printf("handler1:count = %lld\n", _G_Count);

        if (_G_Count > 100000000) {
            return (LW_NULL);
        }
    }

    Lw_SemaphoreB_Post(_G_Block);

    return (LW_NULL);
}

PVOID handler2 (PVOID pvarg)
{
    int ret;
    int i;

    for (i = 0; i < 1000; ++i) {
        printf("High thread running.\n");
    }

    sleep(5);

    ret = Lw_SemaphoreB_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
    if (ret != ERROR_NONE) {
        return (LW_NULL);
    }

    _G_Count++;
    printf("handler2:count = %lld\n", _G_Count);

    Lw_SemaphoreB_Post(_G_Block);

   return (LW_NULL);
}

PVOID handler3 (PVOID pvarg)
{
    long long t = 0;

    while (1) {
        t++;
        printf("handler3:t = %lld\n", t);
    }

    return (LW_NULL);
}
int main (int argc, char **argv)
{
    LW_HANDLE  hThread1;
    LW_HANDLE  hThread2;
    LW_HANDLE  hThread3;

    _G_Block = Lw_SemaphoreB_Create("count_lock",
                                    LW_TRUE,
                                    LW_OPTION_WAIT_PRIORITY |
                                    LW_OPTION_OBJECT_LOCAL,
                                    LW_NULL);
    if (_G_Block == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create semaphoreB\n");
        return (PX_ERROR);
    }

    hThread1  = Lw_Thread_Create("handler_1", handler1, LW_NULL, LW_NULL);

    if (hThread1 == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create handler_1");
        return (PX_ERROR);
    }

    hThread2  = Lw_Thread_Create("handler_2", handler2, LW_NULL, LW_NULL);

    if (hThread2 == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create handler_2");
        return (PX_ERROR);
    }

    sleep(10);

    hThread3  = Lw_Thread_Create("handler_3", handler3, LW_NULL, LW_NULL);

    if (hThread3 == LW_OBJECT_HANDLE_INVALID) {
           printf("fail to create handler_3");
           return (PX_ERROR);
    }

    Lw_Thread_SetPriority(hThread1, 220);
    Lw_Thread_SetPriority(hThread2, 205);
    Lw_Thread_SetPriority(hThread3, 210);

    Lw_Thread_Join(hThread1, LW_NULL);
    Lw_Thread_Join(hThread2, LW_NULL);
    Lw_Thread_Join(hThread3, LW_NULL);

    Lw_SemaphoreB_Delete(&_G_Block);
    return (PX_ERROR);
}

2.解决方法之一

#include <stdio.h>
#include <pthread.h>
#include <SylixOS.h>

static LW_HANDLE  _G_Block;
static long long  _G_Count = 0;

PVOID handler1 (PVOID pvarg)
{
    int ret, i;

    for (i = 0; i < 300; ++i) {
        printf("Low thread running.\n");
    }

    ret = Lw_SemaphoreM_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
    if (ret != ERROR_NONE) {
        return (LW_NULL);
    }

    while (1) {
        _G_Count++;
        printf("Low: count = %lld\n", _G_Count);

        if (_G_Count > 100000000) {
            return (LW_NULL);
        }
    }

    Lw_SemaphoreM_Post(_G_Block);

    return (LW_NULL);
}

PVOID handler2 (PVOID pvarg)
{
    int ret;
    int i;

    for (i = 0; i < 1000; ++i) {
        printf("High thread running.\n");
    }

    sleep(2);

    ret = Lw_SemaphoreM_Wait(_G_Block, LW_OPTION_WAIT_INFINITE);
    if (ret != ERROR_NONE) {
        return (LW_NULL);
    }
    while (1) {
        _G_Count++;
        printf("High: count = %lld\n", _G_Count);
    }

    Lw_SemaphoreM_Post(_G_Block);

   return (LW_NULL);
}

PVOID handler3 (PVOID pvarg)
{
    long long t = 0;

    while (1) {
        t++;
        printf("mid: t = %lld\n", t);
    }

    return (LW_NULL);
}
int main (int argc, char **argv)
{
    LW_HANDLE  hThread1;
    LW_HANDLE  hThread2;
    LW_HANDLE  hThread3;

    LW_CLASS_THREADATTR threadattr;

    _G_Block = Lw_SemaphoreM_Create("count_lock",
                                    LW_PRIO_HIGH,
                                    LW_OPTION_WAIT_FIFO |
                                    LW_OPTION_OBJECT_LOCAL |
                                    LW_OPTION_INHERIT_PRIORITY,
                                    LW_NULL);
    if (_G_Block == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create semaphoreB\n");
        return (PX_ERROR);
    }

    Lw_ThreadAttr_Build(&threadattr,
                        4 * LW_CFG_KB_SIZE,
                        LW_PRIO_NORMAL + 1,
                        LW_OPTION_THREAD_STK_CHK,
                        LW_NULL);
    hThread1  = Lw_Thread_Create("t_low", handler1, &threadattr, LW_NULL);
    if (hThread1 == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create handler_1");
        return (PX_ERROR);
    }

    Lw_ThreadAttr_Build(&threadattr,
                        4 * LW_CFG_KB_SIZE,
                        LW_PRIO_NORMAL - 1,
                        LW_OPTION_THREAD_STK_CHK,
                         LW_NULL);
    hThread2 = Lw_Thread_Create("t_high", handler2, &threadattr, LW_NULL);
    if (hThread2 == LW_OBJECT_HANDLE_INVALID) {
        printf("fail to create handler_2");
        return (PX_ERROR);
    }

    sleep(5);

    Lw_ThreadAttr_Build(&threadattr,
                        4 * LW_CFG_KB_SIZE,
                        LW_PRIO_NORMAL,
                        LW_OPTION_THREAD_STK_CHK,
                        LW_NULL);

    hThread3 = Lw_Thread_Create("t_mid", handler3, &threadattr, LW_NULL);
    if (hThread3 == LW_OBJECT_HANDLE_INVALID) {
       printf("fail to create handler_3");
       return (PX_ERROR);
    }

//    Lw_Thread_SetPriority(hThread1, 220);
//    Lw_Thread_SetPriority(hThread2, 205);
//    Lw_Thread_SetPriority(hThread3, 210);

    Lw_Thread_Join(hThread1, LW_NULL);
    Lw_Thread_Join(hThread2, LW_NULL);
    Lw_Thread_Join(hThread3, LW_NULL);

    Lw_SemaphoreM_Delete(&_G_Block);
    return (PX_ERROR);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值