61

The title might not be clear enough because I don't know how to define my questions actually.

I understand Pthread is a thread library meeting POSIX standard (about POSIX, see wikipedia: http://en.wikipedia.org/wiki/Posix). It is available in Unix-like OS.

About thread, I read that there are three different models:

User level thread: the kernel does not know it. User himself creates/implements/destroy threads.

Kernel level thread: kernel directly supports multiple threads of control in a process.

Light weight process(LWP): scheduled by kernel but can be bounded with user threads.

Did you see my confusion? When I call pthread_create() to create a thread, did I create a user level thread? I guess so. So can I say, Pthread offers a user level solution for threads? It can not manipulate kernel/LWP?

vatsa
  • 124
  • 1
  • 9
Mengfei Murphy
  • 1,049
  • 3
  • 11
  • 16

6 Answers6

36

@paulsm4 I am doubtful about your comment that kernel knows every thing. In this particular context of user level threads, the kernel is unaware of the fact that such a thing is happening. A user level thread's scheduling is maintained by the user himself (via the interface provided by a library) and the kernel ends up allotting just a single kernel thread to the whole process. Kernel would treat the process as a single threaded and any blocking call by one of the threads would end up blocking all the threads of that process. Refer to http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/threads.htm

pareshverma91
  • 804
  • 2
  • 8
  • 14
  • This would happen if it wouldnt use kernel threads. The other way around. If it would handle this in the user space, you would end up just using one core, cause user space has no control over the cores nor can it schedule something on a core without kernel interaction. – Sebi2020 Aug 01 '21 at 03:46
26

In Linux, pthread is implemented as a lightweight process. Kernel (v2.6+) is actually implemented with NPTL. Let me quote the wiki content:

NPTL is a so-called 1×1 threads library, in that threads created by the user (via the pthread_create() library function) are in 1-1 correspondence with schedulable entities in the kernel (tasks, in the Linux case). This is the simplest possible threading implementation.

So pthread in linux kernel is actually implemented as kernel thread.

Community
  • 1
  • 1
Junji Zhi
  • 1,382
  • 1
  • 14
  • 22
  • 3
    This should be the accepted answer. Pthreads in Linux is implemented with NPTL which is actually a one-to-one threading model. With every user level thread created, there is a kernel level thread associated with it. That means Pthreads implemented with NPTL can take advantage of multicore CPUs – dantebarba Aug 07 '19 at 21:30
  • @dantebarba: Junji Zhi is correct (and I upvoted his answer yesterday ago, and also updated my response years ago), you are wrong. Important notes: 1) Pthreads is just an *interface*. The implementation will vary from environment to environment. 2) The linux implementation of "threads" vs. "processes" does *NOT* necessarily correspond to what's often portrayed in general OS textbooks. 3) Specifically, Linux pthreads implementations generally use Linux [NTPL](https://en.wikipedia.org/wiki/Native_POSIX_Thread_Library), "... a so-called 1x1 threads library". – paulsm4 Aug 09 '19 at 16:17
  • 1
    You are just replying exactly what I've said in the comment earlier... plus Im not the only one stating that your answer was misleading. You specifically said 'kernel knows everything' which is incorrect on a N:1 threading model. The fact that NPTL is an implementation for a 1-1 model doesn't mean all implementations should behave the same way. – dantebarba Aug 10 '19 at 17:21
16

pthreads, per se, isn't really a threading library. pthreads is the interface which a specific threading library implements, using the concurrency resources available on that platform. So there's a pthreads implementation on linux, on bsd, on solaris, etc., and while the interface (the header files and the meaning of the calls) is the same, the implementation of each is different.

So what pthread_create actually does, in terms of kernel thread objects, varies between OSes and pthread library implementations. At a first approximation, you don't need to know (that's stuff that the pthread abstraction allows you to not need to know about). Eventually you might need to see "behind the curtain", but for most pthread users that's not necessary.

If you want to know what a /specific/ pthread implementation does, on a specific OS, you'll need to clarify your question. What Solaris and Linux do, for example, is very different.

FIn
  • 191
  • 2
12

Q: I understand Pthread is a thread library meeting POSIX standard

A: Yes. Actually, "Pthreads" stands for "Posix threads": http://en.wikipedia.org/wiki/Pthreads

Q: It is available in Unix-like OS.

A: Actually, it's available for many different OSs ... including Windows, MacOS ... and, of course, Linux, BSD and Solaris.

Q: About thread, I read that there are three different models

Now you're getting fuzzy. "Threads" is a very generic term. There are many, many different models. And many, many different ways you can characterize and/or implement "threads". Including stuff like the Java threading model, or the Ada threading model.

Q: When I call pthread_create() to create a thread, did I create a user level thread?

A: Yes: Just about everything you do in user space is "protected" in your own, private "user space".

Q: User level thread: the kernel does not know it.

A: No. The kernel knows everything :)

Q: Kernel level thread: kernel directly supports multiple threads of control in a process.

A: Yes, there is such a thing as "kernel threads".

And, as it happens, Linux makes EXTENSIVE use of kernel threads. For example, every single process in a Linux system is a "kernel thread". And every user-created pthread is ALSO implemented as a new "kernel thread". As are "worker threads" (which are completely invisible to any user-level process).

But this is an advanced topic you do NOT need to understand in order to effectively use pthreads. Here's a great book that discussed this - and many other topics - in detail:

Linux Kernel Development, Robert Love

Remember: "Pthreads" is an interface. How it's implemented depends on the platform. Linux uses kernel threads; Windows uses Win32 threads, etc.

=========================================================================== ADDENDUM:

Since people still seem to be hitting this old thread, I thought it would be useful to reference this post:

https://stackoverflow.com/a/11255174/421195

Linux typically uses two implementations of pthreads: LinuxThreads and Native POSIX Thread Library(NPTL), although the former is largely obsolete. Kernel from 2.6 provides NPTL, which provides much closer conformance to SUSv3, and perform better especially when there are many threads.

You can query the specific implementation of pthreads under shell using command:

getconf GNU_LIBPTHREAD_VERSION

You can also get a more detailed implementation difference in The Linux Programming Interface.

"Pthreads" is a library, based on the Posix standard. How a pthreads library is implemented will differ from platform to platform and library to library.

halfer
  • 19,824
  • 17
  • 99
  • 186
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 31
    Are you creating some confusion here? 1. Original Poster has never asked about Linux specific details 2. Kernel does not knows everything, especially in context of User Level Threads, because they are not scheduled by kernel, they are rather "switched" by library, Pthreads in this case. 3. The term "Threads" is generic. True, but, questions is more about OS and application interface in context of threads, the answer should be about said abstractions. – ultimate cause Apr 28 '16 at 14:57
  • 12
    Indeed, this answer is just flat wrong. The kernel does *not* know about userspace threads; if creating a thread required notifying the kernel about it (via a syscall), then your threads wouldn't be "userspace" anymore. The correct answers here include http://stackoverflow.com/a/12169068/1424877 and http://stackoverflow.com/a/27581327/1424877; and http://stackoverflow.com/a/8639239/1424877 is a nice expression of just the correct parts of this answer (but lacking the Linux details that make the other ones really helpful to the OP). – Quuxplusone May 15 '17 at 01:42
  • 1
    How can every Linux process be a kernel thread? A process in Linux is defined as PCB (process control block) in the struct task_struct structure. – trinity420 Jun 13 '19 at 18:03
  • 2
    this answer is misleading as hell. User Level Threads are not visible to the kernel. That is all what is about the N:1 Threading Model. You can read this on every basic Operating Systems Concepts book, like Silberschatz 7th ed or Stallings 7th ed. Plus, there are three threading models, just like the first user describes: N:1 Model: Multiple ULT mapped to a KLT 1:1 Model: One ULT per KLT. N:M Model: An ULT mapped to a LWP mapped to multiple KLT. – dantebarba Aug 07 '19 at 21:28
  • 1
    Dude - the fact is that Linux does things "somewhat differently" (for very good reasons, with very favorable results). so what you read on "every basic Operating Systems Concepts book" may or may not apply in every detail. And yes, I've read Stallings too. Maybe you should read [Love, 3rd Ed](https://www.amazon.com/Linux-Kernel-Development-Robert-Love/dp/0672329468), then re-read Stallings yourself ;) Or just upvote [Fin's](https://stackoverflow.com/a/8639239/3135317) or [Junji Zhi's](https://stackoverflow.com/a/27581327/3135317) replies below. – paulsm4 Aug 08 '19 at 02:15
5

I find previous answers not as satisfying or clear as I would have liked so here goes:

When you call

pthread_create(...)

you always create a new user-level thread. And assuming that there is OS, there is always one or more kernel thread...but let's dive deeper:

According to "Operating system concepts" 10th edition,the actual classification we should be looking at (when it comes to thread libraries) is how the user level threads are mapped onto kernel threads (and that's what the question really meant).

The models are one to one (each user-level thread within a single process is mapped to a different kernel thread),many to one (the thread library is "user level" so all of the different threads within a single process are mapped to a single kernel thread,and the threads data structures, context switch etc are dealt with at user level and not by the OS [meaning that if a thread blocks on some I/O call, the entire process might potentially block]), and many to many (something in between,obviously the number of user-level threads is greater or equal to the number of kernel threads it is being mapped onto).

Now,pthreads is a specification and not an implementation, and the implementation does depend on the OS to which it is written. It could be any one of those models (notice that "many to many" is very flexible).

So,as an example,on Linux and Windows (the most popular OSs for years now,where the model is "one to one") the implementation is "one to one".

Yoni Keren
  • 1,170
  • 2
  • 13
  • 24
0

Pthreads is just a standardized interface for threading libraries. Whether an OS thread or a lightweight thread is created depends on the library you use. Nevertheless, my first guest would be that your threads are “real” OS-level threads.

Eser Aygün
  • 7,794
  • 1
  • 20
  • 30