3

I have to implement kernel level thread but while searching on the net I found that there are three ways to create kernel level thread in linux:

  1. NPTL
  2. kthread
  3. linuxThreads

It was written somewhere that linuxThreads are now abandoned. But I am unable to find current support of NPTL & kthread. Also I am unable to find any source that can simply explain me how to use their functionality.

Which is the currently supported and good library to use kernel level thread?

Also pls share any resource for installing these library and also using them?

Abhishek Gupta
  • 6,465
  • 10
  • 50
  • 82
  • 1
    Do you mean "a library to run threads in the kernel" or "a library that supports threads as provided by the kernel, running in userspace"? – Mats Petersson Jan 28 '13 at 11:47
  • You seem to be confusing two totally different definitions of "kernel thread". Also, NPTL is the default POSIX threading implementation on pretty much every modern Linux distribution -- it was merged into glibc ages ago. – David Schwartz Jan 28 '13 at 11:56
  • There are two kinds of threads in linux - `userspace and kernel space`. There is 1:1 mapping between these two, right. `pthread` creates a user space thread and for every user space thread a kernel space thread is created. But I want to create only a kernel level thread without creating a user space thread – Abhishek Gupta Jan 28 '13 at 11:57
  • @knoxxs: Then you just want to use the kernel, nothing else. The kernel creates lots of kernel threads. You can see them in `ps ax`', they're the ones in brackets. (kworker, ksoftirqd, scsi_eh, and so on.) – David Schwartz Jan 28 '13 at 11:57
  • @David But I want to create the same manually. – Abhishek Gupta Jan 28 '13 at 11:59
  • @knoxxs: You do it the exact same way. Have a look at the kernel. (Depending on what your thread is going to do. For example, if it's going to service a work queue from a single thread, you create it by calling `create_singlethread_workqueue`. Otherwise, you might call `kthread_run`. And so on. See `kernel/kthread.c`.) – David Schwartz Jan 28 '13 at 11:59
  • But what is the difference between NPTL and kthread? – Abhishek Gupta Jan 28 '13 at 12:05
  • @knoxxs: First of all, you have to read/learn about these different mechanisms. You are asking wrong questions. I am still not sure you really want to create a Kernel Thread. David seems to know about this stuff and is trying to help you, but I think it is better for you to tell what you are really trying to achieve by creating this Kernel Thread... – Malkocoglu Jan 28 '13 at 12:14
  • @knoxxs You said you wanted to create only a kernel level thread. Why are you bringing NPTL and linuxThreads (both user-space POSIX pthreads implementations) into it? – David Schwartz Jan 28 '13 at 12:18
  • Sry for the confusion, because while searching what I understood is before there was linuxThreads and then NPTL is created which brought the concept of 1:1 mapping between the two levels of threads. But from your answer it seems that I am wrong. What I want to do from it: Actually I just want to try and learn kernel level threads (My teacher suggested that if you want to go deep in threads also try to learn about kernel level threads). When I searched on the net I only found the benefits and concepts about kernel level threads but nothing about how to create them – Abhishek Gupta Jan 28 '13 at 12:24
  • @knoxxs: I suspect your teacher meant threads that are scheduled by the kernel, that is, threads that map 1-to-1 to kernel scheduling entities. – David Schwartz Jan 28 '13 at 12:40

2 Answers2

6

You are confusing two very different definitions of "kernel thread".

LinuxThreads and NPTL are implementations of POSIX pthreads for user-space processes. They use a 1-to-1 mapping of kernel scheduling entities to user-space threads. They are sometimes described as kernel threads implementations only because they create threads that are scheduled by the kernel.

LinuxThreads is unsupported and entirely obsolete. NPTL is now part of glibc, so you already have it. There's nothing special to install. You use these the same way you use any POSIX threading library, with calls to functions like pthread_create.

Actual kernel threads run kernel code. None of those libraries are relevant since they're all user-space libraries. Have a look at functions like kthread_run. There's no magic, no secret. Write kernel code the way similar kernel code is written. (Knowledge and experience in writing kernel code is needed. It's, unfortunately, not simple.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • One doubt in answer: You said "You use these the same way you use any POSIX threading library, with calls to functions like pthread_create." but it seems from this [question][1] that pthread and NPTL are not two different things? [1]: http://stackoverflow.com/questions/8576126/ntpl-and-pthread-confusing – Abhishek Gupta Jan 28 '13 at 12:33
  • I'm not sure what it is you don't understand. This is clarified both in my answer above and in the two top answers to the question you linked. – David Schwartz Jan 28 '13 at 12:38
0

I assume that; if you really wanted to create a kernel thread, you would already know about these things.

I think, you want to create multi-threaded applications and trying to find info about user-level multi-threading functions.

And yes, these threads you created will be managed by the kernel itself. This is what you are looking for :: POSIX Threads

Malkocoglu
  • 2,522
  • 2
  • 26
  • 32
  • Then do exactly the same thing existing kernel code does to create kernel level threads. There's no magic, no secret. – David Schwartz Jan 28 '13 at 12:01
  • 1
    @knoxxs: Well, then my answer is wrong. LinuxThreads and NPTL were early thread implementations for Linux. Kernel Thread is something else, it is about threading in the kernel space. These are different contexts. You can not use POSIX Threads in kernel-space and can not create a Kernel Thread in user-space. Maybe this helps :: http://www.linux-mag.com/id/2195/ – Malkocoglu Jan 28 '13 at 12:05