Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does OS scheduler schedule threads that belong to two different processes (tasks)? [closed]

As far as all books say, switching between tasks/processes is more expensive than switching between threads of the same process/task. If so, then scheduler of threads-aware OS should schedule threads in such way that the threads of the same process/task should be executed next to each other (grouped) and not interleaved with threads from other processes/tasks.

As I am reading books on OSes, all books just stop at stating that threads switching is less expensive than process switching. And that's it. No book tells how exactly scheduler is solving problem of avoiding switching between threads of different tasks. As if such problem does not exist or is trivial to each and every reader.

Is my understanding of the problem not correct? Or am I missing something? Why such huge topic of possible performance degrade is not covered in each and every OS book in "Scheduling" chapter? Am I reading wrong books?

like image 580
kachanov Avatar asked Jun 01 '12 07:06

kachanov


People also ask

Does the OS schedule threads or processes?

A user-level thread is a thread that the OS does not know about. The OS only knows about the process containing the threads. The OS only schedules the process, not the threads within the process.

How can the OS regain control of the CPU so that it can switch between processes?

HARDWARE SUPPORT: THE TIMER INTERRUPT The addition of a timer interrupt gives the OS the ability to regain control of the CPU even if processes act in a non- cooperative fashion. Thus, this hardware is key in helping the OS maintain control of the system.

How is thread scheduling done?

Scheduling of threads involves two boundary scheduling, Scheduling of user level threads (ULT) to kernel level threads (KLT) via lightweight process (LWP) by the application developer. Scheduling of kernel level threads by the system scheduler to perform different unique os functions.

How does an OS ensure that a process runs for a certain length of time and then it stops executing?

By limiting each task to a certain amount of time, the operating system can ensure that it can cycle through all ready tasks, giving each one a chance to run. With round robin scheduling, interactive performance depends on the length of the quantum and the number of processes in the run queue.


1 Answers

In my opinion this would be a dangerous optimization, because if the scheduler favored threads based on whether or not the process memory pages are already loaded two things would happen:

  1. Newer processes would be starved.
  2. It would allow a process to keep spawning threads in order to stay on the CPU.

The scheduler's main priorities are:

  1. I/O responsiveness - i.e. I/O bound threads preempt CPU-bound threads.
  2. Fairness - try to ensure that starvation is limited.
  3. Low latency - make sure each process can finish in a reasonable amount of time.

It's pretty easy to see that these 3 conditions conflict with the mentioned optimization.

like image 55
Tudor Avatar answered Oct 14 '22 06:10

Tudor