What is a Thread in OS and what are the differences between a Process and a Thread?

While reading this blog, you may be listening to music or doing some other work. In other words, you may be performing different tasks parallelly. In the same way, in a computer for the processes to run in parallel, threads are used. In a computer or mobile game when you see objects like cars, trucks, etc, these are just threads that run in the game application. Consider another example where a web browser may have a thread to display images or videos while other threads to fetch the data from the internet. So, a single application may need to perform several similar tasks parallel. Thread is used to improve the application through parallelism. So, let's get started and learn more about threads.

Thread

A thread is an execution unit that has its own program counter, a stack and a set of registers that reside in a process . Threads can’t exist outside any process. Also, each thread belongs to exactly one process. The information like code segment, files, and data segment can be shared by the different threads.

Threads are popularly used to improve the application through parallelism . Actually only one thread is executed at a time by the CPU, but the CPU switches rapidly between the threads to give an illusion that the threads are running parallelly.

Threads are also known as light-weight processes.

The diagram above shows the single-threaded process and the multi-threaded process. A single-threaded process is a process with a single thread. A multi-threaded process is a process with multiple threads. As the diagram clearly shows that the multiple threads in it have its own registers, stack, and counter but they share the code and data segment.

Types of Thread

User-Level Thread

  1. The user-level threads are managed by users and the kernel is not aware of it.
  2. These threads are faster to create and manage.
  3. The kernel manages them as if it was a single-threaded process.
  4. It is implemented using user-level libraries and not by system calls. So, no call to the operating system is made when a thread switches the context.
  5. Each process has its own private thread table to keep the track of the threads.

Kernel-Level Thread

  1. The kernel knows about the thread and is supported by the OS.
  2. The threads are created and implemented using system calls.
  3. The thread table is not present here for each process. The kernel has a thread table to keep the track of all the threads present in the system.
  4. Kernel-level threads are slower to create and manage as compared to user-level threads.

Advantages of threads

  1. Performance: Threads improve the overall performance(throughput, computational speed, responsiveness) of a program.
  2. Resource sharing: As the threads can share the memory and resources of any process it allows any application to perform multiple activities inside the same address space.
  3. Utilization of Multiple Processor Architecture: The different threads can run parallel on the multiple processors hence, this enables the utilization of the processor to a large extent and efficiency.
  4. Reduced Context Switching Time: The threads minimize the context switching time as in Thread Switching, the virtual memory space remains the same.
  5. Concurrency: Thread provides concurrency within a process.
  6. Parallelism: Parallel programming techniques are easier to implement.

Difference between process and thread

  1. Definition: Process means a program that is currently under execution, whereas thread is an entity that resides within a process that can be scheduled for execution.
  2. Termination Time: The processes take more time to terminate, whereas threads take less time to terminate.
  3. Creation Time: The process creation time takes more time as compared to thread creation time.
  4. Context Switching Time: Process context switching takes more time as compared to the thread context switching.
  5. Communication: The communication between threads requires less time as compared to the communication between processes.
  6. Resources: Processes are also called heavyweight processes as they use more resources. The threads are called light-weight processes as they share resources.
  7. Memory: A Process is run in separate memory space, whereas threads run in shared memory space.
  8. Sharing Data: Different processes have different copies of data, files, and codes whereas threads share the same copy of data, file and code segments.
  9. Example: Opening a new browser (say Chrome, etc) is an example of creating a process. At this point, a new process will start to execute. On the contrary, opening multiple tabs in the browser is an example of creating the thread.

That’s it for this blog. We will learn more about multi-threading and its models in the next blog . Hope you enjoyed this blog.

Do share this blog with your friends to spread the knowledge. Visit our YouTube channel for more content.

Keep Learning :)

Team AfterAcademy!