Sitemap
Nile Okomo

Nile Okomo

Software Engineer | Data Scientist | Ethical Hacker | Book worm | Writer | Chess enthusiast | Musician

Day 31: Processes

4 min readJun 26, 2025

How devices run many programs at once

Press enter or click to view image in full size
Photo by Kelly Sikkema on Unsplash

Something happens when you double-click on an icon on your machine or tap your app icon on your smartphone. Your OS (may be Windows, Linux or Android, etc) loads the program in memory and your CPU starts executing the instructions. A process is fundamentally a running program. When you have a program on your hard drive (or any storage means basically), it’s just a collection of instructions and data doing nothing. The moment the operating system loads that program into memory and starts executing it, it becomes a process. If a program is like a recipe in a cookbook, then a process is like actually cooking that recipe in your kitchen.

Your computer usually appears to be running many programs simultaneously: your web browser, music player, text editor, and dozens of background programs. However, most computers only have a few CPU cores, so how does this work? The operating system creates this “illusion” through time sharing. This is when it rapidly switches between processes. This happens so quickly that each program appears to run continuously, even though it’s actually sharing the CPU with many other processes.

Every process has what’s called machine state. This is everything the program needs to keep track of while it’s running. This includes the process’s memory (called its address space), which contains both the program’s instructions and its data. The process also has registers (small and fast storage spots in the CPU). Finally, processes often have input/output information, like which files they currently have open. When the operating system creates a new process, it follows a specific sequence. First, it loads the program’s code and initial data from disk into memory. Then it allocates memory for the program’s stack, which handles function calls, parameters, and local variables. For C programs, it also sets up the heap, which is used when the program calls malloc() to request memory dynamically. The OS also sets up standard input/output connections so the program can read from the keyboard and write to the screen. Finally, it jumps to the program's main() function to begin execution.

A process is said to be in the “running” state when it’s actually executing instructions on the CPU. It’s “ready” when it could run but the operating system has chosen to run something else instead. A process becomes “blocked” when it’s waiting for something to happen, like reading data from a disk or waiting for network input. The operating system constantly moves processes between these states. When it decides to switch from one running process to another, it “deschedules” the first (moving it to ready) and “schedules” the second (moving it from ready to running).

Get Nile Okomo’s stories in your inbox

Join Medium for free to get updates from this writer.

The operating system keeps track of all this information using data structures called Process Control Blocks (PCBs). Here’s what a simplified version looks like in the xv6 operating system:

struct proc {
char *mem; // Process memory location
uint sz; // Memory size
enum proc_state state; // Current state (running, ready, blocked)
int pid; // Process ID number
struct proc *parent; // Parent process
struct file *ofile[NOFILE]; // Open files
struct context context; // Saved CPU registers
};

The context struct is particularly important because it stores all the CPU registers when a process isn’t running:

struct context {
int eip; // Instruction pointer - where to resume execution
int esp; // Stack pointer
int ebx, ecx, edx, esi, edi, ebp; // Other CPU registers
};

When the operating system switches from one process to another, it saves the current process’s registers in its context structure, then loads the next process’s registers from its context structure. This is called a context switch, and it’s how the OS can pause one program and continue with another exactly where it previously left off.

Operating systems provide a standard set of functions for managing processes. You can create new processes (like when you double-click a program icon), destroy processes that are misbehaving, wait for a process to finish, suspend and resume processes, and check on a process’s status. These operations form the process API that programs use to manage other programs. The cool takeaway is that the operating system separates the low-level mechanisms (like context switching) from the high-level policies (like deciding which process to run next). The mechanisms are the “how”, they provide the ability to switch between processes and the policies are the “which”, they decide which process should run based on priorities, fairness, and performance goals. This separation makes the system modular and allows the OS to change scheduling strategies easily when necessary.

Thats all for today Nerds!! Hack responsibly and see you tomorrow!!! Get outta here!!!

--

--

Nile Okomo
Nile Okomo

Written by Nile Okomo

Software Engineer | Data Scientist | Ethical Hacker | Book worm | Writer | Chess enthusiast | Musician