3
votes

I study the OS book[operating system concepts] written by Silberschatz recently.

At chapter 5, the book describes the concept of "Contention scope", and mentions that there are two types of contention scope. One is PTHREAD_SCOPE_PROCESS and the other is PTHREAD_SCOPE_SYSTEM.

And for PTHREAD_SCOPE_SYSTEM, the user level thread will bind to one LWP[virtual processor], and this LWP will attaches to one kernel thread.

Actually, I'm not quite understanding the concept of LWP. On the command line terminal I can type the following command:

ps -eLf

And I can see one process[PID] may have several LWP[thread IDs]. By doing some survey, I found that LWP seems simply mean thread or tasks in linux. like the following link:

How Linux handles threads and process scheduling

So I have the following two questions:

Q1 : Is LWP defined as virtual processor in linux? what source code in kernel defines it?

Q2: In the following picture, syslog has the PID [852] and has four threads. And these four LWPs will each bind one kernel thread? Or kernel will just schedule them directly?

enter image description here

The above picture is taken from:

http://www.softprayog.in/tutorials/ps-command-usage-examples-in-linux

Thanks in advance

1

1 Answers

5
votes

Linux does not support PTHREAD_SCOPE_PROCESS. See for example http://man7.org/linux/man-pages/man3/pthread_attr_setscope.3.html

POSIX.1 requires that an implementation support at least one of these contention scopes. Linux supports PTHREAD_SCOPE_SYSTEM, but not PTHREAD_SCOPE_PROCESS.

In Linux, a process' multiple threads are like separate processes that happen to share the same address space. Yes, this is a complete oversimplification, they share other resources as well -- notably, signal handlers, file descriptors and various IDs -- and there are other more subtle differences, but they are each full-fledged independently schedulable entities. The terminology used within the kernel is "task". Every process has at least one task. A multithreaded process has more than one.

Q1: LWP is not the same as a virtual processor (whatever you mean by that -- it's not something that's familiar in this context). An LWP corresponds to a task in linux. Although outside kernel development, "process" and "thread" are still the more common terms.

Q2: Each LWP corresponds to a separate task (or user thread), and each is separately schedulable by the kernel. (In linux, the term "kernel thread" has a specific meaning that is quite different: a kernel thread is a thread [really task] which spends its entire life inside the kernel. They're typically used for various internal housekeeping functions.)

For more on the Posix threading model, see this very good explanation: http://www.icir.org/gregor/tools/pthread-scheduling.html that discusses linux and FreeBSD implementations.

And this one, for more general threading concepts: http://timetobleed.com/threading-models-so-many-different-ways-to-get-stuff-done/