In this article I will briefly explain what processes and threads are and their close relation.
Processes are all around us
You may not realize it, but once you click the power button to start your laptop, desktop computer or mobile device, you have just started a process (a series of processes to be exact). Every time you open an app you start a new process, every time you switch between apps you briefly block one process and run another.
So what is a process? Here is a fun real life, non-technical analogy that explains it pretty well:
Imagine you are getting ready to make an apple pie. You gather all of the necessary ingredients in the kitchen, you open your cook book and search for the recipe. You chop the apples, mix the batter, put it all together in a pan and place it in the oven to bake. After some time, your pie is ready!
Apologies if I made you hungry!
Now to explain this analogy:
- You! (the cook) are the CPU
- Your pie ingredients are the necessary input data
- Your recipe (the steps you take in order to make the pie, your algorithm) is the program/application itself
- And finally, the shier act of "making a pie" (reading the recipe, adding ingredients, baking the pie) is the process
Hope you enjoyed that little analogy and helped you understand. Now for the technical definitions.
A process is a program in execution.
They are one of the oldest and most important abstractions that operating systems provide. They support the ability to have (pseudo) concurrent operation even when there is only one CPU available! They turn a single CPU into multiple virtual CPUs. Without the process abstraction, modern computing could not exist.
In any multiprogramming system (basically every modern-day laptop, desktop computer and smartphone), the CPU switches from process to process quickly, running each for tens or hundreds of milliseconds. While, strictly speaking, at any one instant the CPU is running only one process, in the course of 1 second it may work on several of them, giving the illusion of parallelism. Once multiprocessor systems (multiple cores on a single chip) appeared, true-hardware parallelism could be achieved much more easily.
What about threads?
Now that you understand what a process is, you might be wondering where threads come into this picture. Let's shortly go back to our cooking analogy and add some threads into the mix (pun intended).
Imagine this is your first time backing a pie, so you're having a pretty hard time. You call your mom and sister to help you out! They help you gather the ingredients, cut the apples, mix the batter, etc. In the end, you receive your delicious pie!
You might have already figured it out, but the threads in this analogy are your mother and sister! They were your helpers and that's what threads are in essence.
In operating systems, each process has an address space and a single thread of control. This is the key link between processes and threads, when a process is started there is always at least one thread that does the executing. Every modern-day operating systems (Windows, MacOS, Linux, Android, iOS) provides multithreading (multiple threads of execution concurrently).
You can think of threads as a process starting another process within itself. The main reason for having threads is that in many applications, multiple activities are going on at once, so why not divide the work and finish faster? Threads share a process' data in order to achieve this, however not everything is as simple I have made it out to be in this article.
To sum it up, a process is a program in execution and a thread is a process within the main process that helps it finish its job faster. This short article is merely a scratch on the surface of processes and threads, but I hope it made you even more curious to dive deeper into the complex world of operating systems.
If you want to learn more about this topic and others regarding operating systems, I highly recommend the book "Modern Operating Systems 4th Edition" by author Andrew S. Tanenbaum.