Hệ điều hành - Chapter 4: Threads
Like processes, thread may synchronize with one another.
Any alteration of a resource by one thread affects other threads in the same process
consider two threads each try to add an element to a linked list at the same time
20 trang |
Chia sẻ: huyhoang44 | Lượt xem: 747 | Lượt tải: 0
Bạn đang xem nội dung tài liệu Hệ điều hành - Chapter 4: Threads, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 4ThreadsThreads: Resource ownership and executionProcesses and ThreadsProcesses have two characteristics:Resource ownership – a process includes an address space to hold the process image and may be allocated control or ownership of resourcesScheduling/execution – process execution follows an execution path that may be interleaved with other processesThese two characteristics are treated independently by the operating systemProcesses and ThreadsThe unit of dispatching is referred to as a thread or lightweight processThe unit of resource ownership is referred to as a process or taskMultithreadingThe ability of an OS to support multiple, concurrent paths of execution within a single process.Single Thread ApproachesMS-DOS supports a single user process and a single thread Some UNIX, support multiple user processes but only support one thread per processMultithreadingJava run-time environment is a single process with multiple threadsMultiple processes and threads are found in Windows, Solaris, and many modern versions of UNIXProcessesIn a multithreaded environment, a process is defined as a unit of resource allocation: a virtual address space which holds the process image a unit of protection: protected access to processors, other processes (for IPC), files, I/O resourcesOne or More Threads in ProcessEach thread hasAn execution state (running, ready, etc.)Saved thread context when not runningAn execution stackSome per-thread static storage for local variablesAccess to the memory and resources of its process (all threads of a process share this)One viewOne way to view a thread is as an independent program counter operating within a process.Threads vs. processes Threads vs. processesThe thread control block contains register values, priority, and other thread-related state information.All threads of a process share the state and resources of that process.reside in the same address spacehave access to the same datawhen one thread alters a data, other threads see the resultswhen one thread opens a file, other threads can also access that fileBenefits of ThreadsTakes less time to create a new thread than a process (can be 10 times faster)Less time to terminate a thread than a processSwitching between two threads takes less time that switching processesThreads can communicate with each other without invoking the kernelBenefits of ThreadsIf an application is implemented as a set of related units of execution, it is far more efficient to do so as a collection of threads rather than a collection of separate processesExample Applications of ThreadsIn a file server, a new thread can be spawned for the file management program per each new file request multiple threads within the same process can be executing simultaneously on different processorsIn a spreadsheet program, one thread could read user input while another thread executes user commandsIn a word processor, a thread can be created to do periodic backup asynchronouslyIn a multithreaded process, one thread can compute one batch of data while another thread reads the next batch from an I/O deviceThreads vs. processesLike processes, threads have execution states Running, Ready, and BlockedSome states are process-levelSuspend: if a process is swapped out, all of its threads are necessarily swapped out because they all share the address space of the processThreads vs. processesLike processes, thread may synchronize with one another. Any alteration of a resource by one thread affects other threads in the same processconsider two threads each try to add an element to a linked list at the same timeExample: Remote Procedure CallConsider:A program that performs two remote procedure calls (RPCs) to two different hosts to obtain a combined result.RPCUsing Single ThreadThe program has to wait for a response from each server in turn.RPC Using One Thread per ServerThe program waits concurrently for the two replies.Multithreading on a UniprocessorInterleaving of multiple threads within multiple processes on a uniprocessor
Các file đính kèm theo tài liệu này:
- chap4_778.ppt