480 likes | 575 Views
Lecture 8 Processes, Threads, and Jobs (2). xlanchen@04/08/2005. Contents. The internal structures of process How to create a process The internal structures of thread How to create a thread Thread Scheduling Job Objects. Thread structures.
E N D
Lecture 8 Processes, Threads, and Jobs (2) xlanchen@04/08/2005
Contents • The internal structures of process • How to create a process • The internal structures of thread • How to create a thread • Thread Scheduling • Job Objects Understanding the Inside of Windows2000
Thread structures • a Windows 2000 thread is represented by ETHREAD block in the system address spaceexcept TEB How is the thread related to Csrss and Win32k.sys Understanding the Inside of Windows2000
Fibers vs. Threads • Fibers • "lightweight" threads • implemented in user mode in Kernel32.dll • allow an App. to schedule its own "threads" outside the priority-based scheduling mechanism of 2K • ConvertThreadToFiber, win32 function • CreateFiber • SwitchToFiber Understanding the Inside of Windows2000
Structure of the kernel thread block Understanding the Inside of Windows2000
EXPERIMENT • Displaying ETHREAD and KTHREAD Structures Understanding the Inside of Windows2000
Fields of the TEB Understanding the Inside of Windows2000
EXPERIMENT • Examining the TEB Understanding the Inside of Windows2000
Related Kernel Variables • PspCreateThreadNotifyRoutine • PspCreateThreadNotifyRoutineCount • PspCreateProcessNotifyRoutine • PspCreateProcessNotifyRoutineCount • PspCidTable Understanding the Inside of Windows2000
Process: Priority Base Thread: % Privileged Time % Processor Time % User Time Context Switches/Sec Elapsed Time ID Process ID Thread Priority Base Priority Current Start Address Thread State Thread Wait Reason Related Performance Counters Understanding the Inside of Windows2000
Win32 Thread Functions • CreateThread • CreateRemoteThread • ExitThread • TerminateThread • GetExitCodeThread • GetThreadTimes • Get/SetThreadContext • GetThreadSelectorEntry Understanding the Inside of Windows2000
EXPERIMENT • Using the Kernel Debugger !thread Command Understanding the Inside of Windows2000
EXPERIMENT • Viewing Thread Information Understanding the Inside of Windows2000
Create a thread • CreateThread to create a Win32 thread • In Kernel32.dll • Creates a user-mode stack • Initializes the thread's hardware context • Calles NtCreateThread to create the executive thread object in the suspended state • Notifies the Win32 subsystem about the new thread • Return the thread handle and its ID to the caller Understanding the Inside of Windows2000
the thread starts running • KiThreadStartup • PspUserThreadStartup • LdrInitializeThunk (in Ntdll.dll) Understanding the Inside of Windows2000
Flow of CreateThread Understanding the Inside of Windows2000
2K Scheduling • priority-driven, preemptive • the highest-priority runnable (ready) thread always runs, • processoraffinity • quantum • the length of time a thread is allowed to run before Windows 2000 interrupts the thread to find out whether another thread at the same priority level or higher is waiting to run or whether the thread's priority needs to be reduced Understanding the Inside of Windows2000
EXPERIMENT • Viewing Ready Threads Understanding the Inside of Windows2000
EXPERIMENT • Thread-Scheduling State Changes Understanding the Inside of Windows2000
Thread priority levels Understanding the Inside of Windows2000
Kernel priorities Win32 vs. Windows 2000 Understanding the Inside of Windows2000
kernel's dispatcher • dispatching occurs at DPC/dispatch level and is triggered by any of the following events • A thread becomes ready to execute • A thread leaves the running state • because its time quantum ends, it terminates, or it enters a wait state • A thread's priority changes • The processor affinity of a running thread changes. Understanding the Inside of Windows2000
Win32 Scheduling APIs • Suspend/ResumeThread • Suspends or resumes a paused thread from execution. • Get/SetPriorityClass • Returns or sets a process's priority class (base priority). • Get/SetThreadPriority • Returns or sets a thread's priority (relative to its process base priority). Understanding the Inside of Windows2000
Get/SetProcessAffinityMask • Returns or sets a process's affinity mask. • SetThreadAffinityMask • Sets a thread's affinity mask (must be a subset of the process's affinity mask) for a particular set of processors, restricting it to running on those processors. Understanding the Inside of Windows2000
Get/SetThreadPriorityBoost • Returns or sets the ability for Windows 2000 to boost the priority of a thread temporarily (applies only to threads in the dynamic range). • SetThreadIdealProcessor • Establishes a preferred processor for a particular thread but doesn't restrict the thread to that processor. • Get/SetProcessPriorityBoost • Returns or sets the default priority boost control state of the current process. (This function is used to set the thread priority boost control state when a thread is created.) Understanding the Inside of Windows2000
SwitchToThread • Yields execution for one or more quantums. • Sleep • Puts the current thread into a wait state for a specified time interval (figured in milliseconds [msec]). A zero value relinquishes the rest of the thread's quantum. • SleepEx • Causes the current thread to go into a wait state until either an I/O completion callback is completed, an APC is queued to the thread, or the specified time interval ends. Understanding the Inside of Windows2000
EXPERIMENT • Examining and Specifying Process and Thread Priorities Understanding the Inside of Windows2000
Real-Time Priorities • Increase scheduling priority privilege • 2K is not a true real-time operating system Understanding the Inside of Windows2000
Interrupt Levels vs. Priority Levels Understanding the Inside of Windows2000
Thread states Understanding the Inside of Windows2000
Quantum • Quantum Accounting • Default: 6 • Per clock interrupt: -3 ??? • The clock interval is determined by hardware platform • Controlling the Quantum Understanding the Inside of Windows2000
EXPERIMENT • Determining the Clock Interval Frequency Understanding the Inside of Windows2000
Scheduling Data Structures Understanding the Inside of Windows2000
Scheduling Scenarios • Voluntary Switch • Preemption • Quantum End • Termination Understanding the Inside of Windows2000
Voluntary switching Understanding the Inside of Windows2000
Preemptive thread scheduling Understanding the Inside of Windows2000
Quantum End Understanding the Inside of Windows2000
Context Switching • A thread's context and the procedure for context switching vary depending on the processor's architecture • A typical context switch requires saving and reloading the following data: • Program counter • Processor status register • Other register contents • User and kernel stack pointers • A pointer to the address space in which the thread runs (the process's page table directory) Understanding the Inside of Windows2000
Idle Thread • Dispatched when no runnable thread exists on a CPU • Each CPU is allotted one idle thread • the basic flow • Enables and disables interrupts (allowing any pending interrupts to be delivered). • Checks whether any DPCs are pending on the processor. If DPCs are pending, clears the pending software interrupt and delivers them. • Checks whether a thread has been selected to run next on the processor, and if so, dispatches that thread. • Calls the HAL processor idle routine (in case any power management functions need to be performed). Understanding the Inside of Windows2000
Priority Boosts • In five cases, Windows 2000 can boost (increase) the current priority value of threads • On completion of I/O operations • After waiting on executive events or semaphores • After threads in the foreground process complete a wait operation • When GUI threads wake up because of windowing activity • When a thread that's ready to run hasn't been running for some time (CPU starvation) Understanding the Inside of Windows2000
Priority boosting and decay Understanding the Inside of Windows2000
EXPERIMENT • Watching Foreground Priority Boosts and Decays Understanding the Inside of Windows2000
EXPERIMENT • Watching Priority Boosts on GUI Threads Understanding the Inside of Windows2000
EXPERIMENT • Watching Priority Boosts for CPU Starvation Understanding the Inside of Windows2000
Scheduling on SMP Systems • Affinity • Ideal and Last Processor • Choosing a Processor for a Ready Thread • Selecting a Thread to Run on a Specific CPU • When the Highest-Priority Ready Threads Are Not Running ? Understanding the Inside of Windows2000
Job Objects • A job object is a nameable, securable, shareable kernel object that allows control of one or more processes as a group. • basic function • to allow groups of processes to be managed and manipulated as a unit Understanding the Inside of Windows2000
A process can be a member of only one job object • Win32 API Functions for Jobs • CreateJobObject • OpenJobObject • AssignProcessToJobObject • TerminateJobObject • SetInformationJobObject • QueryInformationJobObject Understanding the Inside of Windows2000
EXPERIMENT • Viewing the Job Object Understanding the Inside of Windows2000