Threads in Operating System

Published by

on

What is Thread?

Thread is a flow of execution through the process code. Thread lives within a process, and one process can contain multiple threads.

Thread has its own

  • Program counter – Keeps track of which instruction to execute next
  • System register – Current working variables
  • Stack – Stores execution history

And shares a few information like code segment, data segment, and open files with other threads.

What is Multithreading?

Thread is less resource intensive than process, so it provides a way to improve application performance with parallelism. The idea of multithreading is that it divides a process into multiple threads. For example, different tabs in Chrome browser can be considered as multiple threads.

Process vs Thread

Thread is also called as a lightweight process, but there are a few differences that distinguishes from a process.

ProcessThread
Process is heavy weight or resource intensive.Thread is light weight, taking less resources than a process.
Process switching needs interaction with OS. Context switching requires more overhead from the CPU.Thread switching does not need interaction with OS. Context switch time between threads is lower compared to process context switch.
In multiple processing environments, each process executes the same code but has its own memory and file resources.
Each process operates independently from each others.
Threads within the same process run in a shared memory and
all threads can share the same set of open files, child processes. One thread can read, write or change another thread’s data
If one process is blocked, the other process cannot execute until the process is unblocked.While one thread is blocked and waiting, a second thread in the same task can run.

Type of Threads

  • User level thread – Threads managed by user
  • Kernel level thread – OS manages threads acting on kernel, an operating system core.

User Level Thread

User threads are implemented by users. User utilizes the thread library to create threads. Thread library contains codes that creates/destroys the threads, passing messages and data between threads, and scheduling thread execution for saving/restoring thread contexts. In this case, thread management kernel is not aware of the existence of threads.
ex) Java thread, etc

Advantages

  • Fast and easy to create/manage threads.
  • If one user level thread performs blocking operation, the entire process will be blocked.
  • Thread switching does not require kernel mode privileges
  • User level threads can be running from any Operating Systems
  • Scheduling can be application specific
  • Context switch time is less

Disadvantages

  • In a typical Operating System, most system calls are blocking.
  • Multithreaded application cannot take advantage of multiprocessing

Kernel Level Thread

Kernel threads are supported directly from Operating System. There are no thread management codes in application area. Thread management is done from the Kernel. All of the threads within an application are supported within a single process.
The Kernel maintains context information for the process as a whole and for individuals threads within the process. Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space. Kernel threads are generally slower to create and manage than the user threads.

Advantages

  • If one thread in a process is blocked, the Kernel can schedule another thread of the same process.
  • Kernel can simultaneously schedule multiple threads from the same process on multiple processes

Disadvantages

  • Slower to create/manage compared to user level threads

User level thread vs Kernel level thread

User-Level ThreadsKernel-Level Threads
Faster to create and manageSlower to create and manage
Implementation by userOperating system supports creation
Generic and can run on any OSSpecific to the OS
Multi-threaded application cannot take advantage of multiprocessingKernel routines themselves can be multithreaded

Multithreading Models in Process Management

Some operating system combines user level thread and kernel level thread facility. In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not to block the entire process. Below are the three types of Multithreading models.

  • Many to Many model
  • Many to One model
  • One to One model

Many to Many Model

User 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 user thread performs a blocking system call, the kernel can schedule other user thread to other kernel thread. Therefore, system doesn’t block if a particular thread is blocked.

Many to One Model

When thread makes a blocking system call, the entire process will be blocked. Only one thread can access 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 OS in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship mode.

One to One Model

This model provides more concurrency than many-to-one model. It allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread requires the corresponding Kernel thread.

Reference

Leave a comment