What are threads in operating systems?

A thread is a singular, sequential flow of the execution of tasks in a process.

It is possible to execute threads within processes in the different operating systems. That said, there can be more than one thread within a process.

Thread as a lightweight process

A thread can be called a lightweight process. The purpose is to accomplish parallelism by splitting a process into many threads.

Some of the examples include:

  • Different tabs in a browser as multiple threads.
  • Different threads for different functions in MS Word.

Process vs. thread

Below are some differences between Processes and Threads.

1. Resources

Processes requires more resourcesheavyweight, while threads take fewer resourceslightweight.

2. Operations

Every process runs separately from other processes. On the other hand, one thread is capable of reading, writing, and changing other threads’ data.

3. Execution

No other process can run if the first process is blocked. Meanwhile, a thread can execute the task even if another thread is blocked.

4. Switching

Interaction is required with the operating system to switch between processes. No interaction is needed with the operating system for thread switching.

5. Threaded processes

Without threads, multiple processes require extra resources. However, fewer resources are needed for multiple threaded processes.

6. Memory and resources

Each process runs the same code, but ithas its individual memory and file resources in a multiprocessing environment. With threads, the same set of open files can be used by children threads.

Benefits of threads

The following are some of the needs and benefits of threads:

Types of threads

  • User-level threads
  • Kernel level threads

User-level threads

The thread library includes code to do the following:

  • Create and destroy threads, route messages, and data among threads
  • Schedule thread execution
  • Store and restore thread contexts

Uses of user-level threads

  • User-level threads are quick to build and operate
  • Thread switching does not need kernel-mode privileges
  • The user-level thread runs on any operating system
  • Scheduling can be application-specific in the user-level thread

Kernel-level threads

The kernel does not recognize the existence of threads.

In kernel-level threads, the kernel takes over the thread management. There is no thread management code in scope, as the kernel threads are directly supported by the operating system.

Any application can be programmed for multithreading. All threads of an application are supported in a single process.

The kernel manages context information for the entire process and for individual threads within the process.

Kernel scheduling is done by a thread. The kernel creates, schedules, and manages threads in kernel space.

Uses of kernel-level threads

  • Different threads from the same process on many processes can be scheduled together by the kernel

  • If a thread is blocked in a process, then a different thread from the same process can be scheduled by kernel

  • There can be multithreaded kernel routines

Free Resources