[{"data":1,"prerenderedAt":572},["ShallowReactive",2],{"note:cs\u002Fos\u002Fnote\u002Fchap4":3,"site-search-catalogue":571},{"id":4,"title":5,"body":6,"description":536,"extension":563,"meta":564,"navigation":566,"path":567,"seo":568,"stem":569,"__hash__":570},"notes\u002Fsource\u002Fcs\u002Fos\u002Fnote\u002Fchap4.md","Chap. 4 Note",{"type":7,"value":8,"toc":535},"minimark",[9,14,19,23,40,58,63,69,83,88,103,109,113,128,154,157,161,171,175,182,219,223,226,238,242,269,271,275,279,302,306,309,329,331,335,339,377,381,384,422,424,428,431,457,461,464,472,474,478,482],[10,11,13],"h1",{"id":12},"chapter-4-threads-smp-and-microkernels-review-notes","Chapter 4: Threads, SMP, and Microkernels - Review Notes",[15,16,18],"h2",{"id":17},"_1-processes-and-threads-进程与线程","1. Processes and Threads (进程与线程)",[20,21,22],"p",{},"In traditional operating systems, the concept of a process embodies two independent characteristics:",[24,25,26,34],"ul",{},[27,28,29,33],"li",{},[30,31,32],"strong",{},"Resource ownership (资源所有权):"," A process includes a virtual address space to hold the process image and is allocated control or ownership of resources such as main memory, I\u002FO channels, I\u002FO devices, and files.",[27,35,36,39],{},[30,37,38],{},"Scheduling\u002Fexecution (调度\u002F执行):"," The execution of a process follows an execution path (trace) through one or more programs, and it is the entity that is scheduled and dispatched by the OS.",[20,41,42,43,46,47,50,51,46,54,57],{},"To distinguish between these two characteristics, the unit of dispatching is usually referred to as a ",[30,44,45],{},"Thread (线程)"," or ",[30,48,49],{},"Lightweight process (轻量级进程)",", while the unit of resource ownership is referred to as a ",[30,52,53],{},"Process (进程)",[30,55,56],{},"Task (任务)",".",[59,60,62],"h3",{"id":61},"_11-multithreading-多线程","1.1 Multithreading (多线程)",[20,64,65,68],{},[30,66,67],{},"Multithreading"," refers to the ability of an OS to support multiple, concurrent paths of execution within a single process.",[24,70,71,77],{},[27,72,73,76],{},[30,74,75],{},"In a multithreaded environment:"," A process is defined as the unit of resource allocation and a unit of protection. All of the threads of a process share the state and resources of that process.",[27,78,79,82],{},[30,80,81],{},"Thread Elements:"," Within a process, each thread has an execution state (Running, Ready, etc.), a saved thread context, an execution stack, per-thread static storage for local variables, and access to the shared memory and resources of its process.",[20,84,85],{},[30,86,87],{},"Benefits of Threads (线程的优势):",[89,90,91,94,97,100],"ol",{},[27,92,93],{},"It takes far less time to create a new thread in an existing process than to create a brand-new process.",[27,95,96],{},"It takes less time to terminate a thread than a process.",[27,98,99],{},"It takes less time to switch between two threads within the same process than to switch between processes.",[27,101,102],{},"Threads enhance efficiency in communication between different executing programs because threads within the same process share memory and files, allowing them to communicate without invoking the kernel.",[20,104,105,108],{},[30,106,107],{},"Uses of Threads:","\nThreads are useful for foreground and background work, asynchronous processing, speeding up execution via parallel computation, and organizing a modular program structure.",[59,110,112],{"id":111},"_12-thread-functionality-线程功能","1.2 Thread Functionality (线程功能)",[20,114,115,116,119,120,123,124,127],{},"The key execution states for a thread are ",[30,117,118],{},"Running (运行)",", ",[30,121,122],{},"Ready (就绪)",", and ",[30,125,126],{},"Blocked (阻塞)",".\nThere are four basic thread operations associated with a change in thread state:",[24,129,130,136,142,148],{},[27,131,132,135],{},[30,133,134],{},"Spawn (派生):"," When a new process is spawned, a thread is also spawned. A thread can subsequently spawn another thread within the same process.",[27,137,138,141],{},[30,139,140],{},"Block (阻塞):"," A thread blocks when it needs to wait for an event, saving its user registers, program counter, and stack pointers.",[27,143,144,147],{},[30,145,146],{},"Unblock (解除阻塞):"," When the event occurs, the thread is moved to the Ready queue.",[27,149,150,153],{},[30,151,152],{},"Finish (结束):"," When a thread completes, its register context and stacks are deallocated.",[155,156],"hr",{},[15,158,160],{"id":159},"_2-types-of-threads-线程的类型","2. Types of Threads (线程的类型)",[20,162,163,164,167,168,57],{},"There are two broad categories of thread implementation: ",[30,165,166],{},"User-Level Threads (ULT, 用户级线程)"," and ",[30,169,170],{},"Kernel-Level Threads (KLT, 内核级线程)",[59,172,174],{"id":173},"_21-user-level-threads-ult","2.1 User-Level Threads (ULT)",[20,176,177,178,181],{},"In a pure ULT facility, all of the work of thread management is done by the application (using a ",[30,179,180],{},"threads library (线程库)","), and the kernel is not aware of the existence of threads.",[24,183,184,200,213],{},[27,185,186,189],{},[30,187,188],{},"Advantages:",[89,190,191,194,197],{},[27,192,193],{},"Thread switching does not require kernel-mode privileges, saving the overhead of two mode switches.",[27,195,196],{},"Scheduling can be application-specific and tailored to the application's needs.",[27,198,199],{},"ULTs can run on any OS because no changes are required to the underlying kernel.",[27,201,202,205],{},[30,203,204],{},"Disadvantages:",[89,206,207,210],{},[27,208,209],{},"When a ULT executes a blocking system call, all of the threads within the entire process are blocked.",[27,211,212],{},"A multithreaded application cannot take advantage of multiprocessing since the kernel assigns one process to only one processor at a time.",[27,214,215,218],{},[30,216,217],{},"Jacketing (封装\u002F套套):"," A technique to overcome blocking threads by converting a blocking system call into a nonblocking application-level I\u002FO jacket routine.",[59,220,222],{"id":221},"_22-kernel-level-threads-klt","2.2 Kernel-Level Threads (KLT)",[20,224,225],{},"In a pure KLT facility, all thread management work is done by the kernel, with applications using an API to access the kernel thread facility.",[24,227,228,233],{},[27,229,230,232],{},[30,231,188],{}," The kernel can simultaneously schedule multiple threads from the same process on multiple processors, and blocking one thread does not block the entire process.",[27,234,235,237],{},[30,236,204],{}," The transfer of control from one thread to another within the same process requires a mode switch to the kernel, incurring much greater latency overhead than ULTs.",[59,239,241],{"id":240},"_23-combined-approaches-and-other-arrangements","2.3 Combined Approaches and Other Arrangements",[24,243,244,250],{},[27,245,246,249],{},[30,247,248],{},"Combined Approaches (组合方法):"," Thread creation is done entirely in user space, but multiple ULTs from a single application are mapped onto a smaller or equal number of KLTs (e.g., Solaris).",[27,251,252,255],{},[30,253,254],{},"Other Relationships:",[24,256,257,263],{},[27,258,259,262],{},[30,260,261],{},"M:N (Many-to-Many):"," Explored in systems like TRIX where a thread can move from one domain (address space) to another.",[27,264,265,268],{},[30,266,267],{},"1:M (One-to-Many):"," A thread can migrate from one process environment to another, such as in distributed operating systems like Clouds (Ra kernel) and Emerald.",[155,270],{},[15,272,274],{"id":273},"_3-multicore-and-multithreading-多核与多线程","3. Multicore and Multithreading (多核与多线程)",[59,276,278],{"id":277},"_31-performance-on-multicore","3.1 Performance on Multicore",[24,280,281,296],{},[27,282,283,286,287,291,292,295],{},[30,284,285],{},"Amdahl's Law (阿姆达尔定律):"," Defines the potential speedup of a program on multiple processors as ",[288,289,290],"code",{},"Speedup = 1 \u002F ((1 - f) + f\u002FN)",", where ",[288,293,294],{},"f"," is the infinitely parallelizable fraction. Even a small amount of serial code (e.g., 10%) severely limits the performance gain on multicore architectures.",[27,297,298,301],{},[30,299,300],{},"Beneficial Applications:"," Database management systems, multithreaded native applications, multiprocess applications, Java applications (running on the multithreaded JVM), and multi-instance applications scale well on multicore systems.",[59,303,305],{"id":304},"_32-threading-granularity-线程粒度","3.2 Threading Granularity (线程粒度)",[20,307,308],{},"Using Valve's game software as an example, there are three options for threading granularity:",[89,310,311,317,323],{},[27,312,313,316],{},[30,314,315],{},"Coarse threading (粗粒度线程):"," Individual modules (e.g., rendering, AI, physics) are assigned to individual processors.",[27,318,319,322],{},[30,320,321],{},"Fine-grained threading (细粒度线程):"," Many similar tasks (e.g., a loop) are spread across multiple processors.",[27,324,325,328],{},[30,326,327],{},"Hybrid threading (混合线程):"," A selective use of fine-grained threading for some systems, and single-threaded for other systems. This approach scales the best.",[155,330],{},[15,332,334],{"id":333},"_4-windows-process-and-thread-management-windows进程与线程管理","4. Windows Process and Thread Management (Windows进程与线程管理)",[59,336,338],{"id":337},"_41-process-and-thread-objects","4.1 Process and Thread Objects",[24,340,341,347,353,359,365,371],{},[27,342,343,346],{},[30,344,345],{},"Windows Process:"," Represents an application and contains a virtual address space, executable code, open handles, a security access token, and at least one thread.",[27,348,349,352],{},[30,350,351],{},"Windows Thread:"," The dispatchable entity within a process that can be scheduled for execution. It maintains exception handlers, a scheduling priority, and a saved context.",[27,354,355,358],{},[30,356,357],{},"Job Object (作业对象):"," Allows groups of processes to be managed as a unit (e.g., enforcing limits or terminating them together).",[27,360,361,364],{},[30,362,363],{},"Thread Pool (线程池):"," A collection of worker threads used to efficiently execute asynchronous callbacks.",[27,366,367,370],{},[30,368,369],{},"Fiber (纤程):"," A unit of execution that must be manually scheduled by the application, running in the context of the thread that scheduled it.",[27,372,373,376],{},[30,374,375],{},"User-Mode Scheduling (UMS, 用户模式调度):"," A lightweight mechanism that allows applications to switch between UMS threads in user mode without involving the system scheduler.",[59,378,380],{"id":379},"_42-thread-states-线程状态","4.2 Thread States (线程状态)",[20,382,383],{},"An existing Windows thread is in one of six states:",[24,385,386,392,398,404,410,416],{},[27,387,388,391],{},[30,389,390],{},"Ready (就绪):"," May be scheduled for execution.",[27,393,394,397],{},[30,395,396],{},"Standby (备用):"," Selected to run next on a particular processor.",[27,399,400,403],{},[30,401,402],{},"Running (运行):"," Currently executing.",[27,405,406,409],{},[30,407,408],{},"Waiting (等待):"," Blocked on an event or voluntarily waiting for synchronization.",[27,411,412,415],{},[30,413,414],{},"Transition (过渡):"," Ready to run, but resources (like paged-out stack) are not currently available.",[27,417,418,421],{},[30,419,420],{},"Terminated (终止):"," Thread has completed or been killed.",[155,423],{},[15,425,427],{"id":426},"_5-solaris-thread-and-smp-management-solaris线程与smp管理","5. Solaris Thread and SMP Management (Solaris线程与SMP管理)",[20,429,430],{},"Solaris employs a multilevel thread architecture utilizing four concepts:",[89,432,433,439,445,451],{},[27,434,435,438],{},[30,436,437],{},"Process (进程):"," The normal UNIX process with an address space.",[27,440,441,444],{},[30,442,443],{},"User-Level Threads (ULT, 用户级线程):"," Invisible to the OS, implemented via a threads library in the user address space.",[27,446,447,450],{},[30,448,449],{},"Lightweight Processes (LWP, 轻量级进程):"," Acts as a mapping between ULTs and kernel threads.",[27,452,453,456],{},[30,454,455],{},"Kernel Threads (内核线程):"," The fundamental entities scheduled and dispatched to system processors.",[59,458,460],{"id":459},"_51-interrupts-as-threads-作为线程的中断","5.1 Interrupts as Threads (作为线程的中断)",[20,462,463],{},"Solaris converts hardware interrupts to kernel threads to reduce overhead and simplify synchronization.",[24,465,466,469],{},[27,467,468],{},"Interrupt threads have their own identifier, priority, context, and stack.",[27,470,471],{},"They use standard mutual exclusion primitives for synchronization and are assigned higher priorities than all other kernel threads.",[155,473],{},[15,475,477],{"id":476},"_6-linux-process-and-thread-management-linux进程与线程管理","6. Linux Process and Thread Management (Linux进程与线程管理)",[59,479,481],{"id":480},"_61-linux-tasks-and-threads","6.1 Linux Tasks and Threads",[24,483,484,509,515],{},[27,485,486,489,490,493,494,119,497,119,500,119,503,123,506,57],{},[30,487,488],{},"Task (任务):"," A process or task in Linux is represented by a ",[288,491,492],{},"task_struct"," data structure. The execution states include ",[30,495,496],{},"Running (执行\u002F就绪)",[30,498,499],{},"Interruptible (可中断阻塞)",[30,501,502],{},"Uninterruptible (不可中断阻塞)",[30,504,505],{},"Stopped (停止)",[30,507,508],{},"Zombie (僵尸)",[27,510,511,514],{},[30,512,513],{},"Threads in Linux:"," Linux does not recognize a strict distinction between threads and processes. User-level threads are mapped into kernel-level processes that share the same group ID.",[27,516,517,524,525,527,528,531,532,534],{},[30,518,519,520,523],{},"The ",[288,521,522],{},"clone()"," System Call:"," A new process is created using the ",[288,526,522],{}," command, which allows the cloned process to share resources (like files and virtual memory) with the parent. The traditional ",[288,529,530],{},"fork()"," is just ",[288,533,522],{}," with all sharing flags cleared.",{"title":536,"searchDepth":537,"depth":537,"links":538},"",2,[539,544,549,553,557,560],{"id":17,"depth":537,"text":18,"children":540},[541,543],{"id":61,"depth":542,"text":62},3,{"id":111,"depth":542,"text":112},{"id":159,"depth":537,"text":160,"children":545},[546,547,548],{"id":173,"depth":542,"text":174},{"id":221,"depth":542,"text":222},{"id":240,"depth":542,"text":241},{"id":273,"depth":537,"text":274,"children":550},[551,552],{"id":277,"depth":542,"text":278},{"id":304,"depth":542,"text":305},{"id":333,"depth":537,"text":334,"children":554},[555,556],{"id":337,"depth":542,"text":338},{"id":379,"depth":542,"text":380},{"id":426,"depth":537,"text":427,"children":558},[559],{"id":459,"depth":542,"text":460},{"id":476,"depth":537,"text":477,"children":561},[562],{"id":480,"depth":542,"text":481},"md",{"order":565},4,true,"\u002Fsource\u002Fcs\u002Fos\u002Fnote\u002Fchap4",{"title":5,"description":536},"source\u002Fcs\u002Fos\u002Fnote\u002Fchap4","ztcm11doFA1H6S3V5-BuqAXB6z1_7t9v1EMM-1UEed8",null,1784032891894]