Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models Many-to-One ...

20
Multithreaded Programing

Transcript of Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models Many-to-One ...

Page 1: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Multithreaded Programing

Page 2: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Outline Overview of threads Threads Multithreaded Models

Many-to-One One-to-One Many-to-Many

Thread Libraries Pthread Win32 Java

Threading Issues The fork() & exec() system call Cancellation Signal Handling Thread Pools Thread-Specific Data Scheduler Activations

Page 3: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threads --an overview

Process can contain multiple threads of controlAPIs for Pthreads, Win32 and Java thread librariesSupport of threads in WinXP and Linux

Thread: A basic unit of CPU utilization. ThreadID, program counter, register set and a stack

It shares its code and data sections with the other thread of the same process.

A process can perform more than one task at a time if a process has multiple threads.

Page 4: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Why Threads (contd..)

A web browser might have one thread to display images or text while another retrieves data from the network.

Word processor and Web serverOperating system kernels are multithreaded

A set of threads in Solaris kernel manage interrupt handling

A kernel thread in Linux manages amount of free memory

Page 5: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threads (contd)

Page 6: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threads (Benefits)Responsiveness

Program can continue even if part of it is blocked or is performing lengthy operation.

Web browser can still allow user interaction in one thread during an image load thread

Resource sharingSeveral different threads of activity within the same

address space --share the code and data sectionEconomy

Costly Memory and resource allocation for processComparatively time consuming to create and manage

processes than threadsSolaris: creating a process 30 times slowerContext switching 5 times slower

Utilization of multiprocessor architecture

Page 7: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Multithreading Models

User Threads: managed without kernel supportKernel threads: managed directly by OSRelationship b/w user and kernel threads

Many-to-one modelOne-to-One modelMany-to-Many model

Page 8: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Multithreading Models Many-to-One

Thread management by thread library in user space

Entire process will block if a thread makes a blocking system call

One thread can access the kernel at a time

Multiple threads cannot run in parallel on multiprocessors.

Developer can create as many threads as he/she wishes

Examples Solaris –Green threads (uses this model) GNU Portable threads

Page 9: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Multithreading Models One-to-One

Maps each user thread to a kernel thread -- More concurrency Multiple threads can run in parallel on multiprocessors Drawbacks? Crating a user thread requires creating a kernel thread Overhead of creating kernel thread –Performance Implementation of this model may limit the number of supported threads Examples:

Linux, Windows

Page 10: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Multithreading Models Many-to-Many

This model multiplexes many user-level threads to a smaller or equal number of kernel threads.

Developer can create as many user threads as necessary –corresponding kernel thread can run in parallel on a multiprocessor

Examples: Solaris 9

Page 11: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Outline Overview of threads Threads Multithreaded Models

Many-to-One One-to-One Many-to-Many

Thread Libraries Pthread Win32 Java

Threading Issues The fork() & exec() system call Cancellation Signal Handling Thread Pools Thread-Specific Data Scheduler Activations

Page 12: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Thread Librariesprovides an API for creating & managing threadsTwo ways of implementing thread libraries

Provide a library entirely in user space with no kernel supportAll code & data structures exist in user spaceInvoking a function in the library results in a local function call in

user space –not system call

Implement a kernel-level library supported directly by the OSCode & data structures for library exist in kernel space.Invoking a function in the API for library results in a system call to

the kernel

Three main thread libraries POSIX Pthread, Win32 and Java

Page 13: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Thread Libraries (contd..)

Pthread LibraryThread extension of the POSIX standard –provided

as either a user or kernel level library

Win 32 LibraryKernel level library for windows

Java thread APIthread creation and management in Java program

Page 14: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Pthreads POSIX standard (IEEE1003.1c) –API for thread

creation and synchronization Just a specification of thread behavior –not an

implementationOS designers may implement it as they wish.Solaris, Linux, MacOSX, Tr64 UNIXA multithreaded program for summation:

sum = ni=0i

Page 15: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Page 16: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Win32 Threads Similar technique –windows.hThreads creation by CreateThread()Pthread_join() = WaitForSingleObject()

Java ThreadsFundamental model of program execution in a java prog.Rich set of features –creation & management of threadsEvery Java program has at least one thread+Create a thread –start()Pthread_join() = join()

Page 17: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threading Issues If one thread in a program calls fork(), does the new

process duplicate al threads, or the new process single-threaded? Two versions

Thread Cancellation: searching database, web browserTarget Thread –thread to be cancelled: Two scenarios

Asynchronous Cancellation: One thread immediately terminates the target thread

Deferred Cancellation: Target thread periodically checks whether it should terminate –termination in orderly fashion.

If resources have been allocated to cancelled thread?A thread is cancelled while in the midst of updating data?Cancellation points

Page 18: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threading Issues (contd)Consider multithreading in web server

Problems?Time required to create a thread prior to servicing a requestThread will be discarded once it has completed its workNo bound in the number of threads concurrently active in the system

Solution ? Thread Pool: Threads sit and wait for work

Benefits?Servicing a request is comparatively fasterThread pool limit the number of threads that exist at any pointThread pool architectures can dynamically adjust the # of threads in

the pool according to usage patternsSmaller pool --larger benefit –less memory consumption

Page 19: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Threading Issues (contd)

Thread Specific Data: Threads share the data of the process Thread might need its own copy of certain data TSD support in Win32, Pthreads & Java

K

LWP

Page 20: Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Outline Overview of threads Threads Multithreaded Models

Many-to-One One-to-One Many-to-Many

Thread Libraries Pthread Win32 Java

Threading Issues The fork() & exec() system call Cancellation Signal Handling Thread Pools Thread-Specific Data Scheduler Activations

Operating-System Examples