Introduction to Processes
From the opertng system concept curriculum
Introduction to Processes
TL;DR
A process is a program that's currently running, and it has multiple parts like code, data, and a stack. As a process runs, its state changes, tracking activity from creation to termination. The Operating System (OS) uses a Process Control Block (PCB) to manage each process.
1. The Mental Model
Think of a process as a recipe being actively cooked. The recipe book is the program code, the ingredients are data, the chef's notes are the stack, and the actual cooking is the execution. The OS is like the kitchen manager, keeping track of all the cooking processes.
2. The Core Material
What is a Process?
A process is essentially a program in execution. Its execution happens step-by-step, in a sequential fashion. Processes are made up of several key parts:
- Program Code (text section): This is the actual set of instructions.
- Current Activity: Includes the program counter (which instruction to execute next) and other processor contents (like CPU registers).
- Stack: Holds temporary data such as function parameters, return addresses, and local variables.
- Data Section: Contains global variables used by the program.
- Heap: Memory that's dynamically allocated during the program's runtime.
It's common for one program to become several processes. For example, if multiple users open the same application (like a web browser), each user's instance is a separate process.
Process States
A process doesn't stay static; it changes its state as it executes. Here are the five main states:
- new: The process is being created.
- running: Its instructions are actively being executed by the CPU.
- waiting: The process is paused, waiting for some event to happen (e.g., waiting for input/output to complete or a resource to become available).
- ready: The process is ready to run and waiting to be assigned to a CPU.
- terminated: The process has finished its execution.
graph TD
subgraph Process Life Cycle
New --> Ready;
Ready --> Running;
Running --> Waiting;
Waiting --> Ready;
Running --> Terminated;
end
Process Control Block (PCB)
The OS uses a Process Control Block (PCB), sometimes called a Task Control Block, to manage each process. Think of it as a process's ID card and status report. The PCB contains all the information associated with that specific process, including its current process state (like running, waiting, etc.).
Scheduling Queues
The OS uses different queues to manage processes based on their state and needs:
- Job queue: This contains all processes currently in the system.
- Ready queue: This holds all processes that are in main memory and ready to execute, just waiting for CPU time.
- Device queues: Processes waiting for a specific I/O device (like a disk drive or printer) are in these queues.
Processes constantly move between these queues as they execute and interact with the system.
Schedulers
Schedulers are OS components responsible for selecting which processes run and when.
- Short-term scheduler (CPU scheduler): This scheduler is very fast and frequently invoked (milliseconds). It selects a process from the ready queue to be executed next and allocates the CPU to it.
- Long-term scheduler (job scheduler): This scheduler is invoked infrequently (seconds, minutes) and can be slower. It selects processes from mass storage to be brought into the ready queue. The long-term scheduler is crucial because it controls the degree of multiprogramming, meaning how many processes are simultaneously competing for the CPU.
Processes can be categorized by their CPU and I/O usage:
- I/O-bound process: Spends more time doing I/O operations than computations. It typically has many short CPU bursts.
- CPU-bound process: Spends more time performing computations. It usually has fewer, very long CPU bursts.
Context Switch
When the CPU needs to switch its attention from one running process to another, a context switch occurs. This involves:
- Saving the current state of the process that's being paused (all its PCB information).
- Loading the saved state of the new process that will now run.
This mechanism ensures that each process can resume exactly where it left off.
When a parent process creates child processes, the OS handles their termination. A parent process can use abort() to terminate child processes. The init process in Unix-like systems periodically calls wait() to check on terminated children and collect
their status data, cleaning up their resources.
3. Worked Example
Imagine you open a web browser (let's call it Process A) and then, while it's loading a page, you start a complex spreadsheet calculation (Process B).
- Process A (Browser) is in the new state, then moves to ready.
- The short-term scheduler picks Process A, and it enters the running state to draw the browser window.
- Process A needs to fetch data from the internet (an I/O operation). It moves to the waiting state.
- While Process A is waiting, Process B (Spreadsheet) finishes creation (
new) and goes to ready. - The short-term scheduler does a context switch, saving Process A's state, and loads Process B. Process B is now running and starts its calculation.
- The browser's data arrives (I/O complete), so Process A moves from
waitingback toready. - Once Process B finishes its calculation, it can then move to the terminated state, or if the calculation is long, the CPU might do another context switch to Process A if it becomes ready again and has higher priority.
4. Key Takeaways
- A process is a program in active execution, not just the static code.
- Every process includes its code, current activity (like the program counter), stack, data section, and heap.
- Processes move through distinct states: new, ready, running, waiting, and terminated.
- The Process Control Block (PCB) is the OS's data structure that holds all vital information for a process.
- Schedulers (long-term and short-term) manage which processes get CPU time and control the degree of multiprogramming.
- A context switch is the mechanism for the CPU to switch efficiently between processes by saving and loading their states.
- Processes can be I/O-bound or CPU-bound depending on their main activity.
Common Mistakes to Avoid
- Confusing a "program" (the static code) with a "process" (the executing instance of that code).
- Forgetting that a single program can be instantiated as multiple independent processes.
- Ignoring the role of the PCB; it's the central record for each process.
- Mixing up "ready" (waiting for CPU) and "waiting" (waiting for an event other than CPU).
5. Now Try It
Think about a typical day using your computer. List three different applications you use. For each application, describe what parts of it would map to the "program code", "data section", "stack", and "heap" within its process. Also, identify one action that would cause that application's process to move into the "waiting" state and another action that would move it into the "terminated" state.
Success looks like clearly identifying the conceptual components for each application and correctly mapping user actions to process state transitions.
Frequently asked about Introduction to Processes
More from opertng system concept
Get the full opertng system concept curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Create Free Account