CSC 4320/6320 Operating Systems Lecture 4 ThreadsChapter 4: ThreadsObjectivesThreadsSingle and Multithreaded ProcessesMultithreaded Server ArchitectureBenefitsProcess VsThreadsConcurrent Execution on a Single-core SystemMulticore ProgrammingUser ThreadsThreads in User SpaceKernel ThreadsThreads in Kernel SpaceHybrid ImplementationsAdvantage-Disadvantage FactorsMultithreading ModelsMany-to-OneOne-to-OneMany-to-Many ModelTwo-level ModelThreads Vs MultiprocessesThread LibrariesPthreadsSlide 25Win32 Thread ProgramJava ThreadsThreading IssuesSemantics of fork() and exec()Thread CancellationSignal HandlingSlide 32Thread PoolsThread Specific DataScheduler ActivationsLightweight Processes (LWP)Operating System ExamplesWindows XP ThreadsSlide 39Linux ThreadsSlide 41End of Lecture 4Saurav KarmakarChapter 4: ThreadsOverviewMultithreading ModelsThread LibrariesThreading IssuesOperating System ExamplesWindows XP ThreadsLinux ThreadsObjectivesTo introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systemsTo discuss the APIs for the Pthreads, Win32, and Java thread librariesTo examine issues related to multithreaded programmingThreadsA thread is a single sequence stream within in a process. Processes are used to group resources together and threads are the entities scheduled for execution on the CPU.Sometimes called lightweight processes. A thread has or consists of thread id, a program counter (PC), a register set, and a stack space. Threads are not independent of one other like processesSingle and Multithreaded ProcessesMultithreaded Server Architecture When some similar tasks to be served by a server should it create a process or a thread?BenefitsResponsivenessResource SharingEconomyScalabilityProcess VsThreadsSimilaritiesThreads share CPU and only one thread active (running) at a time. Threads within a processes execute sequentially. Thread can create children. If one thread is blocked, another thread can run. DifferencesThreads are not independent of one another. All threads can access every address in the task . Threads are designed to assist one other. Note that processes might or might not assist one another because processes may originate from different users.Concurrent Execution on a Single-core SystemParallel Execution on a Multicore SystemMulticore ProgrammingMulticore systems putting pressure on programmers, challenges includeDividing activitiesBalanceData splittingData dependencyTesting and debuggingUser ThreadsThread management done by user-level threads libraryThread switching does not need to call operating system and to cause interrupt to the kernel. The kernel knows nothing about user-level threads and manages them as if they were single-threaded processes. Three primary thread libraries: POSIX Pthreads Win32 threads Java threadsThreads in User SpaceEach process needs a thread tableSimilar to a process table, but tracks only:○Program Counter○Stack Pointer○Registers○StateTable managed by the run-time systemKernel ThreadsSupported by the KernelThe kernel has a thread table that keeps track of all threads in the system.Operating Systems kernel provides system call to create and manage threads. ExamplesWindows XP/2000SolarisLinuxTru64 UNIXMac OS XThreads in Kernel SpacePut the concept of the thread in the kernel There is no need of a run-time systemThe kernel has a thread tableThreads make calls to the kernel for thread creation/terminationKernel holds each thread’s registers, state, etc...Hybrid ImplementationsTrys to get to advantage of both the designsUser and kernel threads are implemented where they will be more efficientDisadvantage : Complicated design by farAdvantage-Disadvantage FactorsRepresentaionManagementSpeed and EfficiencyScheduler DecesionOverheadsApplicationMultithreading ModelsDescribes the relationship between user threads and kernel threadsThree general types :Many-to-OneOne-to-OneMany-to-ManyMany-to-OneMany user-level threads mapped to single kernel threadThread management – in user spaceEntire process gets blocked if one blocks.Only one thread can use kernel at a timeExamples:Solaris Green ThreadsGNU Portable ThreadsOne-to-OneEach user-level thread maps to a kernel threadBetter in concurrencyOnly drawback is creating one of this kind requires corresponding kernel thread creationExamplesWindows NT/XP/2000LinuxSolaris 9 and laterMany-to-Many ModelAllows many user level threads to be mapped to many (smaller or equal #) kernel threads(# depends on application/machine)Allows the operating system to create a sufficient number of kernel threadsOn a blocking call kernel can schedule another threadExample :Solaris prior to version 9Windows NT/2000 with the ThreadFiber packageTwo-level ModelSimilar to M:M, except that it allows a user thread to be bound to kernel threadExamplesIRIXHP-UXTru64 UNIXSolaris 8 and earlierThreads Vs MultiprocessesAdvantages :Context SwitchingSharingDisadvantages :BlockingSecurityThread LibrariesThread library provides programmer with API for creating and managing threadsTwo primary ways of implementingLibrary entirely in user spaceKernel-level library supported by the OSPthreadsMay be provided either as user-level or kernel-levelA POSIX standard (IEEE 1003.1c) API for thread creation and synchronizationAPI specifies behavior of the thread library, implementation is up to development of the libraryCommon in UNIX operating systems (Solaris, Linux, Mac OS X)Win32 Thread ProgramJava ThreadsJava threads are managed by the JVMTypically implemented using the threads model provided by underlying OSJava threads may be created by:Extending Thread classImplementing the Runnable interfaceThreading IssuesSemantics of fork() and exec() system callsThread cancellation of target threadAsynchronous or deferredSignal handlingThread poolsThread-specific dataScheduler activationsSemantics of fork() and exec()Does fork() duplicate only the calling thread or all threads?What about exec() ?What type of fork() is better ?Thread CancellationTerminating a thread before it has
View Full Document