Windows Process Theads and Memory With Notes
Windows Process Theads and Memory With Notes
[email protected]
Roadmap
• What is a process?
– Represents an instance of a running program
• you create a process to run a program
• starting an application creates a process
– Process defined by:
• Address space
• Resources (e.g. open handles)
• Security profile (token)
• Every process starts with one thread
– First thread executes the program’s “main” function
• Can create other threads in the same process
• Can create additional processes
Windows Threads
• What is a thread?
– An execution context within a process
– Unit of scheduling (threads run, processes don’t run)
– All threads in a process share the same per-process
address space
• Services provided so that threads can synchronize access
to shared resources (critical sections, mutexes, events,
semaphores)
– All threads in the system are scheduled as peers to all
others, without regard to their “parent” process
Processes & Threads
Processes
ETHREAD
KTHREAD
KTHREAD
Dispatcher Header
Total User Time
Create and Exit Time
Total Kernel Time
Process ID
Kernel Stack Information
EPROCESS
Thread Start Address System Service Table
Thread Scheduling Information
Access Token
Trap Frame
Impersonation Information
Thread Local Storage
LPC Message Information
Synchronization Information
Timer Information
Pending I/O Requests List of Pending APCs
Timer Block and Wait Blocks
List of Objects Being Waiting On
TEB
Process Environment Block
• Mapped in user
Image base address
space Module list
Thread-local storage data
• Image loader, Code page data
Critical section time-out
heap manager, Number of heaps
Heap size info
Windows system Process
heap
GDI shared handle table
DLLs use this OS version no info
info Image version info
Image process affinity mask
• View with !peb
or dt nt!_peb
Thread Environment Block
Exception list
• User mode Stack base
Stack limit
data Subsyst. TIB
Fiber info
structure Thread ID
Active RPC handle
• Context for PEB
LastError value
image loader Count of owned crit. sect.
and various Current locale
User32 client info
Windows GDI32 info
OpenGL info
DLLs TLS array
Winsock data
Process Creation
• No parent/child relation in Win32
• CreateProcess() – new process with primary
thread
BOOL CreateProcess(
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation)
UNIX & Win32 comparison
• Windows API has no equivalent to fork()
• CreateProcess() similar to fork()/exec()
• UNIX $PATH vs. lpCommandLine argument
– Win32 searches in dir of curr. Proc. Image; in curr. Dir.;
in Windows system dir. (GetSystemDirectory); in Windows dir.
(GetWindowsDirectory); in dir. Given in PATH
• Windows API has no parent/child relations for processes
• No UNIX process groups in Windows API
– Limited form: group = processes to receive a console event
Opening the image to be executed
Create NT
process object
Task Manager
e
m
o
Creation of a Thread
16
15
15 variable levels
1 Used by zero page thread
0
Used by idle thread(s)
i
Special Thread Priorities
• Idle threads -- one per CPU
– When no threads want to run, Idle thread “runs”
• Not a real priority level - appears to have priority zero, but actually runs
“below” priority 0
• Provides CPU idle time accounting (unused clock ticks are charged to the idle
thread)
– Loop:
• Calls HAL to allow for power management
• Processes DPC list
• Dispatches to a thread if selected
• Zero page thread -- one per NT system
– Zeroes pages of memory in anticipation of “demand zero” page faults
– Runs at priority zero (lower than any reachable from Windows)
– Part of the “System” process (not a complete process)
Single Processor Thread
Scheduling
• Priority driven, preemptive
– 32 queues (FIFO lists) of “ready” threads
– UP: highest priority thread always runs
– MP: One of the highest priority runnable thread will be
running somewhere
– No attempt to share processor(s) “fairly” among
processes, only among threads
• Time-sliced, round-robin within a priority level
• Event-driven; no guaranteed execution period
before preemption
– When a thread becomes Ready, it either runs immediately
or is inserted at the tail of the Ready queue for its current
(dynamic) priority
Thread Scheduling
• No central scheduler!
– i.e. there is no always-instantiated routine called “the
scheduler”
– The “code that does scheduling” is not a thread
– Scheduling routines are simply called whenever events occur
that change the Ready state of a thread
– Things that cause scheduling events include:
• interval timer interrupts (for quantum end)
• interval timer interrupts (for timed wait completion)
• other hardware interrupts (for I/O wait completion)
• one thread changes the state of a waitable object upon which other
thread(s) are waiting
• a thread waits on one or more dispatcher objects
• a thread priority is changed
• Based on doubly-linked lists (queues) of Ready threads
– Nothing that takes “order-n time” for n threads
Scheduling Data Structures
Dispatcher Database
Default base prio
Default proc affinity Process
Default quantum thread thread
Process
thread thread
31 Base priority
Current priority
Processor affinity
Quantum
18
from Wait state
17
16
15
14
13
Scheduling Scenarios
Voluntary Switch
• When the running thread gives up the CPU…
• …Schedule the thread at the head of the next non-empty “ready”
queue
Running Ready
18
17
16
15
14
13
to Waiting state
Scheduling Scenarios
Quantum End (“time-slicing”)
• When the running thread exhausts its CPU quantum, it goes to the
end of its ready queue
– Applies to both real-time and dynamic priority threads, user and kernel
mode
• Quantums can be disabled for a thread by a kernel function
– Default quantum on Professional is 2 clock ticks, 12 on Server
• standard clock tick is 10 msec; might be 15 msec on some MP Pentium
systems
– if no other ready threads at that priority, same thread continues
running (just gets new quantum)
– if running at boosted priority, priority decays by one at quantum end
(described later)
Running Ready
18
17
16
15
14
13
Basic Thread Scheduling
States
preemption,
quantum end
voluntary
switch
Waiting (5)
Priority Adjustments
• Dynamic priority adjustments (boost and decay) are applied to
threads in “dynamic” classes
– Threads with base priorities 1-15 (technically, 1 through 14)
– Disable if desired with SetThreadPriorityBoost or
SetProcessPriorityBoost
• Five types:
– I/O completion
– Wait completion on events or semaphores
– When threads in the foreground process complete a wait
– When GUI threads wake up for windows input
– For CPU starvation avoidance
• No automatic adjustments in “real-time” class (16 or above)
– “Real time” here really means “system won’t change the relative
priorities of your real-time threads”
– Hence, scheduling is predictable with respect to other “real-time”
threads (but not for absolute latency)
Priority Boosting
To favor I/O intense threads:
• After an I/O: specified by device driver
– IoCompleteRequest( Irp, PriorityBoost )
Common boost values (see NTDDK.H)
1: disk, CD-ROM, parallel, Video
2: serial, network, named
pipe, mailslot
6: keyboard or mouse
8: sound
Other cases:
• After a wait on executive event or
semaphore
• After any wait on a dispatcher object by a thread in the foreground
process
• GUI threads that wake up to process windowing input (e.g. windows
messages) get a boost of 2
Thread Priority Boost and
Decay
quantum
Priority decay
at quantum end
Time
Five minutes break
Windows Memory Management
Fundamentals
• Classical virtual memory management
– Flat virtual address space per process
– Private process address space
– Global system address space
– Per session address space
• Object based
– Section object and object-based security (ACLs...)
• Lazy evaluation
– Sharing – usage of prototype PTEs (page
table entries)
– Extensive usage of copy_on_write
– ...whenever possible
• Shared memory with copy on write
• Mapped files (fundamental primitive)
– Provides basic support for file system
cache manager
Memory Manager Components
implement shared
memory
(file mapping objects in
Windows API)
Virtual Address Space
Allocation
• Virtual address space is sparse
– Address spaces contain reserved, committed, and
unused regions
• Unit of protection and usage is one page
– On x86, default page size is 4 KB (x86 supports 4KB or
4MB)
• In PAE mode, large pages are 2 MB
– On x64, default page size is 4 KB (large pages are 4 MB)
– On Itanium, default page size is 8 KB
(Itanium supports 4k, 8k, 16k, 64k, 256k, 1mb, 4mb,
16mb, 64mb, or 256mb) – large is 16MB
Large Pages
• Large pages allow a single page directory entry to map a
larger region
– x86, x64: 4 MB, IA64: 16 MB
– Advantage: improves performance
• Single TLB entry used to map larger area
• Processor support:
– About all CPU from Intel, AMD and VIA shipped in last 4 years.
Data Execution Prevention
Orig. Data
Page 1
Orig. Data
Page 2
Page 3
Process Process
Address Address
Space Space
Physical
memory
How Copy-On-Write Works
After
Orig. Data
Page 1
Mod’d. Data
Page 2
Page 3
User
User User
User
accessible
accessible accessible
accessible
v.a.s.
v.a.s. v.a.s.
v.a.s.
7FFFFFFF
Process space
}
00000000
contains: User
User Unique per
– The application accessible
accessible process
you’re running 7FFFFFFF
(.EXE and .DLLs)
80000000
}
– A user-mode stack
for each thread Kernel-mode
Kernel-mode System-
(automatic storage) accessible
accessible wide
– All static storage
defined by the FFFFFFFF
application
Virtual Address Space
(V.A.S.)
}
– Statically-allocated system- 00000000
wide data cells User
User Unique per
– Page tables (remapped for accessible
accessible process
each process)
– Executive heaps (pools) 7FFFFFFF
– Kernel-mode device drivers 80000000
}
(in nonpaged pool)
– File system cache Kernel-mode System-
Kernel-mode
– A kernel-mode stack for accessible wide
every thread in every accessible
process
FFFFFFFF
3GB Process Space Option
00000000 • Only available on operating system
newer than Windows 2000 Server.
– Can be activated from Boot.ini (Win 2k3,
Unique per Unique per
XP) or BCD (Vista, 7, 2008)
process, .EXE
process.EXEcode
code • Provides 3 GB per-process address
accessible in Globals
(= per appl.),
Globals space
user or kernel Per-thread
user mode user
Per-thread user – Commonly used by database servers
mode mode
modestacks
stacks (for file mapping)
.DLL
.DLLcode
code – .EXE must have “large address space
Process aware” flag in image header, or
Per process, Processheaps
heaps they’re limited to 2 GB (specify at link
accessible only time or with imagecfg.exe from
ResKit)
in kernel
– Chief “loser” in system space is file
mode system cache
BFFFFFFF – Better solution: address windowing
C0000000 extensions
Process page tables,
System wide, – Even better: 64-bit Windows
hyperspace
accessible
only in kernel Exec,
Exec,kernel,
kernel,HAL,
HAL,
mode drivers, etc.
drivers, etc.
FFFFFFFF
Physical Memory
XP Home 4 4 n/a
XP Professional 4 4 16 GB
http://msdn.microsoft.com/en-us/library/aa366778(VS.85).aspx
Working Set
PerfMon
Process “WorkingSet”
PerfMon
Process “WorkingSet”
• When working set max reached (or working set trim occurs), must give up
pages to make room for new pages to standby
• Local page replacement policy (most Unix systems implement global or modified
replacement)
– Means that a single process cannot take over all of physical memory page list
unless other processes aren’t using it
• Page replacement algorithm is least recently accessed
(pages are aged)
– On UP systems only in Windows 2000 – done on all systems in Windows
XP/Server 2003
• New VirtualAlloc flag in XP/Server 2003: MEM_WRITE_WATCH
Free and Zero Page Lists
• Free Page List
– Used for page reads
– Private modified pages go here on process exit
– Pages contain junk in them (e.g. not zeroed)
– On most busy systems, this is empty
• Zero Page List
– Used to satisfy demand zero page faults
• References to private pages that have not been created yet
– When free page list has 8 or more pages, a priority zero
thread is awoken to zero them
– On most busy systems, this is empty too
Paging Dynamics
demand zero page read from
page faults disk or kernel
allocations
Standby
Page
List
Modified
Page
working set List
replacement
Private pages at
process exit
8
0
Why “Memory Optimizers” are
Fraudware
Before:
Notepad Word Explorer System Available
During:
Avail. RAM Optimizer
After:
Available
DOMANDE, RICHIESTE,
SUGGERIMENTI?
GRAZIE A TUTTI PER
L’ATTENZIONE!
Copyright Notice
© 2000-2005 David A. Solomon and Mark Russinovich