日期:2014-05-16  浏览次数:20598 次

如何修改主线程的调度策略
我现在想要将主线程的调度策略从默认的SCHED_OTHER修改为SCHED_RR,根据查阅的资料来看应该是调用pthread_setschedparam这个函数,但是我调用的时候要么没有设置成功,要么就是Invalid argument。我确定我是用root用户编译以及运行的程序,所以不能够理解,希望能够有人为我解答,谢谢。

代码如下:
C/C++ code

#include <stdio.h>
#include <pthread.h>
#include <sched.h>
main()
{
    struct sched_param param;
    int policy;

    if (pthread_getschedparam(pthread_self(), &policy, &param) != 0)
        perror("pthread_getschedparam:");

    printf("sched_priority:%d\n", param.sched_priority);

    if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) != 0)
        perror("pthread_setschedparam:");

    if (pthread_getschedparam(pthread_self(), &policy, &param) != 0)
        perror("pthread_getschedparam:");

    if (policy == SCHED_RR)
        printf("policy is SCHED_RR\n");
    else if (policy == SCHED_OTHER)
        printf("policy is SCHED_OTHER\n");
    else if (policy == SCHED_FIFO)
        printf("policy is SCHED_FIFO\n");
    else
        printf("policy is %d\n", policy);

    while (1)
    {
        printf("In while (1)\n");
        sleep(5);
    }

    return 0;
}



------解决方案--------------------
我在pthread.h里面看到这个int pthread_attr_setschedpolicy (pthread_attr_t *, int);
还试过,没百度过,不晓得干什么的
------解决方案--------------------
改变调度策略应该是要进入内核态吧
看看内核代码