Pages

Saturday, September 7, 2024

Program vs Process vs Thread

 


A Program is an executable file containing a set of instructions that is stored on disk. A single program can have multiple processes associated with it. For example, Chrome creates separate processes for each open browser tab.

When a program is loaded into a computer’s memory and begin executing, it becomes a Process. The newly activated process requires access to essential resources like registers, a program counter, and a call stack to operate.

Within the context of an executing process, a Thread represents the smallest sequence of instructions that can be managed independently by the operating system scheduler.

The relationship between a program, a process, and a thread can be summarized in three steps:

1. A program starts out as a static set of instructions held in an executable file.
2. When loaded into memory, the previously inert program activates into one or more running processes.
3. After a process initializes, it acquires memory and resources from the operating system. A single process can then further subdivide itself into one or more threads if needed. For example, Microsoft Word often devotes one thread to spellchecking duties and another to inserting text into a document.

There are several key differences between processes and threads:

🔹 Processes tend to execute independently of each other, while threads exist within the context of a parent process.
🔹 Individual processes do not share the same memory space, while threads belonging to the same parent process can access shared memory.
🔹 Processes require more overhead to initialize and terminate than threads. They are heavyweight operations.
🔹 Transitioning between processes requires more expensive context switching compared to transitioning between threads.
🔹 Communication and data sharing can happen faster between threads since they inhabit the same process context.



No comments:

Post a Comment