《操作系统英文课件:ch2a Processes and Threads》由会员分享,可在线阅读,更多相关《操作系统英文课件:ch2a Processes and Threads(48页珍藏版)》请在金锄头文库上搜索。
1、Processes and ThreadsChapter 22.1 Processes2.2 Threads2.3 Interprocess communication2.4 Classical IPC problems2.5 Scheduling22.1 Process1. Sequential ExecutionlSequentiallthe order of program structure (maybe switch or loop)lCloselOccupy all resource alonelReappearlif the initial condition be same,
2、the result be same.2. Concurrency Execution: Lose all the character3lConcurrency ProcesslMultiprogramminglProblem:compete the resource;communication; collaboration .lSolution: process concept 4The Process Modela)Multiprogramming of four programsb)Conceptual model of 4 independent, sequential process
3、esc)Only one program active at any instantlIn the process model, all runnable software is organized as a collection of processes. 5Process Conceptlfrom MULTICS and CTSS/360 system (task)lAn abstraction of a running programla program in executionl进程,即是一个具有一定独立功能的程序关于某个数据集合的一次活动。lAnalogy: birthday cak
4、e recipe; activity (reading, fetching, baking)6Difference between process and program1.Program: the collection of instruction, static concept; Process: describe concurrency, dynamic concept2.Process include program, data, and PCB3.Process: temporary; Program: permanent4.A program can be the executio
5、n program of multiple process; also, a process can call multiple programs.5.Process can create other process in it.7Process Creation (when?)lPrincipal events that cause process creation1.System initialization (reboot)2.Execution of a process creation system call (fork()3.User request to create a new
6、 processCommand line or click an icon4.Initiation of a batch joblForeground processes are those that interact with users and perform work for them.lBackground processes that handle some incoming request are called daemons.8Process CreationlIn UNIX, a fork() system call is used to create a new proces
7、s.lInitially, the parent and the child have the same memory image, the same environment strings, and the same open files. lThe execve() system call can be used to load a new program.lBut the parent and child have their own distinct address space.lIn Windows, CreateProcess handles both creation and l
8、oading the correct program into the new process. How to list the running processes?In UNIX, use the ps command.In Windows, use the task manager.9Unix Example: ps10Windows Task Manager11Process Termination (when?)lConditions which terminate processes1.Normal exit (voluntary)lExit in UNIX and ExitProc
9、ess in Windows.2.Error exit (voluntary)lExample: Compiling errors.3.Fatal error (involuntary)lExample: Core dump4.Killed by another process (involuntary)lKill in UNIX and TerminateProcess in Windows.12Process HierarchieslParent creates a child process, child processes can create its own processeslFo
10、rms a hierarchy (init)lUNIX calls this a process group“lWindows has no concept of process hierarchylall processes are created equal13Process States (1)lbasic process stateslRunning - using the CPU.lReady - runnable (in the ready queue).lBlocked - unable to run until an external event occurs; e.g., w
11、aiting for a key to be pressed.14Process States (2)Transitions between process states:15lOther process statesNew state, Exit state:Process States (3)16Suspend : Blocked, suspend stateReady, suspendProcess States (4)17Implementation of Processes(1)lWhat is in a process? Process imageUser programUser
12、dataStack (procedure call and parameter passing)PCBprocess control block (process attribute)In kernel 1819Implementation of Processes(2)Some of the fields of a typical process table entry20Process ContextThe static description of the whole execution process User Context, Register Context, System Con
13、text Context SwitchSwitch CPU from one process to anotherPerformed by scheduler (chapter 2.5) It includes: save PCB state of the old process; load PCB state of the new process;Flush memory cache;Change memory mapping;Context switch is expensive(1-1000 microseconds)No useful work is done (pure overhe
14、ad)Can become a bottleneckImplementation of Processes(3)21lProcess table or PCB tablelOs maintain a process table, each entry is PCB.lThe size of PCB table determine the concurrency degree of system.lTwo organization form:lLink lIndexImplementation of Processes(4)PCB TableReadyBlockedPCB TableIndex
15、TableReadyBlocked22Summary(1)lWhat is processlDifference between process and programlProcess states and modellWhat information in a processlProcess Context switch23lBasic attribute of Process:lResource allocate unitlScheduler unit lOS must do:lCreate processlTerminate processlSwitch ProcessSpend muc
16、h time and space, restrict the concurrency.2. Thread24 Thread conceptl如何能使多个程序更好地并发执行,同时又尽量减少系统的开销,成为设计操作系统的重要目标。l将进程的两个基本属性分开,由操作系统分开处理:l对于作为调度和分派的基本单位,不同时作为拥有资源的单位,以做到“轻装上阵”;l对于拥有资源的基本单位,又不对之进行频繁的切换。lA thread lightweight process (LWP) lThread: a basic unit of CPU utilization.lProcess: a basic unit
17、 of resource allocation.25 Thread conceptl原进程PCB的内容分成两部分:l描述进程资源和空间的部分;l描述执行现场、状态及调度的部分。将第二部分内容作为线程控制块TCB的内容,且一个进程内允许多个线程存在。l新进程描述为:l一个独立的进程空间,可装入进程映像;l一个独立的进程相关联的执行文件;l进程所用的系统资源;l一个或多个线程。(进程在创建时一般同时创建好第一个线程,其他线程按需要由用户程序请求创建)26 Thread conceptl线程不拥有系统资源,只有其运行所必需的一些数据结构:TCB, a program counter, a regis
18、ter set, and a stack. 它与该进程内其它线程共享该进程所拥有的全部资源。l堆栈和寄存器用来存储线程内的局部变量;l线程控制块TCB用来说明线程存在的标识、记录线程属性和调度信息lA traditional (heavyweight) process has a single thread of control.lIf the process has multiple threads of control, it can do more than one task at a time. This situation is called multithreading.27 Th
19、read vs Process 进程是资源分配的基本单位,所有与该进程有关的资源分配情况,如打印机、I/O缓冲队列等,均记录在进程控制块PCB中,进程也是分配主存的基本单位,它拥有一个完整的虚拟地址空间。而线程与资源分配无关,它属于某一个进程,并与该进程内的其它线程一起共享进程的资源。不同的进程拥有不同的虚拟地址空间,而同一进程中的多个线程共享同一地址空间。进程调度的切换将涉及到有关资源指针的保存及进程地址空间的转换等问题。而线程的切换将不涉及资源指针的保存和地址空间的变化。所以,线程切换的开销要比进程切换的开销小得多。28 Thread vs Process进程的调度与切换都是由操作系统内核
20、完成,而线程则既可由操作系统内核完成,也可由用户程序进行。进程可以动态创建进程。被进程创建的线程也可以创建其它线程。进程有创建、执行、消亡的生命周期。线程也有类似的生命周期。29Thread AdvantagelThread creation, shortlThread exit, shortlThread switch, shortlCommunication of threads, simple (share the memory and file resource of process) 30Single and Multithreaded Processes31The Thread M
21、odel (1)(a) Three processes each with one thread(b) One process with three threads32The Thread Model (2)lItems shared by all threads in a processlItems private to each thread33The Thread Model (3)Each thread has its own stack34Thread UsagelWhy do you use threads?lResponsiveness: Multiple activities
22、can be done at same time. They can speed up the application.lResource Sharing: Threads share the memory and the resources of the process to which they belong.lEconomy: They are easy to create and destroy.lUtilization of MP (multiprocessor) Architectures: They are useful on multiple CPU systems.lExam
23、ple - Word Processor, SpreadsheetlOne thread interacts with the user.lOne formats the document (spreadsheet).lOne writes the file to disk periodically.Why not three processes?35Thread Usage (1)A word processor with three threads36Thread Usage(2)lExample Web serverlOne thread, the dispatcher, distrib
24、utes the requests to a worker thread.l A worker thread handles the requests.37Thread Usage (2)A multithreaded Web server38Thread Usage (2)lRough outline of code for previous slide(a) Dispatcher thread(b) Worker thread39POSIX ThreadsFigure 2-14. Some of the Pthreads function calls.40POSIX ThreadsFigu
25、re 2-15. An example program using threads.41Implementation of ThreadslThree mechanismlImplementing threads in user spacelImplementing threads in kernel spacelHybrid Implementations42User Threads(ULT)lThe threads package entirely in user space, kernel doesnt know threadlThreads management done by use
26、r-level threads librarylThread switch dont need kernel privilege, user-level threads are fast to create and manage.lProblem: If the kernel is single-threaded, then any user-level thread performing a blocking system call will cause the entire process to block.lExamples of threads library- POSIX Pthre
27、ads- Mach C-threads- Solaris UI-threads43Implementing Threads in User SpaceA user-level threads package44Kernel Threads(KLT)lSupported by the Kernel: The kernel performs thread creation, scheduling, and management in kernel space.lNo thread library, kernel provide APIlKernel maintain context of proc
28、ess and threadlThread switch need kernellThread is the basic unit of Scheduler .lDisadvantage: high costlExamples - Windows 95/98/NT/2000/XP 45Implementing Threads in the KernelA threads package managed by the kernel46Hybrid Implementations Multiplexing user-level threads onto kernel-level threads(S
29、UN solaris)47Pop-Up ThreadslCreation of a new thread when message arrives(a) before message arrives(b) after message arrives48Summary(2)lWhat is thread? Why need threads?lDifference between process and threadlThread UsagelThread Implementations and their tradeoffslUser-levellKernel-levellHybridlPop-up threadslNext lecture: Inter-process Communication