Definition:
Threads are implemented by a library that utilizes underlying kernel-supported threads of control, called light-weight processes (LWPs).
Each LWP can be thought of as a virtual CPU which is available for executing code or system calls. Each LWP:
is separately dispatched by the kernel.
may perform independent system calls.
incur independent page faults.
may run in parallel on a multiprocessor.
Note: Most programmers use threads without thinking about LWPs.
Mapping threads to LWPs
All the LWPs in the system are scheduled by the kernel onto the available CPUs according to their scheduling class and priority.
Each process contains one or more LWPs, each of which runs one or more user threads.
There is no one-to-one mapping between user threads and LWPs.
User-level unbound threads can freely migrate from one LWP to another.
If there are available processors, the LWPs run in parallel.
The OS has no knowledge about what user threads are or how many are active in each process.
Unbound Threads:
Threads that are scheduled on the LWP pool are called unbound threads. They float among the LWPs.
The library invokes LWPs as needed and assigns them to execute runnable threads.
The LWP assumes the state of the thread and executes its instructions.
If another thread should be run, the thread state is saved in process memory and the threads library assigns another thread to the LWP.
Bound Threads:
If needed, a thread can be permanently bonded to an LWP.
For example, you can bind a thread to:
Have the thread scheduled globally (such as realtime)
Give the thread an alternate signal stack
Give the thread a unique alarm or timer
Threads Library Implementation
Each thread is represented by a thread structure that contains:
The thread ID.
An area to save the thread execution context.
The thread signal mask
The thread priority
A pointer to the thread stack.
The storage for the stack is either automatically allocated by the library or it is passed in by the application on thread creation
When a thread is created, a thread ID is assigned. The thread ID is used as an index in a table of pointers to thread structures.
Thread-local Storage:
Threads have some private storage (in addition to the stack) called thread-local storage (TLS).
Most variables in the program are shared among all the threads executing it, but each thread has its own copy of thread-local variables.
Conceptually, thread-local storage is
Unshared.
Statically allocated data.
Used for thread-private data that must be accessed quickly.