1

I know linux scheduler will schedule the task_struct which is a thread. Then if we have two processes, e.g., A contains 100 threads while B is single thread, how can the two processes be scheduled fairly, considering if each thread would be scheduled fairly?

In addition, so in Linux, context switch between threads from the same process would be faster than that between threads from different processes, right? Since the latter will have something to do with process control block while the former wouldn't.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Hao Shen
  • 2,605
  • 3
  • 37
  • 68
  • Consider looking at the documentation, https://www.kernel.org/doc/Documentation/scheduler/sched-design-CFS.txt it has very clear description of how CFS works. – rakib_ Jan 16 '14 at 06:15
  • @rakib Thanks for your reply. But I didn't see anything that can answer my question in the page :( – Hao Shen Jan 16 '14 at 15:45
  • Ok, see my answer, hope it'll help. – rakib_ Jan 16 '14 at 16:47
  • possible duplicate of [Would process with more threads on linux have more cpu time than process with one thread?](http://stackoverflow.com/questions/12713880/would-process-with-more-threads-on-linux-have-more-cpu-time-than-process-with-on) – ninjalj Jan 16 '14 at 16:53
  • The second part: _context switch between threads from the same process would be faster than that between threads from different processes_ should be on a separate question. – ninjalj Jan 16 '14 at 16:54

1 Answers1

1

The point you are missing here is, how scheduler looks at threads or tasks. Well, the Linux kernel scheduler will treat them as individual scheduling entity, therefore will be counted and scheduled differently.

Now let's see what CFS documentation says - it has a simplistic approach of giving out even slice of CPU time to each runnable process, therefore, if there are 4 runnable process/threads they'll get 25% of cpu time each. But on real hardware it's not possible and to fix the issue vruntime was introduced (take more on this from here

Now come back to your example, if process A creates 100 threads and B creates 1 thread then the # of running processes or threads becomes 103 (assuming all are runnable state) then CFS will evenly share the cpu using formula 1/103 (cpu/number of running tasks). And the context switching is same for all the scheduling entities, threads only shares task's internal mm_struct and when they run they have their own sets of registers, task status to load up to start with. Hope this will help to understand better.

rakib_
  • 136,911
  • 4
  • 20
  • 26
  • @rabik So you mean A and B will not be treated fairly just because A has more threads? I am not sure. But this looks not fair as B will starve A. – Hao Shen Jan 16 '14 at 16:52
  • I said, threads or processes whatever, scheduler will not treat them separately, therefore they're same. If they're same, now apply CFS formula on it and think, everything will be clear. – rakib_ Jan 16 '14 at 16:57
  • @rakib_ Is the example correct? I think # of running is not 103 but 101 because there is only tasks in linux, so that we don't have to count process and thread differently – obanadingyo Oct 15 '22 at 12:18