Multi-threaded Programming

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.

Multi-threaded Processes

The traditional single-threaded program can do only one task, the multi-threaded process can do more than one tasks at a time.

Advertisements

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.

Figure 1 – Single-Threaded Process and Multi-Threaded Process

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.

Multi-Threading Models

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
2. One-to-one
3. Many-to-many

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.

Figure 2 – Many-to-One Model

Only one thread can access the kernel thread at a time, hence, parallel execution of threads not possible in multiprocessors system.

One-To-One Model

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.

Figure 3 – One-to-One Model

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.

Advertisements

Many-To-Many 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.

Figure 4 – Many-to-Many Model

Two-Level Model

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).

Figure 5 – Two-Level Model

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 Libraries

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
2. Win32
3. Java

In the next article, we will discuss the above three libraries in detail.

References

  • 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.
Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version