# kernel thread and user thread



## jronald (Nov 28, 2009)

In the book "UNIX Internals", it says:


> The kernel retains the responsibility for process switching, because it alone has the privilege
> to modify the memory management registers. User threads are not truly schedulable entities, and the
> kernel has no knowledge of them. The kernel simply schedules the underlying process or LWP,
> which in turn uses library functions to schedule its threads. When the process or LWP is preempted,
> ...


Is it still the case for freebsd?


----------



## aCOSwt (Nov 29, 2009)

I am not sure I fully understand your question.
If you mean does FreeBSD implement a 1:1 scheduling model, then the answer is :
- Yes since V6. (Although N:M was the default and recommended choice)
- 1:1 is the default and recommended choice since V7.
I know nothing about V8, but I read somewhere that N:M would no longer be supported.


----------



## jronald (Nov 29, 2009)

aCOSwt said:
			
		

> I am not sure I fully understand your question.
> If you mean does FreeBSD implement a 1:1 scheduling model, then the answer is :
> - Yes since V6. (Although N:M was the default and recommended choice)
> - 1:1 is the default and recommended choice since V7.
> I know nothing about V8, but I read somewhere that N:M would no longer be supported.



What do you mean by 1:1 or N:M?
The kernel schedules only processes, include processes, and it doesn't know about user thread. Is it still the case for FreeBSD nowadays?


----------



## aCOSwt (Nov 29, 2009)

jronald said:
			
		

> The kernel schedules only processes, include processes, and it doesn't know about user thread.


Well, this is not exactly right.
The kernel schedules only _scheduling entities_ which can be far smaller than an entire process.
To make short, let's call them _kernel-threads_.
In a 1:1 scheduling model such as the one priviledged under freeBSD 7, A single _user-thread _is associated to a single _kernel-thread_.
which means that, although the kernel knows nothing about user-threads, by scheduling a _kernel-thread_, a single user thread (i.e. not an entire process if this one is multi-threaded) will be scheduled.
I understand the part of the book you mention as dealing with a N:M scheduling model. In such a model, there can be more than one _user-thread _associated to a single _kernel-thread_.
"_The kernel simply schedules the underlying process or LWP_, [i.e kernel-thread] _which in turn uses library functions to schedule its threads. When the process or LWP is preempted, 
so are its threads._
In other words, all the user-threads associated with the kernel-thread will be scheduled when the kernel-thread is scheduled.
However, this does not mean that the whole user process will be scheduled. It will depend on how the userland part of the scheduler will have made the associations.
Anyway, this is no longer the priviledged approach under freeBSD since V7.


----------



## jronald (Nov 30, 2009)

aCOSwt said:
			
		

> Anyway, this is no longer the priviledged approach under freeBSD since V7.



Thanks a lot.
The priviledged approach is a little complicated,
I'm going to study the new ones in V7 and V8.


----------

