1. The Abstraction - The Process
The process
it is a running program.The program itself is a lifeless thing: it just sits there on the disk, a bunch of instructions and maybe some static data, waiting to spring into action.
How CPU run many process at the same time
-
The OS creates this illusion by virtualizing the CPU. By running one process, then stopping it and running another, and so forth,
-
the OS can promote the illusion that many virtual CPUs exist
when in fact there is only one physical CPU (or a few). -
This basic technique, known as time sharing of the CPU, allows users to run as many concurrent processes as they would like.
-
the potential cost is performance, as each will run more slowly if the CPU(s) must be shared.
To implement virtualization of the CPU
To implement virtualization of the CPU, and to implement it well, the OS will need both some low-level machinery and some high-level intelligence. We call the low-level machinery mechanisms,For example, we’ll learn later how to implement a context switch.
[!tip] mechanisms
mechanisms are low-level methods or protocols that implement a needed piece of functionality.
Key Components of a Process
To fully understand a process, we need to look at its machine state, which includes everything the program interacts with while running.
-
Memory (Address Space):
- The process's instructions and data are stored in memory.
- The part of memory a process can access is called its address space.
-
Registers:
- These are small storage locations in the CPU that hold temporary data.
- Some important registers include:
- Program Counter (PC): Tracks the next instruction to execute.
- Stack Pointer & Frame Pointer: Manage function calls, local variables, and return addresses.
-
I/O and Storage:
- A process may interact with files and storage devices.
- It maintains a list of open files and other resources.
Process API & How the OS Manages Processes
A Process API is the set of functions that an operating system (OS) provides to manage processes. Every modern OS includes these fundamental operations:
-
Create 🛠️
- The OS must provide a way to create new processes.
- This happens when you type a command in the terminal or double-click an application.
-
Destroy (Terminate) ❌
- Sometimes, a process needs to be forcefully stopped.
- Most processes exit naturally when they finish, but if they misbehave or hang, users can kill them manually.
-
Wait ⏳
- A process may need to wait for another process to complete execution before continuing.
- This is useful when a parent process needs to wait for a child process.
-
Miscellaneous Controls ⏸️▶️
- Processes can sometimes be paused (suspended) and later resumed.
- This helps with system resource management and multitasking.
-
Status Information 📊
- The OS provides details about a process, such as:
- How long it has been running
- Its current state (running, waiting, etc.)
- The OS provides details about a process, such as:
Why is This Important?
The Process API ensures that users and the OS can efficiently manage running programs, improving system stability and control.
Process Creation: How a Program Becomes a Running Process
When you run a program, the operating system (OS) must perform several steps to transform it into a process. Here’s a breakdown of how this works:
1. Loading the Program into Memory
- Programs are stored as executable files on disk (HDD/SSD).
- The OS loads the program’s code and static data (e.g., initialized variables) into memory, creating an address space for the process.
- Older OSes loaded everything at once (eager loading), but modern OSes load pieces as needed (lazy loading) to save memory.
2. Setting Up Memory Structures
- Stack: Used for local variables, function parameters, and return addresses.
- The OS initializes the stack and sets up
argc
andargv
(command-line arguments) formain()
.
- The OS initializes the stack and sets up
- Heap: Used for dynamically allocated memory (
malloc()
in C).- Starts small but grows as needed when the program requests more memory.
3. Handling Input/Output (I/O)
- The OS sets up default file descriptors in UNIX-like systems:
- Standard Input (stdin) – Reads user input (e.g., keyboard).
- Standard Output (stdout) – Prints output to the screen.
- Standard Error (stderr) – Displays error messages.
4. Starting Execution
- After setting up memory and I/O, the OS jumps to the
main()
function, handing over control to the program. - This marks the start of process execution.
Process States in an Operating System
A process state represents what a process is currently doing at any given moment. In a simplified model, a process can be in one of three states:
1. The Three Process States
📌 Running
- The process is actively executing instructions on the CPU.
- Only one process per core can be in this state at a time.
📌 Ready
- The process is ready to run but is waiting for the CPU.
- It could run if the OS scheduler selects it.
📌 Blocked
- The process cannot run because it is waiting for an event (e.g., I/O operation completion).
- Example: A process reading from a file is blocked until the read operation finishes.
2. How Processes Transition Between States
- Running → Blocked: The process starts an I/O operation (e.g., reading a file) and must wait.
- Running → Ready: The OS pauses the process (context switch) to allow another process to run.
- Ready → Running: The OS schedules the process to execute on the CPU.
- Blocked → Ready: The I/O operation completes, and the process becomes eligible to run.
3. Example Scenario
Case 1: Two CPU-Only Processes
- Process0 starts and uses the CPU.
- Process1 is in the Ready state.
- The OS switches between them to share CPU time.
Case 2: A Process Performs I/O
- Process0 starts and uses the CPU.
- It requests an I/O operation (e.g., reading a file) → Blocked state.
- The OS starts running Process1.
- Once the I/O is done, Process0 moves to Ready.
- The OS may allow Process1 to finish first before running Process0 again.
4. Why Process States Matter
✅ Maximizing CPU Usage – The OS schedules another process when one is waiting for I/O.
✅ Efficient Multitasking – Multiple processes can share CPU time.
✅ Foundation for Scheduling – These state transitions help decide which process runs next.
Summary: OS Data Structures for Process Management
The Operating System (OS) manages processes using data structures that track process state, memory, files, and registers.
1️⃣ Key OS Data Structures for Process Tracking
The OS maintains a process list that keeps track of all processes in the system. Each process has an associated process control block (PCB), which stores important information.
📌 Process Control Block (PCB) Stores:
- Process ID (PID): Unique identifier for each process.
- Process State: The current status of the process (Running, Ready, Blocked, etc.).
- Memory Management Info:
- Address space (memory used by the process).
- Stack (for function calls and local variables).
- Heap (for dynamic memory allocation).
- CPU Registers: Saved during a context switch (when the OS switches from one process to another).
- Parent Process Info: The process that created this one.
- File Descriptors: List of open files by the process.
2️⃣ Process States in Detail
Beyond the basic Running, Ready, and Blocked states, additional states exist:
State | Description |
---|---|
Unused | Process slot is available (not used). |
Embryo | Process is being created. |
Sleeping (Blocked) | Waiting for an event (e.g., I/O completion). |
Runnable (Ready) | Ready to run, waiting for CPU. |
Running | Actively executing on the CPU. |
Zombie | Process has finished execution but still exists in the system until the parent collects its status using wait() . |
📌 Zombie processes exist until the parent process reads their exit status. If the parent doesn't handle them, they remain in the process table until the system reclaims resources.
3️⃣ Context Switching: How the OS Manages Processes
A context switch happens when the OS switches the CPU from one process to another.
🔹 Steps of a context switch:
1️⃣ Save the current process’s registers and program counter (PC) into its PCB.
2️⃣ Load the next process’s saved registers and PC from its PCB.
3️⃣ Transfer CPU control to the new process.
👀 Example:
- Process A is running but needs to read a file → it enters the Blocked state.
- The OS switches to Process B, saving A’s state.
- When the file read is complete, Process A moves to Ready and may get CPU time again.
4️⃣ Real-World Example: Running a Command in Linux
Imagine you run the command:
ls -l
1️⃣ The shell creates a new process (calls fork()
).
2️⃣ The OS loads the ls
program into memory.
3️⃣ The process executes and lists files.
4️⃣ Once finished, it enters the Zombie state until the parent (shell) collects its exit status.
5️⃣ The OS cleans up the process, removing it from the process list.
Other Posts
BLEU: The Brief
Building, Learning, Exploring, Uniting. a Tech Community Rooted in Action. you'll get to know the "why" of BLEU
مقدمة صغيرة عن لغة كوتلن
لغة كوتلن: كوتلن والـ JVM لغة برمجة كوتلن (Kotlin) هي واحدة من اللغات اللي بتشتغل على الـ JVM (Java Virtual...