Khoa học máy tính - Chapter 5: Processes and threads
A thread is an alternative model of execution of a program
Overhead of switching between threads is much less than the overhead of switching between processes
Three models of threads:
Kernel-level threads
User-level threads
Hybrid threads
Thread models have different implications for switching overhead, concurrency, and parallelism
48 trang |
Chia sẻ: nguyenlam99 | Lượt xem: 829 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Khoa học máy tính - Chapter 5: Processes and threads, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 5Processes and ThreadsCopyright © 20081Operating Systems, by Dhananjay Dhamdhere*IntroductionProcesses and ProgramsImplementing ProcessesThreadsCase Studies of Processes and Threads2Operating Systems, by Dhananjay Dhamdhere*Processes and ProgramsWhat Is a Process?Relationships between Processes and ProgramsChild ProcessesConcurrency and Parallelism3Operating Systems, by Dhananjay Dhamdhere*What Is a Process?A process comprises six components:(id, code, data, stack, resources, CPU state)4Operating Systems, by Dhananjay Dhamdhere*Relationships between Processes and ProgramsA program is a set of functions and proceduresFunctions may be separate processes, or they may constitute the code part of a single process5Operating Systems, by Dhananjay Dhamdhere*Child ProcessesKernel initiates an execution of a program by creating a process for itPrimary process may make system calls to create other processesChild processes and parents create a process treeTypically, a process creates one or more child processes and delegates some of its work to each Multitasking within an application6Operating Systems, by Dhananjay Dhamdhere*Child Processes (continued)7Operating Systems, by Dhananjay Dhamdhere*Example: Child Processes in a Real-Time Application8Operating Systems, by Dhananjay Dhamdhere*Concurrency and ParallelismParallelism: quality of occurring at the same timeTwo tasks are parallel if they are performed at the same timeObtained by using multiple CPUsAs in a multiprocessor systemConcurrency is an illusion of parallelismTwo tasks are concurrent if there is an illusion that they are being performed in parallel whereas only one of them may be performed at any timeIn an OS, obtained by interleaving operation of processes on the CPUBoth concurrency and parallelism can provide better throughput9Operating Systems, by Dhananjay Dhamdhere*Implementing ProcessesTo OS, a process is a unit of computational workKernel’s primary task is to control operation of processes to provide effective utilization of the computer system10Operating Systems, by Dhananjay Dhamdhere*Process States and State Transitions11Operating Systems, by Dhananjay Dhamdhere*Process States and State Transitions (continued)A state transition for a process is a change in its stateCaused by the occurrence of some event such as the start or end of an I/O operation12Operating Systems, by Dhananjay Dhamdhere*Process States and State Transitions (continued)1314Operating Systems, by Dhananjay Dhamdhere*Example: Process State Transitions A system contains two processes P1 and P215Operating Systems, by Dhananjay Dhamdhere*Suspended ProcessesA kernel needs additional states to describe processes suspended due to swapping16Operating Systems, by Dhananjay Dhamdhere*Process Context and the Process Control BlockKernel allocates resources to a process and schedules it for use of the CPUThe kernel’s view of a process is comprised of the process context and the process control block17Operating Systems, by Dhananjay Dhamdhere*18Operating Systems, by Dhananjay Dhamdhere*Context Save, Scheduling, and DispatchingContext save function:Saves CPU state in PCB, and saves information concerning contextChanges process state from running to readyScheduling function:Uses process state information from PCBs to select a ready process for execution and passes its id to dispatching functionDispatching function:Sets up context of process, changes its state to running, and loads saved CPU state from PCB into CPUFlushes address translation buffers used by MMU19Operating Systems, by Dhananjay Dhamdhere*Event HandlingEvents that occur during the operation of an OS:Process creation eventProcess termination eventTimer eventResource request eventResource release eventI/O initiation request event20Operating Systems, by Dhananjay Dhamdhere*Event Handling (continued)Events that occur during the operation of an OS (continued):I/O completion eventMessage send eventMessage receive eventSignal send eventSignal receive eventA program interruptA hardware malfunction event21Operating Systems, by Dhananjay Dhamdhere*Event Handling (continued)When an event occurs, the kernel must find the process whose state is affected by itOSs use various schemes to speed this upE.g., event control blocks (ECBs)22P1 initiates I/O operation on d23Operating Systems, by Dhananjay Dhamdhere*Sharing, Communication and Synchronization Between Processes24Operating Systems, by Dhananjay Dhamdhere*SignalsA signal is used to notify an exceptional situation to a process and enable it to attend to it immediatelySituations and signal names/numbers defined in OSCPU conditions like overflowsConditions related to child processesResource utilizationEmergency communications from a user to a processCan be synchronous or asynchronousHandled by process-defined signal handler or OS provided default handler25Operating Systems, by Dhananjay Dhamdhere*Example: Signal handling26Operating Systems, by Dhananjay Dhamdhere*ThreadsA thread is an alternative model of program executionA process creates a thread through a system callThread operates within process contextUse of threads effectively splits the process state into two partsResource state remains with processCPU state is associated with threadSwitching between threads incurs less overhead than switching between processes27Operating Systems, by Dhananjay Dhamdhere*Threads (continued)28Operating Systems, by Dhananjay Dhamdhere*29Coding for use of threadsUse thread safe libraries to ensure correctness of data sharingSignal handling: which thread should handle a signal?Choice can be made by kernel or by applicationA synchronous signal should be handled by the thread itselfAn asynchronous signal can be handled by any thread of the processIdeally highest priority thread should handle itOperating Systems, by Dhananjay Dhamdhere*30Operating Systems, by Dhananjay Dhamdhere*POSIX ThreadsThe ANSI/IEEE Portable Operating System Interface (POSIX) standard defines pthreads APIFor use by C language programsProvides 60 routines that perform the following:Thread managementAssistance for data sharing─mutual exclusionAssistance for synchronization─condition variablesA pthread is created through the call pthread_create(,, ,)Parent-child synchronization is through pthread_joinA thread terminates pthread_exit call31Operating Systems, by Dhananjay Dhamdhere*32Operating Systems, by Dhananjay Dhamdhere*Kernel-Level, User-Level, and Hybrid ThreadsKernel-Level Threads Threads are managed by the kernelUser-Level ThreadsThreads are managed by thread libraryHybrid ThreadsCombination of kernel-level and user-level threads33Operating Systems, by Dhananjay Dhamdhere*Kernel-Level ThreadsA kernel-level thread is like a process except that it has a smaller amount of state informationSwitching between threads of same process incurs the overhead of event handling34Operating Systems, by Dhananjay Dhamdhere*User-Level ThreadsFast thread switching because kernel is not involvedBlocking of a thread blocks all threads of the processThreads of a process: No concurrency or parallelism35Operating Systems, by Dhananjay Dhamdhere*Scheduling of User-Level Threads Thread library maintains thread state, performs switching36Operating Systems, by Dhananjay Dhamdhere*Hybrid Thread Models Can provide a combination of parallelism and low overhead37Operating Systems, by Dhananjay Dhamdhere*Case Studies of Processes and ThreadsProcesses in UnixProcesses and Threads in LinuxThreads in SolarisProcesses and Threads in Windows38Operating Systems, by Dhananjay Dhamdhere*Processes in UnixProcess executes kernel code on an interrupt or system call, hence kernel running and user running statesA process Pi can wait for the termination of a child process through the system call wait39Operating Systems, by Dhananjay Dhamdhere*Processes in Unix (continued)40Operating Systems, by Dhananjay Dhamdhere*Processes and Threads in LinuxProcess states: Task_running, Task_interruptible, Task-uninterruptible, task_stopped and task_zombieInformation about parent and child processes or threads is stored in a task_struct Linux 2.6 supports kernel-level threads41Operating Systems, by Dhananjay Dhamdhere*Threads in SolarisThree kinds of entities govern concurrency and parallelism within a process: User threadsLightweight processes (LWPs)Provides arallelism within a processUser thread are mapped into LWPsKernel threadsSupported two different thread modelsM x N model upto solaris 81 : 1 model Solaris 8 onwardsProvides scheduler activations to avoid thread blocking and notify events42Operating Systems, by Dhananjay Dhamdhere*Threads in Solaris (continued)43Processes and Threads in WindowsEach process has at least one thread in it.Uses three control blocks per processExecutive process block: process id, a kernel process block and address of process environment blockKernel process block: process state, KTB addressesProcess environment block: information about code and heapUses three thread blocks per threadExecutive thread block contains pointer to kernel thread block and executive process blockKernel thread block: stack, state and kernel environment blockOperating Systems, by Dhananjay Dhamdhere44Operating Systems, by Dhananjay Dhamdhere*Processes and Threads in Windows(continued)45Operating Systems, by Dhananjay Dhamdhere*SummaryExecution of a program can be speeded up through either parallelism or concurrencyA process is a model of execution of a programCan create other processes by making requests to the OS through system callsEach of these processes is called its child processProvides parallelism or concurrencyOS provides process synchronization meansOS allocates resources to a process and stores information about them in the process context of the process46Operating Systems, by Dhananjay Dhamdhere*Summary (continued)To control operation of the process, OS uses notion of a process stateReady, running, blocked, terminated, suspendedOS keeps information concerning each process in a process control block (PCB)Process state and CPU state associated with processScheduler selects a ready process and dispatcher switches CPU to selected process through information found in its process context and the PCB47Operating Systems, by Dhananjay Dhamdhere*Summary (continued)A thread is an alternative model of execution of a programOverhead of switching between threads is much less than the overhead of switching between processesThree models of threads:Kernel-level threadsUser-level threadsHybrid threadsThread models have different implications for switching overhead, concurrency, and parallelism48
Các file đính kèm theo tài liệu này:
- chapter_05_9104.ppt