July 07, 2023
Image by Unsplash
This is an exciting interview question 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++.
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 WorldUnless 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.
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:
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.
Subscribe to the newsletter to learn more about the decentralized web, AI and technology.
Please be respectful!