A thread is a basic unit of CPU utilization; it has a thread id, a program counter, a register set, and a stack. It shares code, data and other os resources such as open files and signals with other threads in the process.
The traditional single-threaded program can do only one task, the multi-threaded process can do more than one tasks at a time.
For example, if a web server handles multiple users, rather than creating multiple similar processes, it could create multiple threads for multiple users. Many OS is multi-threaded and each thread performs a specific task at the kernel level.
Advantages of Using Multi-threaded Programming
There are many advantages of using multi-threaded programming which are:
- Responsiveness – One part of a process is blocked, while other threads interact with users doing another task.
- Resource sharing – Threads share codes, data and memory to each other by default.
- Economy – It is easier to create and manage thread than process because the threads use same address space as the process, sharing data is easier for threads, than inter-process communication.
- Scalability – You can run different threads on different processors in multi-processor systems.
Since the processes are different and run in different modes their threads also run in respective modes. The kernel thread supports the user-thread. The multi-threaded model defines the relationship between user-thread and the kernel-thread.
1. Many-to-One Model
Many user threads map to one kernel-thread in this scheme. A thread library at user space manages the user threads. A blocking system call from one thread will block the whole process.
Only one thread can access the kernel thread at a time, hence, parallel execution of threads not possible in multiprocessors system.
This model maps each user thread to a kernel thread. Unlike the many-to-one model, it allows parallel execution of user threads on multiprocessors.
There is a performance overhead when a new thread is created because we also required to create a kernel thread. So to implement this model, the OS must restrict the number of threads. Linux, as well as Windows OS, implement this model.
Maps many user threads to a smaller or equal number of kernel threads. This model is better than other models and achieves true concurrency.
The two-level model is a variation of many-to-many model used in some operating systems such as HP-UX, True64 UNIX, IRIX, and older versions of Solaris (prior to Solaris 9).
The user threads are supported by smaller or an equal number of kernel threads, and also allows one-to-one mapping with kernel thread.
Thread library provides API to create and manage threads. The library is implemented at the user level space or the kernel level space depending on the OS.
API - application programming interface is a set of functions to build applications.
Two ways to implement a thread library.
1. Library at userspace with no OS support.
2. Kernel level with OS support.
There are three thread libraries in use today. Here is the list:
1. POSIX Pthread
In the next article, we will discuss the above three libraries in detail.
- Abraham Silberschatz, Peter B. Galvin, Greg Gagne (July 29, 2008) Operating System Concepts, 8 edn., : Wiley.
- Tanenbaum, Andrew S. (March 3, 2001) Modern Operating Systems, 2nd edn., : Prentice Hall.