3

I have some doubts about setting the policy of a thread and how that policy is going to be followed while it is executing. Pthread allows setting the scheduling policy of a thread to SCHED_FIFO/SCHED_RR/SCHED_OTHER. I am trying to understand how this user-set-policy works as the Linux kernel uses CFS as the default scheduler policy. Will the user-set-policy is going to be overriden to CFS when it is executing? If so , what is the use of pthread schedling policy?

0
+50

A/ BASIC THEORY 3: CFS is not the default "scheduler policy" under Linux. CFS is the default scheduler under linux.
A scheduler chooses among all existing threads those to which cpu time should be granted.
This choice is governed by miscellaneous parameters that are taken into account differently depending on the scheduling policy of the threads.
All threads get a scheduling policy.
Default scheduling policy under CFS is known as : SCHED_OTHER also sometimes labeled SCHED_NORMAL.
This policy actually instructs the scheduler to take the nice value into account and ensure fairness among all threads running under this policy.

B/ RUN TIME : 1 Every tick (or whatever dedicated interrupt) the scheduler maintains (reorders) a list (a queue) of runable threads according to their associated scheduling policy and other parameters depending on that policy. When the reordering is over, the thread on the top of the queue will be the one elected.
Threads belonging to "real-time" policies (SCHED_RR / SCHED_FIFO) (if any in a runable situation) will always be at the top of the list. Order, among them, being governed by the real-time priority setting.

C: YOUR QUESTION : If, in these conditions, you change the scheduling policy of some given thread (more precisely : if some running thread issue a system call requesting the change of it's scheduling policy 2) then, provided it gets the rights to do so, the scheduler will reorder its queue accordingly.
If, for example, some SCHED_OTHER thread changes to SCHED_RR, it will enter the top of the list, the scheduler will ignore it's nice value and order it, among other SCHED_RR threads according to its given realtime priority.

BTW, if that was part of your questioning :

  • The scheduler never decides / forces the scheduling policy of threads.
  • The scheduler never changes depending on scheduling policies. If CFS has been chosen at boot time, CFS will always be THE scheduler. One can always opt for other schedulers, some consisting in CFS patches, others written from scratch, each of them claiming lesser overhead / better handling of nice values / more efficient handling of the SCHED_RR scheduling policy / more efficient when MAX_CORES <= 4, etc. But whatever scheduler you boot with, will be kept as the only program scheduling threads until shudown.
  • In any case, the scheduler adapts its behaviour according to scheduling policies afforded to threads by (most of the time) their parent and, more rarely by themself.

1 : This shall be considered in a single core environment.
It could be extended to whatever SMP / SMP + HT environment at the cost of extra complexity for the understanding because of the possibility to share (or not) queues between cores and to allow threads to run on all / some specific set of available cores.

2 : The family of system calls to use will depend on the API used.
sched_setscheduler() as the standard way, pthread_setschedparam() when using the POSIX API. (function names differ but results (the impact on CFS) are identical)

3 : For a detailed description of each available scheduling policy, please refer to the sched(7) Linux manual page (man sched.7), which, I make no doubt about it, is the most trustable/reputable source you are looking for.

| improve this answer | |
  • 1
    Thanks for the answer. The answer makes things clear for me. I believed CFS is for NORMAL priority threads/process and there is a core scheduler which takes care of RT threads. Now I understand and conclude like this, CFS is going to be the one and only scheduler(if selected at the boot time) , though CFS' default policy is SCHED_NORMAL, it takes care of the RT(SCHED_FIFO, SCHED_RR) also. – Franc M Aug 9 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.