What are Threads In Operating System ?

Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads are also known as Lightweight processes. Threads are popular way to improve application through parallelism. The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel.

As each thread has its own independent resource for process execution, multpile processes can be executed parallely by increasing number of threads.

Single Threaded and Multithreaded Process


Types of Thread

There are two types of threads :

  • User Threads
  • Kernel Threads

User threads, are above the kernel and without kernel support. These are the threads that application programmers use in their programs.

Kernel threads are supported within the kernel of the OS itself. All modern OSs support kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously.


Multithreading Models

The user threads must be mapped to kernel threads, by 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 all mapped onto a single kernel thread.
  • Thread management is handled by the thread library in user space, which is efficient in nature.

Many to One thread model


One-To-One Model

  • The one-to-one model creates a separate kernel thread to handle each and every user thread.
  • Most implementations of this model place a limit on how many threads can be created.
  • Linux and Windows from 95 to XP implement the one-to-one model for threads.

One to One thread model


Many-To-Many Model

  • The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models.
  • Users can create any number of the threads.
  • Blocking the kernel system calls does not block the entire process.
  • Processes can be split across multiple processors.

Many to Many thread model


Thread Libraries

Thread libraries provides programmers with API for creating and managing of threads.

Thread libraries may be implemented either in user space or in kernel space. The user space involves API functions implemented solely within user space, with no kernel support. The kernel space involves system calls, and requires a kernel with thread library support.

 

There are three types of thread :

  • POSIX Pitheads, may be provided as either a user or kernel library, as an extension to the POSIX standard.
  • Win32 threads, are provided as a kernel-level library on Windows systems.
  • Java threads – Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pitheads or Win32 threads depending on the system

Benefits of Multithreading

  1. Responsiveness
  2. Resource sharing, hence allowing better utilization of resources.
  3. Economy. Creating and managing threads becomes easier.
  4. Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a series of processors to scale.
  5. Context Switching is smooth. Context switching refers to the procedure followed by CPU to change from one task to another.

Multithreading Issues

  1. Thread Cancellation.Thread cancellation means terminating a thread before it has finished working. There can be two approaches for this, one is Asynchronous cancellation, which terminates the target thread immediately. The other is Deferred cancellation allows the target thread to periodically check if it should be cancelled.
  2. Signal Handling.Signals are used in UNIX systems to notify a process that a particular event has occurred. Now in when a Multithreaded process receives a signal, to which thread it must be delivered? It can be delivered to all, or a single thread.
  3. fork() System Call.fork() is a system call executed in the kernel through which a process creates a copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process be copied or not?
  4. Security Issues because of extensive sharing of resources between multiple threads.

There are many other issues that you might face in a multithreaded process, but there are appropriate solutions available for them. Pointing out some issues here was just to study both sides of the coin.