OS — Threads

Creating a separate process for each of potentially many short-lived activities is too inefficient. A thread is an instance of executing a portion of a program within a process without incurring the overhead of creating and managing separate PCBs (Process Control Block).
Thread Control Block (TCB):
Each thread within a process is an independent execution activity, the runtime information held in the PCB and the execution stack must be replicated for each thread. A thread control block (TCB) is a data structure that holds a separate copy of the dynamically changing information necessary for a thread to execute independently.
User-level vs kernel-level threads
Threads can be implemented completely within a user application. A thread library provides functions to create, destroy, schedule, and coordinate threads. The library maintains all TCBs within the user process. Consequently, the OS kernel is not aware of the threads and treats the process as a single-threaded execution.
The alternative is to implement threads within the OS kernel. The TCBs are not directly accessible to the application, which uses kernel calls to create, destroy, and otherwise manipulate the threads.
Combining user-level and kernel-level threads
Advantages of user-level threads (ULTs) over kernel-level threads (KLTs):
— Because ULTs do not require any cooperation from the kernel, ULTs are much faster to manage (create, destroy, and schedule) and thus many more can be created than KLTs.
— Applications using ULTs are portable between different OSs without modifications.
Main disadvantages of ULTs over KLTs:
— ULTs are not visible to the kernel. When one ULT blocks, the entire process blocks, which decreases concurrency and thus the performance and responsiveness of the application.
— ULTs cannot take advantage of multiple CPUs because the process is perceived by the kernel as a single thread of execution.
Many modern OSs take a combined approach. The kernel supports a small number of KLTs. Each application can implement any number of ULTs, which are then mapped on the KLTs based on the ULTs’ needs and the available resources.