5

From my understanding, a user thread is created by the user from library and managed in user space. A process can contain one or more user threads and the kernel is not aware of them.

So what is a kernel thread? Are they similar to processes or are they contained in processes similar to user thread?

Also, I saw diagrams of user threads mapping to kernel threads. How exactly does that work in terms of execution? Does the kernel schedule a kernel thread and execute the user threads mapped to that thread?

Instinct
  • 2,201
  • 1
  • 31
  • 45
  • "A process can contain one or more user threads and the kernel is not aware of them." Not really -- the kernel knows about user-land threads, that's how it can give them different IDs. (See Windows for example.) I think it may be more of a terminology thing, especially for *nix systems. – user541686 May 01 '13 at 16:51

2 Answers2

3

Yes, kernel threads are quite similar to processes. In fact, modern operating systems blur the distinction between threads and processes. In Linux, the clone system call can be used to create a thread (same PID, same address space, same file descriptor table, etc.) or a process (distinct PID, etc.) or anything in between.

(FreeBSD has a similar system call called rfork, which generalizes fork. I think the idea of unifying threads and processes originated in Plan 9.)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    Similar question to you sir, When a user thread makes a system call, does the kernel thread that is mapped to it get executed and returns the result of the system call through that kernel thread? – Instinct May 01 '13 at 16:52
  • @user1773070: depends on the OS, I guess. It sounds like a plausible implementation strategy, but I'm not really sure how Linux or other OSs do this. – Fred Foo May 01 '13 at 16:57
  • @Instinct Yep! That is why there is a one-to-one mapping between user and kernel threads in most modern OSes. "Green threads" / "fibers" are user-space-only threads (which break the one-to-one property), and usually they farm out their syscalls to a thread pool which is used only for I/O. The I/O threads each have an OS thread to match so that they can run system calls. – Dan Apr 11 '14 at 06:54
1

From here: Relationship between a kernel and a user thread

When they say map, they mean that each kernel thread is assigned to a certain number of user mode threads.

Kernel threads are used to provide privileged services to applications (such as system calls ). The are also used by the kernel to keep track of what all is running on the system, how much of which resources are allocated to what process, and to do scheduling.

If your applications make a heavy use of system calls, the more user threads per kernel thread, the slower your applications will run, because the kernel thread will become a bottleneck, since all system calls will pass through it.

On the flip side though, if you're programs rarely use system calls (or other kernel services), you can assign a large number of user threads to a kernel thread without much performance penalty, other than overhead.

You can increase the number of kernel threads, but this adds overhead to the kernel in general, so while individual threads will be more responsive with respect to system calls, the system as a whole will become slower.

That is why it is important to find a good balance between the number of kernel threads and the number of user threads per kernel thread.

Also see here: What is the difference between kernel threads and user threads?

What is the difference between kernel threads and user threads?

Kernel threads are privileged and can access things off-limits to user mode threads. Take a look at "Ring (Computer Security)" on Wikipedia. On Windows, user mode corresponds to Ring 3, while kernel mode corresponds to Ring 0.

What are techniques used for creating kernel threads?

This is extremely dependent upon the operating system.

now in case of user level threads Is it that this TCB is created in user's address space ?

The TCB records information about a thread that the kernel uses in running that thread, right? So if it were allocated in user space, the user mode thread could modify or corrupt it, which doesn't seem like a very good idea. So, don't you suppose it's created in kernel space?

What are these models? How are these models practically used?

Wikipedia seems really clear about that.

Community
  • 1
  • 1
Jordan
  • 2,992
  • 2
  • 20
  • 29
  • Thanks for the response. I am still a little confused about how the kernel threads are executing. When a user thread makes a system call, does the kernel thread that is mapped to it get executed and returns the result of the system call through that kernel thread? And also, are kernel threads not inside processes? Are they similar to processes then? – Instinct May 01 '13 at 16:50
  • What type of system architecture are we talking about? – Jordan May 01 '13 at 16:56
  • This only talks about the user and kernel thread but doesn't clarify the relationship between the process and kernel thread – Bhabani Das Feb 20 '22 at 19:42