Linux kernel thread model

Dave Hylands dhylands at gmail.com
Thu Jun 16 17:54:21 EDT 2011


Hi Mulyadi,

On Thu, Jun 16, 2011 at 10:37 AM, Mulyadi Santosa
<mulyadi.santosa at gmail.com> wrote:
> On Thu, Jun 16, 2011 at 14:06, manish honap
> <manish_honap_vit at yahoo.co.in> wrote:
>> How they understand whether kernel part is scheduled or user part is scheduled ?
>
> not sure if I got your question correctly, but the one that is
> scheduled is the process..... 1:1 model means for single
> fork()/clone()/pthread_create() in user space, clone() syscall with
> various parameters are called in kernel space. Then, we end up
> creating single process. This represent that whole single thread.

A few clarifications. I like to think of a process as a memory space,
and a thread as a unit of execution.

Many processes have a single thread, but processes can have mutliple
threads as well.

Each thread has a kernel side and a user-mode side. The kernel mode
portion has its own stack which is separate from the user-mode stack,
but you can think of them all together as being a single thread.

You can view all of the processes in the system by using

ls -d /proc/[0-9]*

You can view all of the threads in the system by using

ls -d /proc/[0-9]*/task/*

Due to historical reasons, we have some confusing terminology which is
different between kernel space and user-space.
The numbers under the task directory are called tids in user-space,
but are called pids in kernel space (i.e. current->pid)
The numbers under the proc directory are called pids in user-space,
but are called tgid in kernel space (i.e. current->tgid)

When running in kernel space, the kernel component of the thread runs
(so the kernel thread-stack is used). When running in user-space, the
user component of the thread tuns (so the user-space stack is used).
When scheduling, you're scheduling the thread as a whole, and not the
kernel-side or user-side component.

If you write a driver or loadable module, you can create kernel
threads which have no user-mode portion, but you can't create a
user-mode thread which doesn't have a kernel side component.
kernel mode threads appear to be their own process, which is where my
process=memory space model break down, but for user space it holds.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com



More information about the Kernelnewbies mailing list