优先级翻转场景设计及实现(C语言实现)

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>

pthread_t th_flag;
pthread_attr_t pattr[5];
pthread_mutex_t mutex;
sem_t lock;
INT priority_flag;
INT priority;
INT count = 0;

void delayManu(){
    int i, j;
    for(i=0; i<10000; i++)
        for(j=0; j<5000; j++);
}

static void *start_routine1(void *arg){

    while(1){
        sem_wait(&lock);

        delayManu();
        count++;
//        fprintf(stdout, "thread:%ld  priority: %d  count = %d\n",(long)arg, pattr[(long)arg].schedparam.sched_priority, count);
        sem_post(&lock);

        pthread_getschedprio(pthread_self(), &priority);
        fprintf(stdout, "thread:%ld  priority: %d  count = %d\n",(long)arg, priority, count);
    }

    return 0;
}

static void *start_routine2(void *arg){

    while(1){
        delayManu();
        count++;
        pthread_getschedprio(pthread_self(), &priority);
        fprintf(stdout, "thread:%ld  priority: %d  count = %d\n",(long)arg, priority, count);

//        API_ThreadgetPriority(API_ThreadIdSelf(), &priority);
    }
    return 0;
}

static void *start_routine3(void *arg){
    while(1){
        pthread_mutex_lock(&mutex);

        delayManu();
        count++;
        pthread_getschedprio(pthread_self(), &priority);
        fprintf(stdout, "thread:%ld  priority: %d  count = %d\n",(long)arg, priority, count);
//        fprintf(stdout, "thread:%ld  priority: %d  count = %d\n",(long)arg, pattr[(long)arg].schedparam.sched_priority, count);

        pthread_mutex_unlock(&mutex);
    }

    return 0;
}

int test1(){
    pthread_t th1, th2, th3;

    sem_init(&lock, 1, 1);                                              /* 使用匿名信号量实现优先级反转 */

    if ((pthread_attr_init(&pattr[1]) != 0) ||
        (pthread_attr_init(&pattr[2]) != 0) ||
        (pthread_attr_init(&pattr[3]) != 0))
        goto _error1_;

    /*
     * 优先级 th1 < th2 < th3
     */
    pattr[1].schedparam.sched_priority = 5;
    pattr[2].schedparam.sched_priority = 6;
    pattr[3].schedparam.sched_priority = 7;

    /*
     * 创建线程 th1、th2、th3,同时将线程标记传入线程函数内完成信息打印
     */
    pthread_create(&th1, &pattr[1], start_routine1, (void *)1);

    sleep(2);
    pthread_create(&th2, &pattr[2], start_routine2, (void *)2);

    sleep(2);                                                           /* 阻塞th3运行,保证th2抢占th1  */
    pthread_create(&th3, &pattr[3], start_routine1, (void *)3);

    printf("test1 end!\n");

    return  (0);

_error1_:
    fprintf(stderr, "initialize failed\n");
    return -1;
}

int test2(){
    pthread_t __unused th1, th2, th3, th4;
    pthread_mutexattr_t mutexattr;
    int prioceiling = 100;

    if(pthread_mutexattr_init(&mutexattr))
        goto _error2_;
    pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_NONE);     /* 优先级继承,按先入先出顺序等待 */
//    pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_INHERIT);  /* 优先级继承,按优先级顺序等待   */
//    pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_PROTECT);  /* 优先级天花板                   */

    pthread_mutex_init(&mutex, &mutexattr);

//    pthread_mutex_setprioceiling(&mutex, prioceiling);
    pthread_mutex_getprioceiling(&mutex, &prioceiling);
    fprintf(stdout, "mutex prioceiling is: %d\n", prioceiling);

    if(pthread_mutexattr_destroy(&mutexattr))
        goto _error2_;

    if ((pthread_attr_init(&pattr[1]) != 0) ||
        (pthread_attr_init(&pattr[2]) != 0) ||
        (pthread_attr_init(&pattr[3]) != 0) ||
        (pthread_attr_init(&pattr[4]) != 0))
        goto _error2_;

    /*
     * 优先级 th1 < th2 < th3,实现 th2 抢占 th1 ,以此阻塞 th3
     */
    pattr[1].schedparam.sched_priority = 5;
    pattr[2].schedparam.sched_priority = 6;
    pattr[3].schedparam.sched_priority = 7;
    pattr[4].schedparam.sched_priority = 7;

    /*
     * 创建线程 th1、th2、th3、th4,同时将线程标记传入线程函数内完成信息打印
     */
    pthread_create(&th1, &pattr[1], start_routine3, (void *)1);

    sleep(2);
    pthread_create(&th2, &pattr[2], start_routine2, (void *)2);

    sleep(2);                                                           /* 阻塞th3运行,保证th2抢占th1  */
    pthread_create(&th3, &pattr[3], start_routine3, (void *)3);

    sleep(2);
    pthread_create(&th4, &pattr[4], start_routine3, (void *)4);

    pthread_mutex_destroy(&mutex);

    printf("test2 end!\n");
    return 0;

_error2_:
    fprintf(stderr, "initialize failed\n");
    return -1;
}

int main (int argc, char **argv)
{
    pid_t pid;

//    iCpuMax = sysconf(_SC_NPROCESSORS_ONLN);
    pid = getpid();

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(0, &cpuset);

    pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); //线程绑核
    sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset);                 //进程绑核


//    test1();                                                            /* 优先级反转 - 场景模拟        */
    test2();                                                            /* 优先级反转 - 解决方案        */
//    pthread_attr_getschedpolicy()
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值