中科大操作系统原理与实现课件4_Threads1

合集下载

ch04_Thread-1pp 操作系统 讲义

ch04_Thread-1pp 操作系统 讲义

线程的概念第4章线程n g S y s t e m s , B J U T4.1概述传统进程资源分配的单位在CPU 上执行的单位: 一个控制线索n g S y s t e m s , B J U T 为什么引入线程概念? 引入进程的目的使多个进程并发执行改善资源利用率提高系统吞吐量 问题提出一个应用程序可能需要执行多个不同的或相似的任务创建多个进程进程的创建进程的创建、、切换切换、、撤销 时空开销 引入线程的原因程序并发执行时所付出的时空开销↓n g S y s t e m s , B J U T 线程(Thread)轻量级进程(lightweight process, LWP) 使用CPU 的基本单位控制线索在CPU 上执行的单位 包含线程ID程序计数器(PC)寄存器集合栈 属于同一进程的各线程, 共享该进程的资源 代码段, 数据段, 打开的文件, ……n g S y s t e m s , B J U T多线程进程n g S y s t e m s , B J U T 多线程环境下的进程和线程 进程资源分配的单位保护的单位 线程进程中的一个实体调度和分派的基本单位只拥有运行所必须的资源 PC, 一组寄存器, 栈与同进程内的其他线程与同进程内的其他线程共享进程所拥有的资源共享进程所拥有的资源 基本状态: 运行运行、、就绪就绪、、阻塞n g S y s t e m s , B J U T 多线程的优点提高并发性响应度高共享资源 经济—降低时空开销同一进程的多个线程共享地址空间同一进程的多个线程共享地址空间、、资源 线程的创建线程的创建、、撤销撤销、、切换—开销小 易于调度线程间通信效率高 多处理器体系结构利用 一进程的多个线程可在不同处理器上并行执行n g S y s t e m s , B J U T4.2 多线程模型用户线程与内核线程 多线程模型 多对一模型 一对一模型 多对多模型 二级模型n g S y s t e m s , B J U T用户线程用户级线程库管理线程 User threads are supported above the kernel and are managed without kernel support.n g S y s t e m s , B J U T内核线程Kernel threads are supported andmanaged directly by the operating systemn g S y s t e m s , B J U Tn g S y s t e m s , B J U Tn g S y s t e m s , B J U T多对多模型n g S y s t e m s , B J U T二级模型n g S y s t e m s , B J UT Multithreaded Server Architecture (补充) 线程池n g S y s t e m s , B J U TConcurrent Execution on a Single-core Systemn g S y s t e m s , B J UT Parallel Execution on a Multicore Systemn g S y s t e m s , B J U T 本章其余内容自学 推荐编程实践。

操作系统原理第四章(英文版)课件

操作系统原理第四章(英文版)课件

1. Many-to-One Model 2. One-to-One Model 3. Many-to-Many Model
<
Operating Systems 4.15 X.J.Lee 2009
1. Many-to-One Model
Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads.
4.1 Overview
Process 资源分配单位 CPU调度单位 CPs
4.3
X.J.Lee 2009
Thread (LWP --lightweight process) a basic unit of CPU utilization a thread ID, a program counter, a register set, and a stack. -----与运行有关 It shares with other threads belonging to the same process code section, data section, and other operating-system resources ( such as open files and signals) LWP),是CPU利用的基本单元。它由 线程,有时叫做轻型进程(LWP 一个线 标识符 一个程序 数器,一个寄存器集合 一个程序计 一个寄存器集合及一个堆 一个线程标识符,一个程序计数器 一个寄存器集合 一个堆 栈组成 共享(进程的)代码段、 栈组成。它与属于同一进程的其它线程共享 共享 数据段及其他操作系统资源(如:打开的文件和信号)

[工学]操作系统原理4[兼容模式]

[工学]操作系统原理4[兼容模式]

Chapter 4: Threads沈卓炜h @d**************.cn 九龙湖校区计算机楼347Chapter 4: Threads C apte eadsOverviewM ltith di M d lMultithreading Models Thread LibrariesThreading IssuesThreading Issues Operating System ExamplesWhat is a thread?at s a t eadA also known as lightweight process A thread , also known as lightweight process (LWP), is a basic unit of CPU execution.A thread has a thread ID a program A thread has a thread ID , a program counter , a register set , and a stack . Thus, it is similar to a process has is similar to a process has.However, a thread shares with other threads in the process its code section data in the same process its code section, data section, and other OS resources (e.g ., files and signals).and signals).A process, or heavyweight process, has a single thread of control.single thread of control.Single and MultithreadedP rocessesItems shared by all threads in a process Items private to each threadThread Usage (1)ead Usage ()A word processor with three threadsThread Usage (2)ead Usage ()A multithreaded Web serverThread Usage (3)ead Usage (3)Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread ()BenefitsResponsi eness Responsiveness Resource Sharing Economy Utilization of MP Architectures Utilization of MP ArchitecturesUser Threads Use eadsThread management done b ser le el Thread management done by user-level threads library Examples-POSIX Pthreads -Mach C-threads Solaris UI--Solaris UI-threadsUser Threads (Cont.)Use eads (Co t )User threads are supported at the user level . The kernel is not aware of user threads. A library provides all support for thread creation, termination, joining, and ,,j g,scheduling.There is no kernel intervention, and, hence, ,,,user threads are usually more efficient . Unfortunately, since the kernel only y,y recognizes the containing process (of the threads), if one thread is blocked, every other threads of the same process are also blocked because the containing process is bl k d blocked.Implementing Threads in User SpaceA user-level threads package A user-level threads packageKernel Threads e e eadsS pported b the KernelSupported by the Kernel ExamplesWindows 95/98/NT/2000-Windows 95/98/NT/2000-Solaris-Tru64 UNIX-BeOS-LinuxKernel Threads (Cont.)e e eads (Co t )Kernel threads are directly supported by the kernel The kernel does thread kernel. The kernel does thread creation,termination, joining, and scheduling in kernel space scheduling in kernel space.Kernel threads are usually slower than the user threads user threads.However, blocking one thread will not cause other threads of the same process to block other threads of the same process to block . The kernel simply runs other threads.In a multiprocessor environment the kernel In a multiprocessor environment, the kernel can schedule threads on different processorsImplementing Threads in the KernelA threads package managed by thekernelChapter 4: Threads C apte eadsO er ieOverview Multithreading Models gThreading IssuesWi d XP Th d Windows XP Threads Linux ThreadsJava ThreadsWindows Threads API PthreadsMultithreading Models u t t ead g ode sMan to OneMany-to-One One-to-OneMany-to-ManyMany-to-One a y to O eMan ser le el threads mapped to single Many user-level threads mapped to single kernel thread.Used on systems that do not support kernel Used on systems that do not support kernel threads.Many-to-One Model a y to O e odeMany-to-One Model (Cont.)a y to O e ode (Co t )One-to-OneO e to O eEach ser le el thread maps to kernel Each user-level thread maps to kernel thread.Examples-Windows 95/98/NT/2000-OS/2One-to-one ModelO e to o e odeOne-to-one Model (Cont.)O e to o e ode (Co t )Many-to-Many Model a y to a y odeAllo s man ser le el threads to be Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads sufficient number of kernel threads. Solaris 2Windows NT/2000 with the ThreadFiber packageMany-to-Many Model a y to a y odeMany-to-Many Model (Cont.)a y to a y ode (Co t )Chapter 4: Threads C apte eadsO er ieOverview Multithreading Models gThreading IssuesWi d XP Th dWindows XP Threads Linux ThreadsJava ThreadsWindows Threads APIPthreadsThreading Issues ead g ssuesSemantics of fork()and e ec()s stem calls Semantics of fork() and exec() system calls. Thread cancellation.Signal handlingTh d lThread pools Thread specific data pScheduler ActivationsSemantics of fork() and exec()Se a t cs o o ()a d e ec()Does d plicate onl the calling thread Does fork()duplicate only the calling thread or all threads?Thread Cancellation ead Ca ce at oTerminating a thread before it has finished Terminating a thread before it has finished Two general approaches:Asynchronous cancellation terminates the target thread immediately g yDeferred cancellation allows the target thread to periodically check if it should be cancelled to periodically check if it should be cancelled 9The point a thread can terminate itself is a cancellation point cancellation point.Thread Cancellation (Cont.)ead Ca ce at o (Co t )With asynchronous cancellation if the target With asynchronous cancellation , if the target thread owns some system-wide resources, the system may not be able to reclaim all recourses system may not be able to reclaim all recourses With deferred cancellation , the target thread determines the time to terminate itself. Reclaiming resources is not a problem. Most systems implement asynchronous cancellation for processes (e.g.,use the cancellation for processes (e.g., use the kill system call) and threads.supports deferred cancellation Pthread supports deferred cancellation .Signal HandlingS g a a d gare used in UNIX systems toSignals are used in UNIX systems to notify a process that a particular event has doccurred All signals follow the same pattern:g p1.Signal is generated by particular event 2Signal is delivered to a process2.Signal is delivered to a process3.Signal is handledA signal handler is used to process signalsSignal Handling (Cont.)S g a a d g (Co t )Options:Deliver the signal to the thread to which the signal appliesDeliver the signal to every thread in the processp Deliver the signal to certain threads in the processAssign a specific thread to receive all signals for the process for the processThread Pools ead oo sCreate a n mber of threads in a pool Create a number of threads in a pool where they await workAdvantages:Usually slightly faster to service a request Usually slightly faster to service a request with an existing thread than create a new threadAllows the number of threads in theapplication(s)to be bound to the size of the application(s) to be bound to the size of the poolThread Specific Data ead Spec c ataAllo s each thread to ha e its o n Allows each thread to have its own copy of dataUseful when you do not have control over the thread creation process (i e over the thread creation process (i.e., when using a thread pool)Scheduler ActivationsSc edu e ct at o sBoth M:M and Two-level models require Both M:M and Two level models require communication to maintain the appropriate number of kernel threads allocated to the number of kernel threads allocated to the applicationScheduler activations provide upcalls -a communication mechanism from the kernel to communication mechanism from the kernel to the thread libraryThi i ti ll li ti t This communication allows an application to maintain the correct number kernel threadsChapter 4: Threads C apte eadsO er ieOverview Multithreading Models g Threading Issues Wi d XP Th d Windows XP Threads Linux Threads Java Threads Windows Threads API PthreadsSolaris 2 Threads So a s eadsSolaris ProcessSo a s ocessWindows XP Threads do s eadsImplements the one to one mapping Implements the one-to-one mapping. Each thread contains -a thread idi t t-register set -separate user and kernel stacks p-private data storage areaThread BlockETHREADKTHREAD KTHREADDispatcher Header Create and Exit TimeProcess IDThread Start Address EPROCESSTotal User Time Total Kernel TimeSystem Service Table Kernel Stack Information Thread Start AddressImpersonation Information LPC Message InformationAccess TokenTimer Information Thread Scheduling InformationSynchronization Information System Service TableThread Local StorageTrap FrameTimer InformationPending I/O RequestsyList of Pending APCs Timer Block and Wait Blocks List of Objects Being Waiting OnTEBLinux Threads u eadsLin refers to them as rather thanLinux refers to them as tasks rather than threads .Thread creation is done through clone() system call system call.Clone() allows a child task to share the dd f th t t k ()address space of the parent task (process)Java Threads Ja a eadsJa a threads ma be created bJava threads may be created by: Extending Thread classImplementing the Runnable interfaceImplementing the Runnable interface Java threads are managed by the JVMJava threads are managed by the JVM.Java Thread States Ja a ead StatesChapter 4: Threads C apte eadsO er ieOverview Multithreading Models g Threading Issues Wi d XP Th d Windows XP Threads Linux Threads Java Threads Windows Thread APIs PthreadsWindows Thread APIs do s ead sCreateThreadGetCurrentThreadId -returns global ID G tC tTh d t h dl GetCurrentThread -returns handle SuspendThread/ResumeThread ExitThread TerminateThread GetExitCodeThread G tTh dTiGetThreadTimesWindows API Thread Creation do s ead C eat oHANDLE CreateThread (LPSECURITY_ATTRIBUTES lpsa,DWORD cbStack DWORD cbStack,LPTHREAD_START_ROUTINE lpStartAddr,LPVOID lpvThreadParm,DWORD fdwCreate,LPDWORD lpIDThread)cbStack == 0: thread‘s stack size defaults to primary thread‘s size lpstartAddr points to function declared asDWORD WINAPI ThreadFunc(LPVOID)p yDWORD WINAPI ThreadFunc(LPVOID) lpvThreadParm is 32-bit argumentLPIDThread points to DWORD that receives thread ID non-NULL pointer !pWindows API Thread Termination do s ead e at oVOID ExitThread(DWORD devExitCode )VOID ExitThread( DWORD devExitCode )When the last thread in a process terminates, theprocess itself terminates process itself terminatesBOOL GetExitCodeThread (HANDLE hThread LPDWORD lpdwExitCode)HANDLE hThread, LPDWORD lpdwExitCode)R t it d STILL ACTIVEReturns exit code or STILL_ACTIVESuspending and ResumingTh dThreadsEach thread has suspend count Each thread has suspend count Can only execute if suspend count == 0 Thread can be created in suspended stateDWORD ResumeThread (HANDLE hThread)DWORD SuspendThread(HANDLE hThread)DWORD SuspendThread(HANDLE hThread)Both functions return suspend count or0xFFFFFFFF on failureExample: Thread Creation a p e ead C eat oWhat’s Wrong?。

操作系统第4章ppt课件

操作系统第4章ppt课件

THANKS
感谢观看
P/V操作
对信号量进行加减操作,实现进程同 步与互斥。
经典同步问题及其解决方法
1 2
生产者-消费者问题
通过两个信号量分别控制生产者和消费者进程, 确保生产者和消费者之间的同步与互斥。
哲学家进餐问题
通过引入资源分级法或信号量集机制,避免死锁 的发生,确保哲学家进餐过程中的同步与互斥。
3
读者-写者问题

多线程模型比较分析
01
多对一模型Leabharlann 将多个用户级线程映射到一个内核级线程上。该模型下,线程管理在用
户空间完成,线程的调度采用非抢占式调度,由线程库负责。
02
一对一模型
将每个用户级线程都映射到一个内核级线程上。该模型下,线程的创建
、撤销和同步等都在内核中实现,线程的调度由内核完成。
03
多对多模型
将多个用户级线程映射到少数但不止一个内核级线程上。该模型结合了
前两种模型的优点,允许多个用户级线程映射到不同的内核级线程上运
行。
线程同步与互斥机制
互斥锁
采用互斥对象机制,只有拥有互斥对象的线程才有访问公共 资源的权限。因为互斥对象只有一个,所以能保证公共资源 不会同时被多个线程同时访问。
信号量
信号量是一个整型变量,可以对其执行down和up操作,也 就是常见的P和V操作。信号量初始化为一个正数,表示并发 执行的线程数量。
死锁避免:银行家算法是一种典型的 死锁避免算法。该算法通过检查请求 资源的进程对资源的最大需求量是否 超过系统可用资源量来判断是否分配 资源给该进程。如果分配后系统剩余 资源量仍然能够满足其他进程的最大 需求量,则分配资源,否则不分配资 源。
死锁检测:通过定期运行死锁检测算 法来检测系统中是否存在死锁。常见 的死锁检测算法有资源分配图算法和 银行家算法等。如果检测到死锁发生 ,则需要采取相应措施来解除死锁, 例如通过撤销部分进程或抢占部分资 源来打破死锁状态。

操作系统原理课件(2024)

操作系统原理课件(2024)

访问权限保护
通过设置内存页的访问权 限,防止程序对未授权内 存的访问。
内存加密保护
通过对内存数据进行加密 ,防止敏感数据被窃取或 篡改。
14
虚拟内存技术
虚拟内存概念
将内存和外存结合起来,为用户 提供一种比实际物理内存大得多
的逻辑内存空间。
2024/1/29
请求分页系统
将用户程序的逻辑地址空间划分为 若干个固定大小的页面,当页面被 访问时,才将其调入内存。
动态分配
在程序运行时动态申请 和释放内存,如可变分 区分配、页式分配和段 式分配。
回收策略
包括立即回收和延迟回 收两种策略,以及常用 的内存回收算法如引用 计数法、标记-清除法和 复制法等。
13
内存保护技术
界限寄存器保护
通过设置界限寄存器来限 制程序对内存的访问范围 ,防止越界访问。
2024/1/29
一定的算法或策略来分配设备的使用权。
设备分配策略的分类
根据设备分配方式和目标的不同,设备分配策略可分为独占分配、共享分配和虚拟分配 等。
2024/1/29
设备分配策略的实现
设备分配策略的实现需要考虑设备的状态、用户的需求和系统的性能等因素,通常包括 设备请求的处理、设备分配算法的选择和设备使用情况的记录等操作。
2024/1/29
18
文件目录结构
2024/1/29
目录的概念
目录是文件系统中存储文件信息的数据库,它记录了文件的名字、位置、大小、 创建时间等信息。
目录结构
常见的目录结构有单级目录结构、二级目录结构和多级目录结构。多级目录结构 又称为树形目录结构,它以根目录为起点,各级子目录为分支,构成一棵倒置的 树。
攻击。

操作系统原理CCH04-Thread

操作系统原理CCH04-Thread
主存和其它
– 又是一个可独立调度和分派的基本单位。
这二个基本属性使进程成为并发执行的基本单位 • 在一些早期的OS中,比如大多数UNIX系统、Linux等, 进程同时具有这二个属性。
4.1 Overview
• 有些OS中,象WindowsNT、Solaris、OS/2、Mac OS等,这二个属性由OS独立处理。
• Responsiveness 响应度高:一个多线程的应用在执行中,即使其中的某个线程阻塞, 其他的线程还可继续执行,从而提高响应速度
• Resource Sharing
资源共享:同一进程的多个线程共享该进程的内存等资源 • Economy
经济性:创建和切换线程的开销要低于进程。比如,Solaris中进程
– program counter 程序计数器 – register set 寄存器集
– stack spaห้องสมุดไป่ตู้e 栈空间
• A thread shares with its peer threads its: 一个线程与它的对等线程共享:
– code section 代码段
– data section 数据段 – operating-system resources 操作系统资源
Win32 Threads
• Win32 API is the primary API for Microsoft OS (Win95,98,NT,2000,XP ) • A kernel-level library on windows
level
• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization

操作系统课件第4章2024新版


06
总结回顾与拓展延伸
关键知识点总结回顾
操作系统的基本概念、功能 和分类
进程与线程的概念、状态和 调度算法
02
01
内存管理的基本原理、分页
和分段技术
03
文件系统的基本概念、文件 结构和访问方法
04
05
设备管理的基本原理、I/O控 制方式和设备驱动程序
案例分析加深理解
案例分析1
Windows操作系统中的进程管 理
05
进程调度算法及评价
调度基本概念和类型
调度定义
操作系统中的调度是指按照某种策略和方法,将处理机分 配给进程或线程,以实现多道程序并发执行的过程。
调度类型
根据调度的发生时机和目的,可分为长期调度(作业调度 )、中期调度(内存调度)和短期调度(进程/线程调度 )。
调度器
负责实现调度的程序称为调度器,它是操作系统内核的一 部分。
资源分配图算法
利用有向图描述系统资源和进程 状态,通过检测有向图是否存在 环路来判断系统是否处于死锁状 态。
死锁检测与恢复方法
死锁检测
通过定时运行死锁检测程序来判断系统是否发生死锁。具体方法可采用资源分配图算法或等待图算法 。
死锁恢复
当检测到死锁时,需要采取措施来解除死锁。常见的解除死锁的方法有抢占资源和回滚操作。抢占资 源是指从一个或多个死锁进程中抢占足够数量的资源,使得其他进程可以继续执行;回滚操作是指将 一个或多个死锁进程回滚到某个状态,以解除死锁。
操作系统课件第4章
contents
目录
• 进程管理 • 进程同步与通信 • 死锁及处理策略 • 线程、多线程编程技术 • 进程调度算法及评价 • 总结回顾与拓展延伸
01

中科大操作系统原理与实现课件4_Threads1

Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
.
.
.
.
.
.
Multithreading Models IV
Two-level Model, a popular variation on many-to-many model
2. Resource Sharing
address space, memory, and other resources
3. Economy
Solaris: creating a process is about 30 times slower then creating a thread; context switching is about 5 times slower
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Examples
操作系统原理与设计
第4章 Threads 1(线程1) 陈香兰
中国科学技术大学计算机学院
December 20, 2009
.
.
.
.
.
.
提纲
Overview Multithreading Models Thread Libraries Threading Issues 小结和作业
.
.
.
.
.

[课件] 大学操作系统课件ch4-Thread


code
data
registers
files stack
thread
code
data
files
registers registers registers
stack
stack
stack
thread
single-threaded process
multithreaded process
✴ 进程具有代码/数据/堆及其它分段 ✴ 进程具有至少有一个线程
Scheduler Activations: Ef
‘m
........ J($
(
1ቤተ መጻሕፍቲ ባይዱ
..“...
Activations: Effective Kernel Support
.
63
Time
User Program
............................ .. ........................
程称为主线程,并拥有进程堆栈 ✴ 创建容易 ✴ 上下文切换快捷 ✴ 线程间具有高效的通信性能 ✴ 如果一个线程死亡,它的堆栈被回收
线程模型
用Us户er线vs程. Kernel T内hr核ea线ds程
user thread
k
kernel thread
多对一模型
user thread
k
k
k
k
kernel thread
多线程中的几个问题
系统调用对线程的影响 fork() exec()
线程撤除 Thread cancellation
信号处理
signal handling
线程池设计
线程专用数据支持

操作系统原理课件ppt


总结词
进程是程序的一次执行,具有动态性、 独立性和制约性。
VS
详细描述
进程是程序在计算机上的一次执行过程, 它具有动态性,即进程的状态可以在运行 过程中改变;同时,进程具有独立性,即 每个进程都有独立的内存空间和系统资源 ;此外,进程之间存在制约性,即进程的 执行需要遵循一定的顺序和规则。
进程的创建和终止
总结词
进程调度是操作系统根据一定的算法选择下 一个要执行的进程,进程切换是保存和恢复 进程的状态。
详细描述
进程调度是操作系统的一项重要任务,它根 据一定的算法(如先来先服务、最短作业优 先等)选择下一个要执行的进程。当一个进 程正在运行时,如果发生中断或需要切换到 另一个进程,操作系统会保存当前进程的状 态(上下文),恢复下一个要执行的进程的 状态,完成进程切换。
内存管理策略
根据不同的需求和应用场 景,选择合适的内存管理 策略,如页面置换算法等 。
04
文件系统
文件系统的概念和功能
文件系统的概念
文件系统是操作系统中用于管理文件 存储空间和文件访问控制的一种机制 。它提供了一种组织和管理文件的方 式,使得用户可以方便地创建、删除 、修改和查找文件。
文件系统的功能
05
设备管理
设备管理的概念和功能
设备管理概念
设备管理是操作系统中负责管理外部设备(如硬盘、 显示器、键盘等)的子系统。它负责设备的初始化、 分配、使用和释放等任务,确保设备能够高效、安全 地为应用程序提供服务。
设备管理功能
设备管理的主要功能包括设备的分配、设备的启动和 关闭、设备的输入和输出控制、设备的同步和异步操 作以及设备的缓冲管理等。
文件系统主要提供了文件的存储、检 索和管理功能。它还负责文件的权限 控制和安全保护,以确保文件的完整 性和机密性。
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Similar to M:M, except that it allows a user thread to be bound to a kernel thread Examples
IRIX HP-UX Tru64 UNIX Solaris 8 and earlier
.
.
.
.
.
.
Outline
Overview Multithreading Models Thread Libraries Threading Issues 小结和作业
It shares with other threads belonging to the same process
code section data section and other OS resources
open files, signals, etc
.
.
.
.
.
.
Thread concept II
Many-to-One One-to-One Many-to-Many
Many-to-One
Many user-level threads mapped to single kernel thread Examples:
Solaris Green Threads GNU Portable Threads
Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
.
.
.
.
.
.
Multithreading Models IV
Two-level Model, a popular variation on many-to-many model
Motivation, think about
a web server, an RPC server and Java’s RMI systems
. . . . . .
Motivation II
PARTICULAR, many OS systems are now multithreaded.
Solaris, Linux(伪)
Windows XP/2000 Solaris Linux (伪) Tru64 UNIX (formerly Digital UNIX) Mac OS X
.
.
.
.
.
.
Multithreading Models I
The relationship between user threads and kernel threads
library user-level kernel-level code & data entirely in user space kernel space API user space user space iion call system call
Kernel may be multithreaded or not.
Three primary thread libraries:
POSIX Pthreads Win32 threads Java threads
.
.
.
.
.
.
Two Methods II
Kernel Threads
Supported by the Kernel, usually may be slower then user thread Examples
one for displaying images or text; another for retrieving data from network
Example 2: A word processor
one for displaying graphics; another for responding to keystrokes from the user; and a third for performing spelling & grammer checking in the background
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Examples
2. Resource Sharing
address space, memory, and other resources
3. Economy
Solaris: creating a process is about 30 times slower then creating a thread; context switching is about 5 times slower
.
.
.
.
.
.
Two Methods I
Two methods to support threads
User threads Kernel threads
User threads
Thread management done by user-level threads library without kernel support
.
.
.
.
.
.
Thread Libraries .
A thread library provides the programer an API for creating and managing threads. Two primary ways
1. to provide a library entirely in user space with no kernel support 2. to implement a kernel-level library supported directly by the OS
4. Utilization of MP Architectures
parallelism and concurrency ↑
. . . . . .
Outline
Overview Multithreading Models Thread Libraries Threading Issues 小结和作业
.
.
.
.
.
.
Multithreaded C program using the Pthreads API I
#include <pthread.h> #include <stdio.h> int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* the thread */ int main(int argc, char *argv[]) { pthread t tid; /* the thread identifier */ pthread attr t attr; /* set of attributes for the thread */ if (argc != 2) { fprintf(stderr,”usage: a.out <integer value>\n”); return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,”Argument %d must be non-negative\n”,atoi(argv[1])); return -1; }
操作系统原理与设计
第4章 Threads 1(线程1) 陈香兰
中国科学技术大学计算机学院
December 20, 2009
.
.
.
.
.
.
提纲
Overview Multithreading Models Thread Libraries Threading Issues 小结和作业
.
.
.
.
.
.
Outline
Overview Multithreading Models Thread Libraries Threading Issues 小结和作业
.
.
.
.
.
.
Thread concept I
A thread is a basic unit of CPU utilization
a thread ID a program counter a register set and a stack
Three main thead libraries
POSIX Pthreads Win32 threads Java threads
.
.
.
.
.
.
.
Pthreads
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX OSes (Solaris, Linux, Mac OS X)
Single threaded VS. Multithreaded processes
.
.
.
.
.
.
Motivation I
相关文档
最新文档