Threads in Operating System

What is Thread, Lightweight Processes, Program Counter, Registers, Examples of thread, Thread Life Cycle, and Thread States discussed in OS.

Threads in operating system.

What is Thread?

A process is divided into many lightweight processes. Each lightweight process is said to be a thread. The thread has a program counter that keeps track of which instruction to execute next. It has registers that hold current working variables. It has a stack, which contains the execution history.

Threads in OS
Process and Threads

The number of threads can share an address space, open files, and other resources. Threads operate in many respects in the same manner as processes. Threads can be in one of several states: Ready, Blocked, Running, and Terminated.

Like the process, threads share the CPU, and only one thread at a time is active(Running). Threads can create child threads. Unlike process, threads are not independent of one another because all threads access every address in the task. A thread can read or write over any other thread stack.

Each thread designates a portion of the program. That may be executed concurrently with other threads. This capability is called multithreading.

Example of Threads in OS

A word processor is an example of a multi-threaded application. For instance, when a user types text in the word processor, it is one thread. Simultaneously, the text is automatically formatted, which is another thread, and the file is automatically saved on the disk, which is another thread.

While typing in a file is a process, formatting, spell-checking, and scanning are threads within the multi-threaded word processor application.

Text editing: When a user is typing in a text editor, the process of formatting, spell-checking, and saving the file can each be a separate thread running concurrently with the main typing thread.

Web browsing: When a user opens a web browser and enters a URL, a separate thread can be created to download the page content, while another thread can be used to render the page on the screen.

Media playback: When a user plays a video or audio file, a separate thread can be used to read the file from the disk, while another thread can be used to decode the media and yet another thread can be used to display or playback the media.

Network communication: When a user sends data over a network, a separate thread can be used to send the data, while another thread can be used to receive data from the network.

Thread Life Cycle in OS

The life cycle of a thread in an operating system involves the creation, scheduling, execution, blocking, and termination. The operating system plays a critical role in managing the life cycle of threads, ensuring that they run efficiently and effectively.

Thread lifecycle in OS
Lifecycle of thread

Creation

A thread is created by a process or by another thread within the same process. The operating system allocates resources such as stack and program counter for the new thread.

Scheduling

The operating system schedules the execution of the thread. The thread is added to the ready queue, and its status is set to ready. The scheduler selects the thread to be executed based on its priority and scheduling algorithm.

Execution

The thread starts executing its instructions. It may run until it is blocked, terminated, or pre-empted by the operating system scheduler.

Blocking

A thread may become blocked when it needs to wait for a resource or an event to occur. For example, a thread may wait for user input or for a semaphore to be released. When a thread is blocked, its status is set to block, and the operating system scheduler selects a different thread to run.

Termination

When a thread has completed its execution, it terminates. The operating system de-allocates the resources that were assigned to the thread, and its status is set to terminate.

Process and Threads

A process is divided into several smaller tasks; each task is called a thread. Many threads within a process executed at a time are called multithreading in OS.

Multithreading Models

In an operating system, multithreading is divided into four categories.

  1. One Process, One Thread: In this traditional approach, the process maintains only one thread. For example, the MS-DOS operating system supports this approach.
  2. One Process, Multi Threads: In this approach, the process is divided into many threads. Java Runtime Environment works on the model.
  3. Multi Processes, One Thread: The operating system supports multiple user processes but only supports one thread process. For example, UNIX.
  4. Multi Processes, Multi Threads: In this approach, a process is divided into many threads. Windows 2000, Solaris, and LINUX are an example of the model.

Difference Between Process and Thread

  • A process cannot share the same memory space, whereas threads can share memory and files.
  • Process creation takes more time, whereas; thread creation takes less time.
  • The process takes more time to complete the execution and termination, whereas; the thread takes less time to terminate.
  • Process execution is slow, but threads execute very fast.
  • Context switching time between two processes is much. Context switching time between two threads is less as compared to the process.
  • Implementing communication between two processes is difficult. But communication between the two threads is easy to implement because threads share the memory.
  • System calls are required to communicate with each process. In the case of a thread system calls are not required to communicate.
  • The loosely coupled process, but tightly coupled threads.
  • The process requires more resources to execute. Threads require fewer resources. Therefore, threads are called lightweight processes.
  • A process is not suitable for the parallel activity, But the thread is suitable for the parallel activity.

Thread Synchronization

All threads within a process share the memory and files. If any thread tries to modify a record in a file, it will be affected by other threads. So, it needs to synchronize the activities of threads.