Search code examples
multithreadingoperating-systemkernelpthreadsuserspace

Thread in user-space schedulling dilemma


Recently I've started reading (again) Tanenbaum's 'Modern Operating Systems 4ed' and I am a little stuck in Chapter 2 that discusses processes and threads. Especially I am confused about user-space threads sub-chapter that lists all advantages and disadvantages of them.

Here is a direct quote in favour of user-space threads:

User-level threads also have other advantages. They allow each process to have its own customized scheduling algorithm. For some applications, for example, those with a garbage-collector thread, not having to worry about a thread being stopped at an inconvenient moment is a plus. They also scale better, since kernel threads invariably require some table space and stack space in the kernel, which can be a problem if there are a very large number of threads.

The way I understand it is that every process that is using threads (therefore creates them/destroy) is responsible for implementing scheduling of these threads in order for them to be efficient. Simple round-robin with predefined short time interval for example.

However later in the sub-chapter we get a quote for disadvantages. Here it comes:

Another problem with user-level thread packages is that if a thread starts running, no other thread in that process will ever run unless the first thread voluntarily gives up the CPU. Within a single process, there are no clock interrupts, making it impossible to schedule processes round-robin fashion (taking turns). Unless a thread enters the run-time system of its own free will, the scheduler will never get a chance.

Hmmm, WTF? So in pros-part there it's an advantage to have process' OWN scheduler for threads and here it seems that no matter that there is customised scheduler - it won't work as expected becuase threads are greedy.

My question is: Is it impossible for process' scheduler when using user-space threads to actually schedule threads in equal manner? Or is my English not good enough to grasp a point here.

PS. When reviewing posted question I was suggested to read this - User-level threads for threading. Isn't it right with a statement that Tanenbaum's model was applicable to older systems? The book was released in 2014 so that might be a thing.


Solution

  • The scheduling algorithm is the algorithm that chooses from among the set of runnable threads, which one should run next. This is the part that you can design however you like in your userspace thread system.

    The text in the "disadvantage" section is talking about when the scheduling algorithm gets to run, and is saying that with userspace threads you are generally limited to that only happening when the currently running thread voluntarily enters the scheduler.

    So the advantage is referring to what happens when the scheduler runs and the disadvantage is referring to when the scheduler gets to run.