为什么线程没有在linux中以正确的优先级运行

时间:2022-04-30 02:18:32

I have following code, which changes priority of current thread. I passed 90 as parameter, yet it looks like thread is running with priority 19. I have:

我有以下代码,它改变了当前线程的优先级。我作为参数传递了90,但看起来线程以优先级19运行。我有:

  • checked that ulimit -r is set to 99
  • 检查ulimit -r是否设置为99

  • process is running as root
  • 进程以root身份运行

How do I know that process is running with priority 19. I executed following command. As you can see pri is 19. Also what is the difference between rtprio and pri? I am using 2.6 kernel from redhat enterprise linux ver 6.3. Since, this is not a real time linux, is this line valid in code if(pthread_setschedparam(pthread_self(), SCHED_RR, &param)) as i am setting scheduler to SCHED_RR

我如何知道该进程以优先级19运行。我执行了以下命令。你可以看到pri是19.另外rtprio和pri有什么区别?我从redhat enterprise linux ver 6.3使用2.6内核。因为,这不是一个实时的linux,这行在代码中有效if(pthread_setschedparam(pthread_self(),SCHED_RR,&param))因为我正在将调度程序设置为SCHED_RR

ps -p 10834 -o pid,tid,class,rtprio,ni,pri,psr,pcpu,stat

PID   TID CLS RTPRIO  NI PRI PSR %CPU STAT
10834 10834 TS       -   0  19   9 99.9 R+
void changePriority(int tPriority)
{
    int  policy;
    struct sched_param param;

    pthread_getschedparam(pthread_self(), &policy, &param);
    param.sched_priority = tPriority;
    if(pthread_setschedparam(pthread_self(), SCHED_RR, &param))
            err_sys("error while setting thread priority to %d", tPriority);
}

1 个解决方案

#1


1  

Referring PRI vs. RTPRIO, verbatim from the man ps:

引用PRI与RTPRIO,逐字逐句地来自man ps:

PRI priority of the process. Higher number means lower priority

PRI优先级的过程。数字越大意味着优先级越低

...

RTPRIO realtime priority.

RTPRIO实时优先级。


PRI could be set using the nice() system call or the nicecommand line tool. PRI is the process' main thread's priority. For Linux this also sets the priority for the process' non real-time threads*.

可以使用nice()系统调用或nicecommand行工具设置PRI。 PRI是进程的主线程优先级。对于Linux,这也为进程的非实时线程*设置了优先级。

From man 1 nice:

从男人1好:

Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).

不错的范围从-20(最有利的调度)到19(最不利的)。

Niceness and PRI are synonyms.

Niceness和PRI是同义词。

*A real-time thread is a thread with a scheduling policy different from SCHED_OTHER, SCHED_BATCH and SCHED_IDLE, as there are: SCHED_FIFO and SCHED_RR.

*实时线程是一个调度策略与SCHED_OTHER,SCHED_BATCH和SCHED_IDLE不同的线程,因为它有:SCHED_FIFO和SCHED_RR。


RTPRIO and scheduling policy could be set via the functions mentioned in the OP.

可以通过OP中提到的功能设置RTPRIO和调度策略。

The scheduling policy is guaranteed to provide at least 32 different levels. Use sched_get_priority_min and sched_get_priority_max to determine the implemented range.

保证调度策略提供至少32个不同的级别。使用sched_get_priority_min和sched_get_priority_max来确定实现的范围。

#1


1  

Referring PRI vs. RTPRIO, verbatim from the man ps:

引用PRI与RTPRIO,逐字逐句地来自man ps:

PRI priority of the process. Higher number means lower priority

PRI优先级的过程。数字越大意味着优先级越低

...

RTPRIO realtime priority.

RTPRIO实时优先级。


PRI could be set using the nice() system call or the nicecommand line tool. PRI is the process' main thread's priority. For Linux this also sets the priority for the process' non real-time threads*.

可以使用nice()系统调用或nicecommand行工具设置PRI。 PRI是进程的主线程优先级。对于Linux,这也为进程的非实时线程*设置了优先级。

From man 1 nice:

从男人1好:

Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable).

不错的范围从-20(最有利的调度)到19(最不利的)。

Niceness and PRI are synonyms.

Niceness和PRI是同义词。

*A real-time thread is a thread with a scheduling policy different from SCHED_OTHER, SCHED_BATCH and SCHED_IDLE, as there are: SCHED_FIFO and SCHED_RR.

*实时线程是一个调度策略与SCHED_OTHER,SCHED_BATCH和SCHED_IDLE不同的线程,因为它有:SCHED_FIFO和SCHED_RR。


RTPRIO and scheduling policy could be set via the functions mentioned in the OP.

可以通过OP中提到的功能设置RTPRIO和调度策略。

The scheduling policy is guaranteed to provide at least 32 different levels. Use sched_get_priority_min and sched_get_priority_max to determine the implemented range.

保证调度策略提供至少32个不同的级别。使用sched_get_priority_min和sched_get_priority_max来确定实现的范围。