5

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?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Not sure where this question would fit best: here on SO, on [Programmers SE](http://programmers.stackexchange.com), or on [Computer Science SE](http://cs.stackexchange.com/). – stakx - no longer contributing Oct 12 '14 at 11:37
  • @stakx linked question is also raised in SO. – overexchange Oct 12 '14 at 11:38
  • @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 Oct 12 '14 at 11:43
  • 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 Oct 12 '14 at 11:45
  • For starters the kernel does the scheduling and so needs to know about all threads on the system – James Oct 12 '14 at 11:57
  • @James I did not understand your update. Is OS scheduler aware of user level thread? Because i read that:`POSIX thread IDs are not the same as the thread IDs returned by the Linux specific gettid() system call...each POSIX thread has a unique kernel thread ID in the Linux NPTL threading implementation` – overexchange Oct 12 '14 at 12:03
  • @James true for real threads, but the trend is raising where the same concepts of multi-threading (or separate stacks) are now being applied on application level. Think about **async** methods in C# or **promises** in Javascript. I don't think there is any real scheduling involved, just simple round-robin is my best guess. It can improve performance by utilizing as much of the scheduled thread time as possible and pile multiple OS callback requests that can be all be handled in a single context switch instead of queueing them between sequential OS-Application contexts. – Louis Somers Dec 18 '16 at 22:31
  • @LouisSomers i don't get your point. Async lets a userland thread continue with other work until it's notified that something it previously requested to be done has completed. This notification will (usually) come from a real kernel thread, for example async i/o – James Dec 19 '16 at 01:07
  • @James I was referring to your remark that the kernel does the scheduling and needs to know about all threads. In userland a process can also do it's own "multithreading", (park a stack-frame and continue on a parallel stack). In fact, many JVM implementations do this in userland (usually where the OS does not support multi-threading). So the kernel does not always need to know about all "threads". Off course, it depends on how you define the concept of a "thread". In userland it may not be easy to use a hardware interrupt to schedule stuff. Never mind, it's not a big deal. – Louis Somers Dec 19 '16 at 08:23

2 Answers2

4

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.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • what is the syscall to choose any of the above three models? In N-1 threading model, How/Who distribute single CPU slice to multiple user level threads? – overexchange Oct 12 '14 at 14:34
4

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.

nif
  • 3,342
  • 20
  • 18
  • So, Is non-multi threaded user process a kernel thread to a kernel? Is kernel thread a separate data structure apart from PCB(process control block) of that user process? when, I say, `Thread.dumpStack()` in java for a non-multi-threaded application, Does it refer to same kernel thread? – overexchange Oct 12 '14 at 12:57
  • 1
    Yes, to the first question, if you don't mean the _real_ kernel thread. Java VMs may implement threading support in [different ways](http://stackoverflow.com/questions/2816011/what-is-the-jvm-scheduling-algorithm). On Linux most implementations will probably use `pthreads`. AFAIK, OpenJDK just spawns a thread using `pthread` if you create a Java thread (or you are on the main thread). Each process on Linux has at least one LWP thread. You can see them with the `ps -eLf` command. Can't answer the second question though, as I don't know the internal representation of LWPs in the kernel. – nif Oct 12 '14 at 13:23
  • So, If POSIX `pthread` or Windows `CreateThread()` is used to spawn a thread within a user process, How do i know, if my muti-threaded process is running under N-1 model or 1-1 model? Do i have control(thru programming in my user program) over switching between these models? – overexchange Oct 12 '14 at 13:32
  • 1
    In Java: No, you don't (except maybe in JNI libraries). In lower-level languages, e.g. C: Yes, you are free to implement a custom scheduling algorithm in user space (or use some library other than `pthreads`). To the kernel your process looks like having just one thread. You can't use a 1-1 model if your kernel doesn't support it though, since it must be able to schedule multiple threads in a single process directly. – nif Oct 13 '14 at 08:07
  • Can you provide some existing resource link in C, that does some custom scheduling algorithm in user space? I am wondering, how threads are created and scheduled within user code without the use of kernel calls. – overexchange Oct 13 '14 at 09:04
  • Check out [pth](http://www.gnu.org/software/pth/), they seem to implement user space threading. – nif Oct 13 '14 at 11:07
  • So, Basically we can think of N-1 threading model, only when user space programmer implements the threading functionality without the help of kernel(system) calls. So, that means kernel will know that only one kernel thread is required to this user process.am i correct? – overexchange Oct 13 '14 at 15:37