Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are dynamic priorities of threads computed in Linux 2.6.x?

A thread (or task) will loss dynamic priority by using a lot of cpu and gain priority by using less of CPU. How exactly are these priorities computed for n threads (using normal scheduling policy SCHED_OTHER )?

like image 333
Shan Avatar asked Aug 29 '14 23:08

Shan


People also ask

What is the priority of threads in Linux?

Threads scheduled with this “real-time” policy can be assigned a priority (under linux) in the range 1..99 with 99 representing the highest priority. Since ordinary, “non-real time” processes execute at priority 0 (nice(1) modifies a “dynamic priority” which only…

How to use POSIX thread priority scheduling under Linux?

How To Use POSIX Thread Priority Scheduling under Linux - EPICS Controls The linux scheduler supports the SCHED_FIFO scheduling policy defined by POSIX.1-2001. Threads scheduled with this “real-time” policy can be assigned a priority (under linux) in the range 1..99 with 99 representing the highest priority.

Can two threads have the same priority?

We know that a thread with high priority will get preference over lower priority threads when it comes to the execution of threads. However, there can be other scenarios where two threads can have the same priority.

Does the thread scheduler schedule the threads according to priority?

In most cases, the thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses.


1 Answers

Quoting from this document, section 5.4.2:

The Linux 2.6.8.1 scheduler rewards I/O-bound tasks and punishes CPU-bound tasks by adding or subtracting from a task’s static priority. The adjusted priority is called a task’s dynamic priority, and is accessible via the task’s prio variable (e.g. p->prio where p is a task). If a task is interactive (the scheduler’s term for I/O bound), its priority is boosted. If it is a CPU hog, it will get a penalty. In the Linux 2.6.8.1 scheduler, the maximum priority bonus is 5 and the maximum priority penalty is 5. Since the scheduler uses bonuses and penalties, adjustments to a task’s static priority are respected. A mild CPU hog with a nice value of -2 might have a dynamic priority of 0, the same as a task that is neither a CPU nor an I/O hog.

I feel this is a fair explanation. The priority is computed based on whether it is a CPU bound thread or an I/O bound one. And regarding what you mentioned in the question, that gain priority by using less of CPU is rather gain priority by being interactive(I/O bound). I hope this excerpt answers your query...

like image 58
Stark07 Avatar answered Oct 27 '22 00:10

Stark07