Difference between process and thread

Introduction

If you search for the phrase “Process vs Thread” I bet you, most of the articles you may find follow a typical structure. You will probably find a list of points in a tabular or bullet format contrasting process vs thread. Some articles are excellent but many lack a proper context. In this post, I do not have much to add however, I need to rephrase it in context and hopefully clarify any confusion.

Let us give it a try…

Processes and threads are foundational concepts in operating system design. For that reason, we need a proper ground for our discussion. Let us briefly describe the general purpose computing system in a bottom up fashion in order to see where a process and thread fit…

Basic computer architecture

  • Roughly speaking, any general purpose computing system has the following architecture (permanent storage i.e. disk, memory or RAM and CPU). For more information, you may check the following article
  • The operating system sits between computer hardware (i.e. architecture) and user applications (i.e. programs).
  • If we take a Linux user program, it consists from the following parts (code or instructions, data such as variables and arrays , stack and heap memory). For more information about stack and heap, you may check the following article
  • The operating system loads a user program from disk into RAM for execution by the CPU. Recall that entire programs cannot fit completely in physical memory. Modern operating systems implement paging to solve that problem. For more information about paging and segmentation, you may check the following articles: page tables and paging vs segmentation
  • Operating systems utilize a shared CPU to run multiple user programs at the same time. You may check the following article for more information

The question is, where does process and thread fit in the aforementioned introduction ? Well, we partially answered that question. To elaborate, a process is essentially the loaded program into RAM (remember code, data, stack and heap). Is that it ? not exactly, the concept of a process in an operating system is not just the loaded program. It is more than that. Code, data, stack and heap is good to start with but also managing the running instance of user program requires the operating system to keep track of more information. A process is all of that so what information the operating system needs to manage a running instance of a computer program ?

The operating system uses a special data structure called process control block (PCB) to keep track of each process. PCB keeps track of information like…

  • Process ID
  • Process state (ex. running, waiting)
  • Open files and network connections
  • Process owner and permissions
  • Paging and segmentation info
  • Scheduling info
  • CPU registers (ex. PC)
  • More

What is a process ?

As you can see, a process is not merely the running code of a user program. A process is the address space that encompasses code, data and resources for easy management. if you are curious, on a Unix based system such as Mac, you can use the top command to list running processes. Take a look at the following screenshot:

Let us now discuss threads…

What is a thread?

Threading is a novel concept to divide process code into logical tasks such that they run concurrently. To achieve concurrency we need two requirements

  • Threads share code, data, and other resources in a single address space (parent process space)
  • A thread has to have its own program counter, stack, and registers

Using threads gives us the benefit of responsiveness. If one thread or task is blocked other threads can still run. For example.

  • Typing and printing in word processor
  • Serving multiple requests by a website

In short, a thread is the basic unit of CPU utilization. A process should have at least one thread called the main thread. In other words there are no independent threads but processes are independent.

For more information on threads you may check the following article

Now let us summarize…

Summary

A process is an instance of a computer program that is being executed plus all the information required to manage it. A thread is an execution stream within a given process. Processes and threads is a big topic in operating systems design. In this post the goal was to clarify the key difference between the two. You may need to read more to familiarize yourself with the topic (ex. context switching, interprocess communication, thread synchronization, etc).

Difference between process and thread

You can find this article in PDF format here

That is it for today. Thanks for visiting. Please use the comments section below for feedback

Add a Comment

Your email address will not be published. Required fields are marked *