Program vs Process vs Thread

This lesson discusses the differences between a program, process, and a thread. Also included is an psuedocode-example of a thread-unsafe program.

Program vs Process vs Thread

Program

A program is a set of instructions and associated data that resides on the disk and is loaded by the operating system to perform a task. An executable file (.exe), a Windows forms application, a python script file are all examples of programs. In order to run a program, the operating system's kernel is first asked to create a new process, which is an environment in which a program is executed.

Process

A process is a program in execution. A process is an execution environment that consists of instructions, user-data, and system-data segments, as well as lots of other resources such as CPU, memory, address-space, disk and network I/O acquired at runtime. A program can have several copies of it running at the same time but a process necessarily belongs to only one program.

Thread

A thread is the smallest unit of execution in a process which simply executes instructions serially. A process can have multiple threads running as part of it. Usually, there would be some state associated with the process that is shared among all the threads and in turn each thread would have some state private to itself. The globally shared state amongst the threads of a process is visible and accessible to all the threads, and special attention needs to be paid when any thread tries to read or write to this global shared state. There are several constructs offered by various programming languages to guard and discipline the access to this global state, which we will go into further detail in upcoming lessons.

Notes

Note that a "program" and a "process" are often used interchangeably, though most of the time the intent is to refer to a process.

There's also the concept of "multiprocessing" systems, where multiple processes get scheduled on more than one CPU. Usually, this requires hardware support, where a single system comes with multiple cores or the execution takes place in a cluster of machines. Processes don't share any resources amongst themselves whereas threads of a process can share the resources allocated to that particular process, including memory address space. However, languages do provide facilities to enable inter-process communication.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.