paint-brush
Comparing Threads, Processes, and Programs: Establishing Key Differencesby@0xkishan

Comparing Threads, Processes, and Programs: Establishing Key Differences

by Kishan KumarJuly 6th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A program is nothing but a set of instructions. A thread is a segment of a process. A process can have multiple threads, but why? It allows parallel processing. They are easy to create and destroy as compared to processes (10–100 times faster, actually). They are helpful in systems with multiple CPUs.
featured image - Comparing Threads, Processes, and Programs: Establishing Key Differences
Kishan Kumar HackerNoon profile picture

A program is nothing but a set of instructions.


This is an exciting interview question used to check the fundamentals of an interviewee. People often need clarification on these three terms. They might know its definition, but they are dumbstruck once asked for an example.


So what if we start this article with an example and define the formal definition later so you can flex on your colleagues?


Let's say we are on a Windows OS; we open our notepad to write a simple "Hello World" program. Do you see what I did there? Let's write this program in Java since it is the most known language, don't worry if you are unfamiliar with it; the syntax is similar to C++.


public class HelloWord {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}


Notice the file name should be the same as the class name for it to work. you can run it using the command line, java HelloWorld.java, and you'll be greeted with the following output.


Hello World


Unless you run the HelloWorld.java file, it is called a program.


A program is nothing but a set of instructions.


An instruction to loop infinitely, an instruction to check an email, an instruction to verify if the integrity of a file is compromised.


But what happens when you run it? It becomes a process.


A process is nothing but a program in execution.


Notice that a program does not require resources other than some disk space to reside on, but a process requires resources such as CPU, memory address, I/O, etc.


E.g., in the above program, its instruction was to send the output to the screen showing "Hello World" to the user. When the program runs, the CPU takes the instruction, analyzes it, and executes it.


Now let's explain what a Thread is.


A thread is a segment of a process. It is sometimes referred to as a mini process or process within a process.


A process can have multiple threads, but why? Because:

  1. It allows parallel processing.

  2. They are easy to create and destroy as compared to processes. (10–100 times faster, actually).

  3. They are helpful in systems with multiple CPUs.


It can be challenging to wrap one's head around this, but let's take an example to understand why a process can have multiple threads and their advantage over a single thread.

Suppose you are writing a letter to your friend on a word processor. The word processor itself is a running program (i.e., a process). Let's say this process is running with three threads.


Each thread is assigned a specific task:

  1. Thread One: Respond to the user when he types (Interactive Thread)

  2. Thread Two: Saves the file to the disk periodically, RAM to DISK (Background Thread)

  3. Thread Three: Render the page according to the changes made. (Computational Thread).


What if the process were single-threaded?

In that case, whenever a disk backup started, commands from the keyboard and mouse would be ignored until the backup was finished, thus making the interaction with the system sluggish.


You may ask, why three threads? Why not three processes?

Well, because all three threads operate on the same document. Threads share the same memory within a process. By having three threads, they share a common memory, and thus all have access to the edited document.


Whereas if we had three processes, we'd have to use IPC (Interprocess Communication) to communicate the information, such as the state between the processes.


I hope this brief article has resolved your doubts about the three most essential aspects asked in an Interview.


Thank you for taking the time to read this article. If you have any doubts, do let me know by commenting below.


Also published here.