Published by: Zaya
Published date: 22 Jun 2021
A thread is the smallest unit of programmed instructions that can be managed independently and processing that can be performed by an operating system. A thread is also called a lightweight process. Multiple threads can be existing within the same process that shares common resources such as memory. If a process has multiple threads of control, it can perform more than one task at a time. Threads within a process share the same virtual memory space but each has a separate stack, and possibly "thread-local storage" if implemented. They are lightweight because a context switch is simply a case of switching the stack pointer and program counter and restoring other registers, whereas a process context switch involves switching the MMU context as well.
Fig: Threads[/caption]
For example a web browser can have a thread to display images or text while another thread retrieves data from the network.
Also in a word processor, a background thread may check spelling and grammar while a foreground thread processes user input (keystrokes), while yet a third thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited.
Advantages of using Threads
Advantages:
Disadvantages:
Advantages:
Disadvantages:
Implement the entire threads package in userspace without the kernel knowing about them. When one thread gets blocked by a system call, all user-level threads get blocked. As far as the kernel is concerned, it is managing ordinary, single-threaded processes. A user-level threads package can be implemented on an operating system that does not support threads.
The threads run on top of a run-time system, which is a collection of procedures that manage threads. When threads are managed in user space, each process needs its own private thread table to keep track of the threads in that process. This table is analogous to the kernel’s process table, except that it keeps tracks only of the per-thread properties, such as each thread’s program counter, stack pointer, registers, state, and so forth. The thread table is managed by the run-time system. When a thread is moved to ready state or blocked state, the information needed to restart it is stored in the thread table, exactly the same way as the kernel stores information about processes in the
process table.
The kernel’s thread table holds each thread registration, state, and other information. The information is the same as with the user-level threads, but now kept in the kernel instead of the user space (inside the run-time system). This information is the subset of information that
traditional kernels maintain about their single-threaded process, that is, the process state. In addition, the kernel also maintains the traditional process table to keep track of processes.
Some operating systems provide a combined user-level thread and Kernel-level thread facility. Solaris is a good example of this combined approach. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. Multi-threading models are three types
The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads.
The following diagram shows the many-to-many threading model where 6 user-level threads are multiplexing with 6 kernel-level threads. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency and when a thread performs a blocking system call, the kernel can schedule another thread for execution.
The many-to-one model maps many user-level threads to one Kernel-level thread. Thread management is done in userspace by the thread library. When the thread makes a blocking system call, the entire process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.
There is the one-to-one relationship of the user-level thread to the kernel-level thread. This model provides more concurrency than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on
microprocessors.
The disadvantage of this model is that creating a user thread requires the corresponding Kernel thread. OS/2, Windows NT and windows 2000 use one to one relationship model.