Exploring Concurrency: A Brief Guide

Lokesh Gadiya
3 min readJan 14, 2024

--

Have you ever wondered how can OS run multiple programs together? And what if the running program has multiple task? How does the system handle multiple tasks concurrently?

To understand above mentioned problems, Let’s start with the basics.

What is Program?

A program is also known as application or software. It gives desired output by executing a set of instructions.
E.g., MS Excel, Chrome Browser etc.

What is process?

A program which is running and utilizing memory space and system resources is process.

What is Thread?

Within a process, there can be many subtasks. For example, a gaming application must concurrently update the UI and take user input. Thus, each of these subtasks is a thread.

Difference between Thread and Process
1.1 Difference between Process and a Thread

Now that we know the basics, let’s dive into multi-tasking, multi-processing, and multi-threading.

Multi-tasking

Consider a single core CPU where we are running two processes. At any instance only one process will be able to access core and its resources. No two process can access core at same time. The processor uses context switching to switch each process back and forth. This provides each process a few milliseconds for execution before switching. It creates an illusion of two processes running simultaneously. This is called multi-tasking.

Note: With modern technology like hyper-threading, it is possible to run multiple tasks simultaneously.

Multi-threading

If a process needs to perform multiple tasks concurrently, it divides these tasks into threads. For instance, when browsing the web using a browser, if you come across a file you wish to download, initiating the download creates a thread. This allows you to continue browsing other web pages while the file is downloading. This way of executing multiple threads in a single process is called multi-threading.

Multithreading is a kind of multitasking where multiple tasks being executed are part of same program.

Multi-processing

Assume you have dual core CPU and you run a single program that doesn’t use multiple threads, it will run on one core while the other sits idle. If you run multiple non-threaded programs, the computer will use both cores to run each program separately. This is called multiprocessing, where each core handles a different program at the same time.

Now you may think what if we have multiple processors.?

For that case, it would depend on the number of cores on each processor. It determines how many processes can run concurrently. Which is also known as multi-processing.

Hyper-threading

Hyper-threading makes it seem like there are two virtual processors inside each physical core of a real processor. Let's refer below image to understand it better.

1.2 System Snapshot

Referring image 1.2, system has one actual Socket (CPU) and 2 cores.

So total number of logical/virtual processors would be:

1 Socket (CPU) * x 2 (Cores) x 2 (Logical CPU per core) = 4 Logical CPU

This enables the concurrent execution of up to 4 processes or thread in a dual core CPU.

Conclusion

Multithreading and multiprocessing utilize the computer’s multitasking ability which in turn use context switching and hyper-threading.

Keep Learning :)

  • Thank you for reading this article. Please provide your valuable suggestions/ feedback on LinkedIn or in comments section below.
  • Clap and share if you liked the content.

--

--