Windows 10 System Programming, Part 1

Windows 10 System Programming, Part 1

Windows 10 System Programming, Part 1 Pavel Yosifovich This book is for sale at http://leanpub.com/windows10systemprogramming This version was published on 2020-08-09 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2019 - 2020 Pavel Yosifovich Contents Introduction ........................................... 1 Who Should Read This Book ............................... 1 What You Should Know to Use This Book ....................... 1 Sample Code ........................................ 2 Chapter 1: Foundations .................................... 3 Windows Architecture Overview ............................ 3 Processes ........................................ 4 Dynamic Link Libraries ................................ 6 Virtual Memory .................................... 7 Threads ......................................... 8 General System Architecture ............................. 9 Windows Application Development ........................... 11 Your First Application ................................. 13 Working with Strings ................................... 18 Strings in the C/C++ Runtime ............................ 22 String Output Parameters ............................... 23 Safe String Functions ................................. 26 32-bit vs. 64-bit Development ............................... 28 Coding Conventions .................................... 30 C++ Usage ....................................... 31 Handling API Errors .................................... 31 Defining Custom Error Codes ............................ 34 The Windows Version ................................... 35 Getting the Windows Version ............................ 39 Exercises ........................................... 40 Summary .......................................... 41 Chapter 2: Objects and Handles ............................... 42 CONTENTS Kernel Objects ....................................... 42 Running a Single Instance Process .......................... 45 Handles ........................................... 48 Pseudo Handles .................................... 57 RAII for Handles .................................... 58 Using WIL ....................................... 60 Creating Objects ...................................... 62 Object Names ........................................ 64 Sharing Kernel Objects .................................. 67 Sharing by Name ................................... 68 Sharing by Handle Duplication ........................... 73 Private Object Namespaces ................................ 78 Bonus: WIL Wrappers for Private Namespaces . 83 Other Objects and Handles ................................ 84 User Objects ...................................... 85 GDI Objects ...................................... 86 Summary .......................................... 86 Chapter 3: Processes ...................................... 87 Process Basics ........................................ 87 Processes in Process Explorer ............................ 94 Process Creation ...................................... 98 The main Functions ..................................107 Process Environment Variables . 110 Creating Processes .....................................114 Handle Inheritance ..................................127 Process Drive Directories ...............................132 Process (and Thread) Attributes . 133 Protected and PPL Processes .............................142 UWP Processes .....................................144 Minimal and Pico Processes .............................153 Process Termination ....................................154 Enumerating Processes ..................................156 Using EnumProcesses ................................156 Using the Toolhelp Functions ............................163 Using the WTS Functions ...............................165 Using the Native API .................................171 Exercises ...........................................174 CONTENTS Summary ..........................................175 Chapter 4: Jobs ......................................... 176 Introduction to Jobs ....................................176 Creating Jobs ........................................178 Nested Jobs .........................................180 Querying Job Information .................................187 Job Accounting Information .............................188 Querying for Job Process List ............................195 Setting Job Limits ......................................197 CPU Rate Limit ....................................204 User Interface Limits .................................211 Job Notifications ......................................214 Silos .............................................219 Exercises ...........................................220 Summary ..........................................220 Chapter 5: Threads Basics .................................. 222 Introduction .........................................222 Sockets, Cores and Logical Processors . 224 Creating and Managing Threads .............................225 The Primes Counter Application . 228 Running Primes Counter ...............................234 Terminating Threads ....................................238 A Thread’s Stack ......................................240 A Thread’s Name ......................................246 What About the C++ Standard Library? . 247 Exercises ...........................................247 Summary ..........................................248 Chapter 6: Thread Scheduling ................................ 249 Priorities ...........................................249 Scheduling Basics ......................................256 Single CPU Scheduling ................................256 The Quantum .....................................262 Processor Groups ......................................265 Multiprocessor Scheduling ................................266 Affinity .........................................266 CPU Sets vs. Hard Affinity ..............................274 CONTENTS System CPU Sets ....................................275 Revised Scheduling Algorithm ............................275 Observing Scheduling ...................................276 General Scheduling ..................................277 Hard Affinity ......................................283 CPU Sets ........................................285 Background Mode .....................................292 Priority Boosts .......................................295 Completing I/O Operations ..............................296 Foreground Process ..................................296 GUI Thread Wakeup .................................297 Starvation Avoidance .................................297 Other Aspects of Scheduling ...............................297 Suspend and Resume .................................297 Suspending and Resuming a Process . 298 Sleeping and Yielding .................................299 Summary ..........................................300 Chapter 7: Thread Synchronization (Intra-Process) ................... 301 Synchronization Basics ..................................302 Atomic Operations .....................................303 The Simple Increment Application . 304 The Interlocked Family of Functions . 306 Critical Sections ......................................309 Locks and RAII .......................................314 Deadlocks ..........................................317 The MD5 Calculator Application .............................317 Calculating MD5 Hash ................................320 The Hash Cache ....................................322 Image Loads Notifications ..............................324 Event Parsing .....................................330 Putting it All Together ................................334 Reader Writer Locks ....................................338 RAII Wrappers .....................................339 MD5 Calculator 2 ...................................342 Condition Variables ....................................344 The Queue Demo Application ............................347 Waiting on Address ....................................357 CONTENTS Synchronization Barriers .................................358 What About the C++ Standard Library? . 362 Exercises ...........................................363 Summary ..........................................363 Chapter 8: Thread Synchronization (Inter-Process) ................... 364 Dispatcher Objects .....................................364 Succeeding a Wait ...................................367 The Mutex ..........................................367 The Mutex Demo Application ............................370 Abandoned Mutex ...................................376 The Semaphore .......................................377 The Queue Demo Application ............................379 The Event ..........................................383 Working with Events .................................385 The Waitable Timer ....................................387 Other Wait Functions ...................................395 Waiting in Alertable State ..............................395 Waiting on GUI Threads ...............................395 Waiting for an Idle GUI Thread . 397 Signaling and Waiting Atomically . 398 Exercises ...........................................399 Summary

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    629 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us