Processes
Process
Windows Task Manager provides information about computer performance and details of the programs and processes running on the computer.
A process is one execution of a program with a particular function operating on a data set. It is the basic unit of resource allocation; in systems with threads, a thread is the basic unit of scheduling. A process entity contains three parts:
- Program
- Data
- Process control block (PCB)
A program is static, whereas a process is dynamic. A process is created when the system loads a program and establishes its execution state and resources. A process can be considered a program in execution and needs resources such as CPU time, memory, files, and I/O devices. These resources can be allocated at creation or during execution. A program can be started by clicking in a graphical user interface (GUI) or entering its name in a command-line interface (CLI).
KNOWLEDGE QUIZ
Memory Organization
Memory normally includes a stack, heap, data region, and code region:
- Stack: normally allocated and released automatically; stores function parameters and local variables.
- Heap: normally allocated and released by the programmer or runtime; reclaimed by the OS when the program ends.
- Data: stores global and static variables; released by the system when the program ends.
- Code: stores the binary code of function bodies.
Process State Models
The two-state model divides processes into running and not running.
Not every nonrunning process is ready. Some must wait for I/O completion, so the nonrunning state can be divided into ready and blocked.
Every process has a lifecycle from creation to destruction and passes through these stages:
- New: the system is allocating the PCB and execution context; the process cannot yet be scheduled.
- Ready: the process is eligible to execute and awaits CPU scheduling.
- Running: the process has obtained CPU resources and is executing.
- Blocked: the process must wait for I/O completion or another event before continuing. An expired time slice instead returns it to ready.
- Terminated: the process has ended naturally or has been forcibly stopped and will no longer execute.
A process's five-state lifecycle
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Multiple Processes
Python's multiprocessing module provides a Process class for creating multiple processes. A Python program begins in the main process; processes created through Process are child processes.
Creating Multiple Processes
import multiprocessing
def worker():
process = multiprocessing.current_process()
print(process.pid, process.name)
def main():
workers = [multiprocessing.Process(target=worker, name=f"Worker-{index}")
for index in range(3)]
for process in workers:
process.start()
for process in workers:
process.join()
if __name__ == "__main__":
main()psutil is a third-party process-management module supporting platforms such as Linux, UNIX, macOS, and Windows. It simplifies process-management operations across systems.
Inspecting All Processes
import psutil
for process in psutil.process_iter(attrs=["pid", "name", "create_time"]):
try:
print(process.info)
except (psutil.NoSuchProcess, psutil.AccessDenied):
continueDraw an editor process's lifecycle
An editor is created and goes through the following events.
- Draw all five states and label every transition in the story.
- Explain why preemption and waiting for a file read lead to different states.
- Sketch code, data, heap, and stack regions. Distinguish the conceptual model from a fixed physical layout.