1 / 48

Lecture 8 Processes, Threads, and Jobs (2)

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.

phuc
Download Presentation

Lecture 8 Processes, Threads, and Jobs (2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8 Processes, Threads, and Jobs (2) xlanchen@04/08/2005

  2. 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

  3. 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

  4. 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

  5. Structure of the kernel thread block Understanding the Inside of Windows2000

  6. EXPERIMENT • Displaying ETHREAD and KTHREAD Structures Understanding the Inside of Windows2000

  7. Fields of the TEB Understanding the Inside of Windows2000

  8. EXPERIMENT • Examining the TEB Understanding the Inside of Windows2000

  9. Related Kernel Variables • PspCreateThreadNotifyRoutine • PspCreateThreadNotifyRoutineCount • PspCreateProcessNotifyRoutine • PspCreateProcessNotifyRoutineCount • PspCidTable Understanding the Inside of Windows2000

  10. 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

  11. Win32 Thread Functions • CreateThread • CreateRemoteThread • ExitThread • TerminateThread • GetExitCodeThread • GetThreadTimes • Get/SetThreadContext • GetThreadSelectorEntry Understanding the Inside of Windows2000

  12. EXPERIMENT • Using the Kernel Debugger !thread Command Understanding the Inside of Windows2000

  13. EXPERIMENT • Viewing Thread Information Understanding the Inside of Windows2000

  14. 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

  15. the thread starts running • KiThreadStartup • PspUserThreadStartup • LdrInitializeThunk (in Ntdll.dll) Understanding the Inside of Windows2000

  16. Flow of CreateThread Understanding the Inside of Windows2000

  17. 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

  18. EXPERIMENT • Viewing Ready Threads Understanding the Inside of Windows2000

  19. EXPERIMENT • Thread-Scheduling State Changes Understanding the Inside of Windows2000

  20. Thread priority levels Understanding the Inside of Windows2000

  21. Kernel priorities Win32 vs. Windows 2000 Understanding the Inside of Windows2000

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. EXPERIMENT • Examining and Specifying Process and Thread Priorities Understanding the Inside of Windows2000

  28. Real-Time Priorities • Increase scheduling priority privilege • 2K is not a true real-time operating system Understanding the Inside of Windows2000

  29. Interrupt Levels vs. Priority Levels Understanding the Inside of Windows2000

  30. Thread states Understanding the Inside of Windows2000

  31. 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

  32. EXPERIMENT • Determining the Clock Interval Frequency Understanding the Inside of Windows2000

  33. Scheduling Data Structures Understanding the Inside of Windows2000

  34. Scheduling Scenarios • Voluntary Switch • Preemption • Quantum End • Termination Understanding the Inside of Windows2000

  35. Voluntary switching Understanding the Inside of Windows2000

  36. Preemptive thread scheduling Understanding the Inside of Windows2000

  37. Quantum End Understanding the Inside of Windows2000

  38. 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

  39. 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

  40. 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

  41. Priority boosting and decay Understanding the Inside of Windows2000

  42. EXPERIMENT • Watching Foreground Priority Boosts and Decays Understanding the Inside of Windows2000

  43. EXPERIMENT • Watching Priority Boosts on GUI Threads Understanding the Inside of Windows2000

  44. EXPERIMENT • Watching Priority Boosts for CPU Starvation Understanding the Inside of Windows2000

  45. 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

  46. 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

  47. 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

  48. EXPERIMENT • Viewing the Job Object Understanding the Inside of Windows2000

More Related