kernel threads

Robert Watson rwatson at freebsd.org
Tue Jan 27 14:47:47 PST 2004


On Tue, 27 Jan 2004, Renaud Molla Wanadoo wrote:

> I'm trying to use the kthread library under 5.2-RELEASE but can't
> compile my program (which actually only tries to create a thread). 
> 
> I've read that there is now KSE to create kernel threads, but i am
> wondering if it could be used within the kernel code. 

I'm left a little unclear by your message what it is you're trying to do. 
In traditional parlance, a "kernel thread" is a thread executing kernel
code in the kernel.  These are created using the kthread(9) API, which is
available both to kernel modules and code compiled directly into the
kernel.  You can see examples of kthread use (both compiled in and in
modules) by grepping in the src/sys/kern and src/sys/dev/* trees.  The
only real caveat here that I know if is that you need to grab the Giant
lock if your thread will use it, since kthreads don't start holding Giant,
and that if you call kthread_exit(), you will need to grab Giant before
that. 

A use of "kernel thread" popularized by linux is the idea of userspace
threads that are backed by a kernel schedulable thread, as opposed to
multiple userspace threads being mapped into a single thread making up a
single process. In FreeBSD 5.x, the "libc_r" library provides multiple
user threads multiplexed onto a single kernel-visible thread/process.
"libkse" and "libthr" provide M:N and 1:1 models.  By linking your
application against libkse or libthr and using the pthreads API, you will
automatically get parallelism and latency improvements over libc_r.

Hope this helps. 

Robert N M Watson             FreeBSD Core Team, TrustedBSD Projects
robert at fledge.watson.org      Senior Research Scientist, McAfee Research



More information about the freebsd-hackers mailing list