4
votes

Below are three threading models that i came across. Based on these below 3 architectures, It is new for me to understand that, there also exist something called kernel thread, apart from user thread which is introduced as part of POSIX.1C

This is 1-1 model

enter image description here

This is N-1 model.

enter image description here

This is Hybrid model.

enter image description here

I have been through many questions on SO for kernel threads. This looks more relevant link for clarification.

At process level, For every user process that is loaded by Linux loader(say), Kernel does not allocate corresponding kernel process for executing machine instructions that a user process has come up with. User process only request for kernel mode execution, when it require a facility from kernel module[like malloc()/fork()]. Scheduling of user process is done by OS scheduler and assign a CPU core.

For example, User process does not require kernel execution mode to execute an instruction

a=a+2;//a is my local variable in a user level C function

My question:

1) So, What is the purpose of kernel level thread? Why does OS need to maintain a kernel thread(additionally) for corresponding user thread of a User level process? Does User mode programmer have any control on choosing any of the above three threading models for a given User process through programming?

After i understand the answer to first question, one relevant supplementary is,

2) Does kernel thread actually get scheduled by OS scheduler but not user thread?

2
Not sure where this question would fit best: here on SO, on Programmers SE, or on Computer Science SE.stakx - no longer contributing
@stakx linked question is also raised in SO.overexchange
@stakx You can suggest me, if you see this question is suppose to be migrated to Programmers SE or CS SE? But am not sure of migration procedure.overexchange
My gut feeling is that your question would be better received on Programmers; but like I said, I am really not sure myself. (Perhaps there's no need to do anything for the moment.)stakx - no longer contributing
For starters the kernel does the scheduling and so needs to know about all threads on the systemJames

2 Answers

3
votes

I think the use of the word kernel thread is a bit misleading in these figures. I know the figures from a book about operating system (design) and if I remember correctly, they refer to the way how work is scheduled by the operating system. In the figures, each process has at least one kernel thread assigned that is scheduled by the kernel.

The N-1 model shows multiple user-land threads that are not known to the kernel at all because the latter schedules the process (or how it's called in the figure, a single kernel thread) only. So for the kernel, each process is a kernel thread. When the process is assigned a slice of processor time, it itself runs multiple threads by scheduling them at its own discretion.

In the 1-1 model, the kernel is aware of the user-land threads and each thread is considered for processor time assignment by the scheduler. So instead of scheduling a whole process, the kernel switches between threads inside of processes.

The hybrid model combines both principles, where lightweight processes are actually threads known to the kernel and which are scheduled for execution by it. Additionally, they implement threads the kernel is not aware of and assign processor time in user-land.

And now to be completely confused, there is actually a real kernel thread in Linux. But as far as I understand the concept, these threads are used for kernel-space operations only, e.g. when kernel modules need to do things in parallel.

3
votes

So, What is the purpose of kernel level thread?

To provide a vehicle for assignment of a set of resources provided by the OS. The set always incudes CPU code execution on a core. Others may include disk, NIC, KB, mouse, timers, as may be requested by syscalls from the thread. The kernel manages access to those resources as they become available and arbitrates between resource conflicts, eg. a request for KB input when none is available will remove CPU execution from the thread until KB input becomes available.

Why do we need a kernel thread(additionally) for corresponding user thread of a User level process?

Without a kernel-level thread, the user thread would not be able to obtain execution - it would be dead code/stack. Note that with Linux, the concept of threads/processes can get somewhat muddied, but nevertheless, the fundamental unit of execution is a thread. A process is a higher-level construct whose code must be run by at least one thread, (eg. the one raised by the OS loader to run code at the process entry point when it is first loaded).

Does User mode programmer have any control on choosing any of the above three threading models for a given User process through programming?

No, not without a syscall, which means leaving user mode.

Does kernel thread actually get scheduled by OS scheduler but not user thread

Yes - it is the only thing that gets to be given execution when it can use it, have execution removed when it cannot, and be subject to preemptive removal of CPU if the OS scheduler requires it for something else.