Multithreading And Its Models

Multithreading allows the Operating System to execute different threads simultaneously. Many software packages for modern computer systems are multithreaded. An application is typically implemented as a separate process with several threads.

For example, a web browser can use only one thread to display images and a second thread to retrieve data from the network.

multithreading

A single application may need to perform many similar tasks in some situations. For example, a web server receives requests for web pages, images, sounds, etc. many clients may be accessing the server at the same time.

You May Also Like:

The web server can serve only one client at a time if it runs as a single-threaded process. The clients may have to wait for a very long time for the service of the requests.

A solution to this problem is that the server should run a process to accept requests. The server then creates a separate process to service that request. This method was used before the popularity of threads. However, the process of creation is very time-consuming.

It is more efficient to use one process with multiple threads. A multithreaded server can create a separate thread for client requests and another thread to service that request.

CPU Threads are also very important in remote procedure call (RPC) systems. RPC servers are typically multithreaded. A server services the received message using a separate thread. It allows the server to service several concurrent requests.

Many Operating System kernels are also multithreading. Many threads operate in the kernel and each thread performs a specific task such as managing devices or handling interrupts. For example, Solaris creates a set of threads in the kernel for handling interrupts.

Linux uses a kernel thread to manage the amount of free memory in the system.

Advantages of Multithreading

  • Responsiveness
  • Resource Sharing
  • Economy
  • Utilization of Multiprocessor Architecture

Responsiveness

The multithreading approach increases the responsiveness of the process. A process consists of more than one thread. If one thread is blocked or busy in a long computation, some other thread may still be executing.

So the user gets an extra response from the executing process. For example, a browser permits a user to interact with it while a file is being downloaded.

Resource Sharing

All threads of one process share the memory and resources of that process. Secondly, it permits an application to have many different threads within the same address space.

Economy

The allocation of memory and resources for process creation is costly. All threads in a process share the resources of that process so it is more economical to create and context switch the threads.

Utilization of Multiprocessor Architecture

Multiprocessor architecture allows the facility of parallel processing. It is the most efficient way of processing. A single-threaded process can be executed on one CPU even if there are more processors.

Multithreading one multiprocessor system increases concurrency. Different parts of a multi-threaded process can be executed simultaneously on different processors.

Multithreading Models

In a specific implementation, the user threads must be mapped to kernel threads using one of the following strategies.

  • Many-to-One Model
  • One-to-One Model
  • Many-to-Many Model

Many-to-One Model

In the many-to-one model, many user-level threads are mapped to one kernel thread. It is efficient because it is implemented in userspace. A process using this model will be blocked entirely if a thread makes a blocking system call.

Only one thread can access the kernel at a time so it cannot run in parallel on a multiprocessor. The Green thread for Solaris and GNU Portable Threads are implemented by the many-to-one model.

One-to-One Model

In the one-to-one model, each user thread is mapped to a kernel thread. It provides more concurrency because it allows another thread to execute if a thread makes a blocking system call. It facilitates parallelism in a multiprocessor system.

Each user thread requires a kernel thread that may affect the performance of the system. The creation of threads in this model is restricted to a certain number. It is used by Linux and Windows XP and OS/2.

Many-to-Many Model

This model multiplexes many user-level threads to a smaller or equal number of kernel threads. The number of kernel threads may be specific to either a particular application or a particular machine.

The user can create any number of user threads and corresponding kernel threads can run in parallel on the multiprocessor system. The kernel can execute another thread if a thread makes a blocking system call.

The individual processes may be allocated variable numbers of kernel threads depending on the number of CPUs available and other factors. It is used by Solaris, IRIX, HP-UX, and Tru64 UNIX.

Thread Libraries

A thread library provides an API to create and manage threads. There are two ways to implement a thread library. The faster approach is to provide a library in userspace with no kernel support. All code and data structures for the library exist in userspace.

The invoking of a function in the library is a local function call in userspace, not a system call. The second approach is to implement a kernel-level library supported by the Operating System. The code and data structures for the library exist in kernel space in this case.

The invoking of a function in the API is a system call to the kernel. Three main thread libraries used today are :

POSIX P Threads

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-level library on the windows system.

Java Threads

Java generally runs on a Java Virtual Machine. The implementation of threads is based on the Operating System and the hardware on which JVM runs.

Operating System Examples

Some examples of Operating System  threads are as follows:

Windows XP Threads

Win32 API thread library supports a one-to-one thread model. It also provides a fiber library that supports the many-to-many model.

Win32 thread components include the following:

  • Thread ID
  • Registers
  • A user stack utilized in user mode and a kernel stack utilized in kernel mode
  • A private storage area is utilized by various run-time libraries and dynamic link libraries.

The key data structures for Windows threads are E-THREAD (executive thread block), K-THREAD (kernel thread block), and TEB (thread environment block). The E-THREAD and K-THREAD structures exist entirely within kernel space and can only be accessed by the kernel. The TEB lies within the userspace.

Linux Threads

Linux does not distinguish between processes and threads. It uses the more generic term’s tasks. The traditional fork () system call completely duplicates a process task.

An alternative call clone () is used for varying degrees of sharing between the parent and child tasks. It is controlled by different flags.

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button