3 What is a Thread is a flow of control within a process. single-threaded process, multi-threaded process.is a basic unit of CPU utilization, which comprisea thread ID, program counter, register set, stack.shares with other threads belonging to the same process its code section, data section, and other OS resources (open files and signal)If a process has multiple threads of control, it can perform more than one task at a time.
4 Single and Multithreaded Processes Word processor: editing + spell checking
5 Multi-threaded programs Many software packages that run on modern OS are multi-threaded.A web browser might haveOne thread display images or textAnother thread retrieves data from the networkA word processor may haveA thread for displaying graphicsAnother thread for responding to keystrokes form the userA third thread for performing spelling and grammar checking
6 Multi-threaded programs Web Server, for exampleSingle-threaded web server: a client might have to wait for its request to be serviced.Multi-processes web server: used before threads become popular, much overhead in creating a new process.Multi-threaded web server: less overhead in thread creation, concurrent service to multiple clientMany OS kernels are now multi-threadedSeveral threads operates in the kernel.Each thread performs a specific task, such as managing devices or interrupt handling.
7 Benefits of multi-threaded programming ResponsivenessMultithreading an interactive application may allow a program to continue running even if part of it is blocked or doing a lengthy operation.Resource SharingThreads share the memory and the resources of the process to which they belong.EconomyBecause threads in a process shares the resources, it is more economical to create and context-switch threads.Creating process is about thirty times slower than creating thread in Solaris.Utilization of Multi-Processor ArchitecturesThreads may be running in parallel on different processors.
8 Two types of threads User Thread Kernel Thread User-level thread are threads that are visible to the programmer and are unknown to the kernel.OS kernel supports and manages kernel-level threadsIn general, user-level threads are faster to create and manage than are kernel threads, because no intervention from the kernel is required.
9 User Threads Thread management done by user-level threads library User thread are supported above the kernel and are managed without kernel support.Three primary thread libraries:POSIX PthreadsWin32 threadsJava threads
10 Kernel Threads Supported by the Kernel Examples The threads are supported and managed directly by the operating system.ExamplesWindows XP/2000SolarisLinuxTru64 UNIXMac OS X
11 Multithreading Models A Relationship between user threads and kernel threads.Many-to-OneOne-to-OneMany-to-Many
12 Many-to-One Many user-level threads mapped to single kernel thread Thread management is done by the thread library in user spaceIt is efficient. can create as many user threads as you wish.The entire process will block when a thread makes a blocking system call.Even on multiprocessors, threads are unable to run in parallelExamples:Solaris Green ThreadsGNU Portable Threads
14 One-to-One Each user-level thread maps to a kernel thread Examples provides more concurrency than the many-to-one modelallows another thread to run when a thread makes a blocking system callCreating a user thread requires creating the corresponding kernel thread. – overhead.The number of threads a process can create is smaller than many-to-one model. – be careful not to create too many thread.ExamplesWindows NT/XP/2000LinuxSolaris 9 and later
16 Many-to-Many ModelAllows many user level threads to be mapped to smaller or equal kernel threadsallows the OS to create a sufficient number of kernel threadsThe number of kernel threads may be specific to either a application or machineExamplesSolaris prior to version 9Windows NT/2000 with the ThreadFiber package
18 Two-level Model One popular variation on many-to-many model Examples Similar to Many-to-Many model,Many user-level threads are multiplexed to a smaller or equal number of kernel threadsBut it allows a user thread to be bound to a kernel threadExamplesIRIXHP-UXTru64 UNIXSolaris 8 and earlier
20 What model is this mapping User ThreadsKernel Threads
21 Thread LibrariesA thread library provides the programmer an API for creating and managing threadsTwo primary way to implementto provide a library entirely in user space with no kernel support.All code and data structures for the library exist in user spaceEvery function call executes in user mode, not in kernel modeto implement a kernel-level library supported by OSAll code and data structures exist in kernel spaceInvoking functions result in a system call to the kernelThree main thread librariesPOSIX Pthreads - Solaris, Linux, Mac OS X, and Tru64 UNIXWin32 Thread - WindowsJava Thread - Java
22 Light Weight Processes (LWP) Many systems implementing either many-to-many or two-level model place an intermediate data structure between user and kernel threads.This data structure is known as Light Weight Process (LWP)A user thread is attached to a LWPEach LWP is attached to a kernel thread.OS schedules the kernel threads (not processes) to run on CPUIf a kernel thread blocks, LWP blocks, and the user thread blocks.To the user thread library, LWP appears to be a virtual CPU on which the application can schedule a user thread to run.The user thread library is responsible for scheduling among user threads to the LWPs. It is similar to the CPU scheduling in kernel.In general, context switching between user threads involves taking a user thread of its LWP and replacing it with another thread.
23 Threading Issues Semantics of fork() and exec() system calls Thread cancellationSignal handlingThread poolsThread specific dataScheduler activations
24 Semantics of fork() and exec() fork() system call is used to create a new separate, duplicate process.If one thread in a program calls fork() system call,does the new process duplicate all threads? ,or is the new process single-threaded?Some UNIX systems provides two versions of fork()
25 Thread CancellationTerminating a thread before it has finished by other threadsEx, multiple threads search DB, one thread returns result. The remaining thread might be canceled.Two general approaches to cancel the target threadAsynchronous cancellation terminates the target thread immediatelyDeferred cancellation allows the target thread to periodically check if it should be cancelled
26 Thread Cancellation Asynchronous cancellation Deferred cancellation The difficulty with cancellation occurs in situationwhere resources have been allocated to a canceled thread, orwhere a thread is canceled while in the midst of updating data it is sharing with other threadsDeferred cancellationOne thread indicates that target thread is to be canceled.Cancellation occurs only after the target thread has checked a flag to determine if it should be canceled or not
27 Signal HandlingSignals are used in UNIX systems to notify a process that a particular event has occurredTwo types of signalsSynchronous signalsillegal memory access, division by 0Asynchronous signalsSpecific keystrokes (Ctrl-C), timer expireWhat happen when a signal generated?Signal is generated by particular eventSignal is delivered from kernel to a processSignal is handled
28 Signal HandlingEvery signal may be handled by one of two possible handlers.A default signal handlerA user-defined signal handlerEvery signal has a default signal handler that is run by the kernel when handling that signal.The default action cat be overridden by a user-defined signal handlerOptions when a signal occurs on a multi-threaded processDeliver the signal to the thread to which the signal appliesDeliver the signal to every thread in the processDeliver the signal to certain threads in the processAssign a specific thread to receive all signals for the process
30 Thread Pools Pool of threads where they await work Advantages: A process creates a number of threads at start up and place them into a poolWhen receiving a request, server awakens a thread from the pool and passes the request to serviceOnes the thread completes its service, it returns to the pool and await more work.If the pool contains no available thread, the server waits until one becomes free.Advantages:Usually slightly faster to service a request with an existing thread than create a new threadAllows the number of threads in the application(s) to be bound to the size of the poolQ: how many threads is disable to be created at the process start up time?
31 Thread Specific DataThreads belong to a process share the data of the process.In some circumstances, each thread may need it’s own dataallows each thread to have its own copy of datais useful when you do not have control over the thread creation process (i.e., when using a thread pool)Most thread libraries (Win32, Pthread, Java) provide some form of support for thread-specific data.
32 Scheduler Activations Both Many-to-Many and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the applicationScheduler activations provide upcalls - a communication mechanism from the kernel to the thread libraryThis communication allows an application to maintain the correct number kernel threads
33 PthreadsA POSIX standard (IEEE c) 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)
34 Windows XP Threads Implements the one-to-one mapping Each thread containsA thread idRegister setSeparate user and kernel stacksPrivate data storage areaThe register set, stacks, and private storage area are known as the context of the threadsThe primary data structures of a thread include:ETHREAD (executive thread block)KTHREAD (kernel thread block)TEB (thread environment block)
35 Linux Threads Linux refers to them as tasks rather than threads Thread creation is done through clone() system callclone() allows a child task to share the address space of the parent task (process)
36 Java Threads Java threads are managed by the JVM Java threads may be created by:Extending Thread classImplementing the Runnable interfaceBecause JVM is running on top of a host OS, the Java thread API is typically implemented using a thread library available on the host system.On Windows system: using Win32 thread libraryOn Linux and Unix system: using Pthread library
37 Summary A thread is a flow of control within process. Multithreaded process contains several different flows of control within the same address space.Benefits of multi-threading includesIncreased responsiveness, Resource sharing within the processEconomy, Ability to take advantage of multiprocessor architectureUser-level thread are thread that are visible to the programmer and are unknown to the kernel.OS kernel supports and manages kernel-level threadsThree types of models relates user and kernel threadsOne-to-one, many-to-one, many-to-manyThread libraries provide the application programmer with an API for creating and managing threadsPOSIX Pthreads, Win32 threads, Java threadsMultithreaded programs introduces several issuesfork()/exec(), thread cancellation, signal handling, thread-specific data.