中科大操作系统原理与实现课件4_Threads1.pdf
《中科大操作系统原理与实现课件4_Threads1.pdf》由会员分享,可在线阅读,更多相关《中科大操作系统原理与实现课件4_Threads1.pdf(38页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、.操作系统原理与设计第4章 Threads 1(线程1)陈香兰中国科学技术大学计算机学院December 20,2009.提纲OverviewMultithreading ModelsThread LibrariesThreading Issues小结和作业.OutlineOverviewMultithreading ModelsThread LibrariesThreading Issues小结和作业.Thread concept IIA thread is a basic unit of CPU utilizationIa thread IDIa program counterIa reg
2、ister setIand a stackIIt shares with other threads belonging to the same processIcode sectionIdata sectionIand other OS resourcesIopen files,signals,etc.Thread concept IIISingle threaded VS.Multithreaded processes.Motivation IIOn modern desktop PC,many APPs are multithreaded.Ia seperate process+seve
3、ral threadsIExample 1:A web browserIone for displaying images or text;Ianother for retrieving data from networkIExample 2:A word processorIone for displaying graphics;Ianother for responding to keystrokes from the user;Iand a third for performing spelling&grammer checking in thebackgroundIMotivation
4、,think aboutIa web server,Ian RPC serverIand Javas RMI systems.Motivation IIIPARTICULAR,many OS systems are nowmultithreaded.ISolaris,Linux(伪).Benefits1.Responsiveness(响应度高)IExample:an interactive application such as web browser,whileone thread loading an image,another thread allowing userinteractio
5、n2.Resource SharingIaddress space,memory,and other resources3.EconomyISolaris:creating a process is about 30 times slower then creating athread;context switching is about 5 times slower4.Utilization of MP ArchitecturesIparallelism and concurrency.OutlineOverviewMultithreading ModelsThread LibrariesT
6、hreading Issues小结和作业.Two Methods IITwo methods to support threadsIUser threadsIKernel threadsIUser threadsIThread management done by user-level threads librarywithout kernel supportIKernel may be multithreaded or not.IThree primary thread libraries:IPOSIX PthreadsIWin32 threadsIJava threads.Two Meth
7、ods IIIKernel ThreadsISupported by the Kernel,usually may be slower then userthreadIExamplesIWindows XP/2000ISolarisILinux(伪)ITru64 UNIX(formerly Digital UNIX)IMac OS X.Multithreading Models IIThe relationship between user threads and kernel threadsIMany-to-OneIOne-to-OneIMany-to-ManyIMany-to-OneIMa
8、ny user-level threadsmapped to single kernelthreadIExamples:ISolaris Green ThreadsIGNU Portable Threads.Multithreading Models IIIOne-to-OneIEach user-level thread maps to a kernel threadIExamplesIWindows NT/XP/2000ILinuxISolaris 9 and later.Multithreading Models IIIIMany-to-Many ModelIAllows many us
9、er level threads to bemapped to many kernel threadsIAllows the operating system to create asufficient number of kernel threadsIExamplesISolaris prior to version 9IWindows NT/2000 with the ThreadFiberpackage.Multithreading Models IVITwo-level Model,a popular variationon many-to-many modelISimilar to
10、M:M,except that it allowsa user thread to be bound to akernel threadIExamplesIIRIXIHP-UXITru64 UNIXISolaris 8 and earlier.OutlineOverviewMultithreading ModelsThread LibrariesThreading Issues小结和作业.Thread Libraries.IA thread library provides the programer an API for creatingand managing threads.ITwo p
11、rimary ways1.to provide a library entirely in user space with no kernelsupport2.to implement a kernel-level library supported directly by theOSlibrarycode&dataAPIinvoking method inside APIuser-levelentirely in user spaceuser spacea local function callkernel-levelkernel spaceuser spacesystem callIThr
12、ee main thead librariesIPOSIX PthreadsIWin32 threadsIJava threads.PthreadsIA POSIX standard(IEEE 1003.1c)API for thread creationand synchronizationIAPI specifies behavior of the thread library,implementation isup to development of the libraryICommon in UNIX OSes(Solaris,Linux,Mac OS X).Multithreaded
13、 C program using the Pthreads API I#include#include 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
14、n”);return-1;if(atoi(argv1)0)for(i=1;i=upper;i+)sum+=i;pthread exit(0);.pthread attr init.NAMEpthread attr init,pthread attr destroy-initialise and destroy threads attributeobjectSYNOPSIS#include int pthread attr init(pthread attr t*attr);int pthread attr destroy(pthread attr t*attr);DESCRIPTIONThe
15、function pthread attr init()initialises a thread attributes object attr withthe default value for all of the individual attributes used by a givenimplementation.The pthread attr destroy()function is used to destroy a thread attributesobject.RETURN VALUEUpon successful completion,both return a value
16、of 0.Otherwise,an error number is returned to indicate the error.pthread create().NAMEpthread create-thread creationSYNOPSIS#include int pthread create(pthread t*thread,const pthread attr t*attr,void*(*start routine)(void*),void*arg);DESCRIPTIONThe pthread create()function is used to create a new th
17、read,with attributesspecified by attr,within a process.Upon successful completion,pthread create()stores the ID of the created thread in the location referencedby thread.The thread is created executing start routine with arg as its sole argument.If pthread create()fails,no new thread is created and
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中科大 操作系统 原理 实现 课件 _Threads1
限制150内