A Threading Library is a piece of code that provides the programmers with APIs to create and manage threads in an Operating Systems. There are two ways to implement a thread library. One is to write a user-level library and the other is kernel-level library.
-
User-Level Library. This way a library is provided entirely in user space with no kernel support and all code and data structures exist in user-space. This means that invoking a function will result in only local funciton call in the user space but not a system call.
-
Kernel-Level Library. This way the library is fully supported by the operating system and data structures and code exist in kernel. Invoking a function call with result in system call directly to the kernel.
Following three are the main threading libraries in majority use.
- POSIX Pthreads. It may be provided as a user or kernel library as an extension to the POSIX standards.
- Win32 Threads. It is provided as a kernel library in the Windows system.
- Java Threads. This library is provided to create and manage threads directly in Java programs and their implementation is depended on the OS on which the JVM (Java Virtual Machine) is running.
Examples In Operating Systems
Following are the examples of threads in use in Operating Systems.
Windows Operating System
Win32 library supports one-to-one threading model but also provides a library to implement many-to-many model. A windows OS run an application as a single process and every process can have one or more threads in it. Every thread in windows has following components.
- A Thread ID
- Registers
- A user-stack in user mode
- A kernel-stack in kernel mode
- A private storage area used by run-time libraries and dynamic-link libraries.
The key data structures for windows threads are ETHREAD (Executive Thread Block), KTHREAD (Kernel Thread Block), and TEB (Thread Environment Block). ETHREAD and KTHREAD can only be accessed by the kernel while TEB can be accessed by the user.
Linux Operating Systems
Linux doesn't distinguish between processes and threads. It uses the more generic term tasks for representing processes and threads. Linux provides the fork() system call that completely duplicates a process task. a separate system call called clone() is also provided to create threads. These are controlled by different flags.