Foundations Level: Beginner Chapter 1: How Windows Works Underneath
Processes and Threads
A program is a file on disk, a process is that program running, and threads are the workers inside the process doing the actual work.
Lesson 2 of 5 in Chapter 1: How Windows Works Underneath. Previous step in this path: Windows Layers and System Calls. 3 learning objectives, 4 examples, and a knowledge check.
Start: what you will learn
A program is a file on disk, a process is that program running, and threads are the workers inside the process doing the actual work.
Learning objectives
- Tell a program, a process, and a thread apart
- Explain PID, private virtual address space, handles, and access tokens
- Use Get-Process to look at running processes on your own computer
Concept in simple words
What it is: A program is a file on disk, such as notepad.exe, doing nothing until you start it. A process is that program running, alive, with its own resources. A thread is a worker inside a process, and it is the thread that actually runs code, one step at a time. One process can have many threads, and each thread can be paused while it waits, for example while it waits for you to type.
Why it works
Why it works this way: Windows gives each process its own private virtual address space, which is its own private view of memory, as if it owned the whole memory. A page is a small fixed size block of that memory, often 4 KB. This privacy matters, because one process cannot easily scribble over another process memory by accident. Each process also has a handle table, which is a list of handles, and a handle is like a ticket that lets the process use a kernel object, such as a file. Each process also has an access token, which is like an identity card that says who the process runs as and what it is allowed to do.
Think of it like this
Think of it like this: A program is a recipe book sitting on a shelf. A process is a cook actually cooking from that book in a kitchen of their own. Threads are the hands doing the work, chopping, stirring, and checking the oven. Two cooks can use the same book at the same time, but they work in different kitchens, with different tools, and if one cook burns a dish, the other kitchen keeps cooking.
How it works, step by step
How it works, step by step: First, you start a program, and Windows creates a new process for it. Next, Windows gives that process a PID, which is a Process ID, a unique number that identifies that one running process. Then Windows gives it a private virtual address space, a handle table, and an access token. Then Windows creates at least one thread inside the process, and the CPU runs that thread. When a process starts another process, the first one is called the parent, and the new one is called the child.
Let us walk through an example: Open a text editor twice, and you get two processes, not one process shown twice. They share the same program name, but they have different PIDs, different private memory, and different tokens if they run as different users. If you close one window, that process ends, and the other keeps running, because each process lives its own life. That is why the PID matters so much, it is how you tell two lookalike processes apart.
When to use it
When to use it, and when not to: Use Get-Process when you want to see what is running, check a PID, or see how much CPU a process is using. Do not use a PID from an old listing without checking again, because PIDs are reused after a process ends, and a new process can get an old number. Do not end a process just because you do not recognise its name, because many system processes look strange and are meant to be there.
Start from zero: Think of three separate things, and keep them separate in your head. The program is the file, the process is the running copy, and the thread is the worker doing the steps. A file cannot run code by itself. A process cannot run code by itself either, in a strict sense. Only a thread runs code, one instruction at a time. A process is the home that holds the threads, the memory, the handles, and the token. A thread is the worker inside that home. When you say a program is running, you really mean at least one thread inside its process is running, or waiting to run.
How a process is born, step by step: First, Windows reads the program file and checks that it is a valid program. Next, Windows creates a new process object, and gives it a PID, a private virtual address space, a handle table, and an access token. Next, Windows loads the program code and the DLLs it needs into that private address space. Next, Windows creates the first thread, called the primary thread, and gives it a stack, which is its own small private workspace for function calls and local values. Last, the CPU starts running that thread at the program entry point, which is the first instruction the program is meant to run. From that moment, the program is a process.
Private virtual address space, in depth: Each process sees a large private range of addresses, as if it owned all memory. Behind the scenes, Windows maps those virtual addresses to real RAM, page by page, using page tables. A page is a fixed size block, often 4 KB. If two processes use the same virtual address, they usually mean different real pages, because their page tables point to different places. That is why one process cannot simply read another process memory by guessing an address. Shared code, such as a common DLL, can be mapped into many processes at once, but the shared part is normally read only, so one process cannot rewrite it for everyone else.
Handles, in depth: A handle is not a direct pointer to a kernel object. It is an index, like a ticket number, into the handle table of that process. When the process wants to use a file, Windows looks up the ticket in that process own table, and finds the real object. Because tickets are per process, the same number can mean a different object in a different process, so you cannot copy a handle number from one process and expect it to work in another. When a process closes a handle, or when the process ends, Windows releases that ticket. If a program keeps opening handles and never closes them, the table grows, and that slow leak is a real bug to watch for.
Access tokens, in depth: The token is the identity card of the process. It says which user the process runs as, which groups that user belongs to, and which privileges the process holds. Privileges are special rights, such as the right to shut the system down, and they are not the same as being a member of a group. When a process creates a child process, the child normally gets a copy of the parent token, unless the child is started in a different way, such as running as administrator. That is why starting the same program normally and as administrator can give two processes with different powers, even though the program file is the same.
Threads, in depth: A thread has a state. It can be running on a CPU, ready to run and waiting for a free CPU, or waiting for something, such as input, a file, or a timer. Windows switches threads very quickly, and that switching is called a context switch. On one CPU, only one thread runs at a time, but fast switching makes it look as if many programs run at once. On a machine with many CPU cores, several threads really do run at the same time, on different cores. More threads do not always mean faster work, because threads that wait on the same file, or that fight over the same data, can spend more time waiting and switching than working.
Parents, children, and PIDs, in depth: A parent process creates a child process by asking Windows to start a new program. The child gets its own PID, its own address space, and its own handle table. On Windows, ending the parent does not automatically end the child in the normal case, so a child can keep running after its parent is gone. PIDs are numbers, and Windows reuses them after a process ends, so a PID alone is only safe if you checked it just now. Very low PIDs belong to system processes, so do not be surprised to see System with a small number. That is normal, and it is not a sign of a problem by itself.
What the columns mean when you look at Get-Process: Name tells you the program name. Id tells you the PID. CPU tells you total processor time used so far, not a live percent. Handles tells you how many handle tickets that process holds right now. WorkingSet64 tells you how many bytes of that process are currently in RAM. A large WorkingSet64 does not by itself mean a leak or a fault, because a program that works with large files will legitimately hold more memory. Compare the same process over time, and look for steady growth with no good reason, rather than judging one number alone.
A worked picture with two editors: Open a text editor, type in it, and open the same editor again with a different file. You now have two processes with the same name, two PIDs, two address spaces, and two sets of threads. If you close the first window, only the first process ends. The second window, its threads, and its memory keep going, because they live in a different home. If you sort by Id, the older process usually has the lower number, but do not rely on that after PIDs have been reused. Always check the live list again before you act on a PID, and never act on a PID from an old screenshot.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-ProcessExample 1
Get-Process asks Windows for the running processes. The pipe, written as |, passes those process objects to the next command. Select-Object keeps only the Name and Id properties, and -First 5 keeps only the first five results, so two processes with the same name are easy to tell apart by their different Id values.
Get-Process | Select-Object Name, Id -First 5Illustrative output: Name Id ---- -- explorer 1234 notepad 5678 notepad 9012
Example 2
Get-Process asks Windows for the running processes. Where-Object keeps only the objects that pass a test, and here the test is in braces. Inside the test, $_ means the current process, .CPU reads its CPU property, and -gt means greater than, so only processes with a CPU value over 0 survive. Select-Object then keeps the Name, Id, and CPU properties, and -First 5 keeps only the first five results.
Get-Process | Where-Object { $_.CPU -gt 0 } | Select-Object Name, Id, CPU -First 5Illustrative output: Name Id CPU ---- -- --- explorer 1234 12.5 powershell 5678 3.2
Example 3
Get-Process asks Windows for the running processes. The -Name parameter narrows the result at the source to processes called notepad, and you type the name without .exe. The pipe, written as |, passes those process objects to the next command. Select-Object then keeps the Name, Id, and CPU properties. Look for one row per open editor window, each with a different Id, and look at CPU as total processor time so far. If you change the name to a process that is not running, you get an error, not an empty table. If you remove -Name, you get all processes instead of just the editors.
Get-Process -Name notepad | Select-Object Name, Id, CPUIllustrative output: Name Id CPU ---- -- --- notepad 5678 0.4 notepad 9012 0.1
Example 4
Get-Process asks Windows for the running processes. Sort-Object orders those objects by their WorkingSet64 property, which is how many bytes of the process are currently in RAM, and -Descending puts the largest values first. Select-Object then keeps the Name, Id, and WorkingSet64 properties, and -First 3 keeps only the first three results. Look for the largest memory values at the top, and remember that a large value alone is not proof of a fault. If you remove -Descending, the smallest values come first, and your top 3 list would show the lightest processes instead.
Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object Name, Id, WorkingSet64 -First 3Illustrative output: Name Id WorkingSet64 ---- -- ------------ explorer 1234 120000000 powershell 5678 80000000
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Thinking a program file and a running process are the same thing, when the file sits still on disk and the process is the live running copy.
- Thinking two processes with the same name must be the same process, when each one has its own PID and its own private memory.
- Trusting an old PID without checking again, when Windows can reuse a PID for a different process after the first one ends.
- Reading the CPU column as a live percent, when CPU in Get-Process output is total processor time used so far, not how busy the process is right now.
- Thinking closing a window always ends the process at once, when a process can linger for a short time, so check the live list again before you conclude it is gone.
- Thinking more threads always means faster work, when threads that wait on the same file or the same data can spend more time waiting and switching than working.
Knowledge check
Knowledge check
What does a private virtual address space mean for two running processes?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: A program is a file, a process is a running program with a PID, private memory, handles, and an access token, and threads are the workers inside the process. Get-Process shows you the running processes with their names and PIDs, so you can see the picture, even though its normal table does not show the parent of each process.
Next step: Services and Service Accounts