Java Multithreading — Processes and Threads

Process and Thread Context Switching, Do You Know the Difference?

An article on process and threads, their differences, and context switching.

Vikram Gupta
Javarevisited

--

Process and Threads-image by Author

I have seen programmers a bit confused with the process and thread-related concepts. Many others struggle with process and thread context switching. So in this article, I’ll be discussing these two concepts.

What Is Process?

A Program is a set of instructions. It is stored on a disk of a computer and hence it is Passive.

When the same program is loaded into the main memory and the OS assigns some heap memory to this program(application) is under execution is called a Process.

Hence a Process is a program under execution. So we can say it is Active.

When one process is loaded into the main memory from the secondary memory and another process is unloaded from the main memory to the secondary memory is termed the Swapping of Processes.

Process-related information is stored in something called PCB(Process control block). Generally, process id, process number, process state, program counter, registers, and opened files are stored in PCB. This PCB is stored in a protected memory area to avoid normal user access as it contains critical information.

PCB — image by Author

A process can create child processes by using the fork system calls. Different types of schedulers do different operations for processes.

Let’s see the types of schedulers and what they do :

Short-term scheduler(CPU Scheduler): Selects a process from the main memory (ready queue) using a scheduling algorithm for processing in the CPU.

Long-Term Scheduler (Job Scheduler): Which processes should be placed into the main memory(ready queue) and which should be in secondary memory is done by this scheduler.

Medium-Term Scheduler: Swapping of processes is done by a medium-term scheduler.

A single process can have multiple threads to perform different tasks. Each process has its memory (called a heap) which is not shared between different processes.

What Is Thread?

A Thread is a segment or part of a process that executes some tasks of the process. A process can have multiple threads which can run concurrently within the process.

Each thread has its own thread stack but multiple threads of a process share a common heap area of that process.

Hence Threads are lightweight and faster than processes.

Local variables are stored in thread stack, and they aren’t shared between the multiple threads.

Object’s fields are stored on the heap, and hence they are shared between the multiple threads.

What Is a Practical Example of a Process and a Thread?

A Java application under execution is an example of a Process. This application is assigned some heap memory while executing. The application has one thread called main and that is responsible for executing our code.

Similarly, we can create multiple threads within the same application performing different tasks.

What Are the Differences Between Process and Thread?

  • The process is a program under execution whereas a thread is a part of the process.
  • The process takes more time to create and terminate than threads.
  • The process consumes more resources(Memory, IO, etc) than threads.
  • The processes do not share memory (heap) whereas the threads share common heap memory of the process.
  • The process-context switching takes more time and is done by the operating system whereas thread-context switching takes less time and does not require an operating system call.

What Are the Differences Between Process and Thread Context Switching?

  • Process context switching takes place when the operating system’s scheduler saves the current state of the running program(current process) (including the state of PCB) and switches to another process whereas Thread context switching takes place when the CPU saves the current state of the thread and switches to another thread of the same process.
  • When PCS happens, the processor’s cache and Translational Lookaside Buffer get flushed, but the TCS processor’s cache and Translational Lookaside Buffer preserve their state.
  • PCS involves the heavy cost of changing the Process control block hence it is less efficient and slower whereas in the case of TCS, there is no need for swapping of PCB and hence it is efficient and faster.

What is the Relation Between a Thread and a CPU core?

A CPU core is a physical processing unit in a computer’s central processing unit (CPU) that can execute instructions independently. A thread, on the other hand, is a unit of execution within a process, which represents a sequence of instructions that can be executed independently by a CPU.

In general, the number of threads that can be executed simultaneously on a CPU is limited by the number of cores available in the CPU. Each core can execute one thread at a time, so having multiple cores allows for multiple threads to be executed in parallel, potentially leading to improved performance.

However, the relationship between threads and CPU cores is more complex than just one-to-one mapping.

In modern computer systems, threads can be scheduled dynamically on different cores by the operating system, and a single core can switch between multiple threads in order to maximize the utilization of available resources and CPU cores.

Additionally, some systems may also use techniques such as hyper-threading, where a single physical core is treated as multiple virtual cores, potentially allowing for even more threads to be executed simultaneously.

--

--

Vikram Gupta
Javarevisited

-: Empowering Developers to Ace Their Technical Interviews :-