Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed...

31
Chapter 4: Threads

Transcript of Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed...

Page 1: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Chapter 4: Threads

Page 2: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Chapter 4: Threads

• Overview

• Multithreading Models

• Thread Libraries

• Threading Issues

• Operating System Examples

• Windows XP Threads

• Linux Threads

•Objectives• To introduce the notion of a thread• A fundamental unit of CPU utilization that forms the basis of

multithreaded computer systems• To discuss the APIs for the Pthreads, Win32, and Java thread libraries• To examine issues related to multithreaded programming

Page 3: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Thread• A thread is a basic unit of CPU utilization;

• it comprises a thread ID, a program counter, a register set, and a stack.

• It shares with other threads belonging to the same process its code section, data section, and other operating-system resources, such as open files.

• Heavy weight process has a single thread of control.

• A single-threaded process and a multithreaded process.

• Multiple threads of control, it can perform more than one task at a time.

Page 4: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threads Use• Many software packages run on modern desktop PCs are multithreaded and is (single

process with several threads of control).• A word processor:

• A thread for displaying graphics, A thread for responding to keystrokes from theuser and A thread for spelling and grammar checking.

• Web browser:• Thread display images/text and Thread gets data from the network.

• A Web server Problem:• Accepts client requests for Web pages, images, sound,..• If the server is busy (have several clients concurrently accessing it), and ran as a

traditional single-threaded process, it will serve only one client at a time, and aclient might have to wait long time.

• Single thread Process Solution:• The server accepts requests and creates a separate process for each request (

process creation is time consuming and resource lost ).• If the new process will perform the same tasks as the existing process (overhead)

• Multiple threads Process:• The server will create a separate thread that listens for client requests. When a

request is made, the server will create a new thread to service the request andresume listening for additional requests.

Page 5: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threads Use• Threads used in remote procedure call (RPC)

• Allow inter-process communication, When a server receives a message, it services the message using a separate thread (allows server to handle several concurrent requests).

• Finally, most operating system kernels are now multithreaded; • Several threads operate in the kernel, Each thread performs a specific task,

such managing devices or interrupt handling.

Page 6: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Benefits1. Responsiveness:

• Allow a program to continue running even if part of it is blocked or isperforming a lengthy operation(Web browser allow user interactionin one thread while an image is being loaded in another).

2. Resource sharing:• Threads share memory and resources of the parent process by default.

3. Economy:

• Create and context-switch threads is less time consuming thancreate and manage processes.

4. Scalability:

• Threads may be running in parallel on different processors. A single-threaded process can only run on one processor, regardless howmany are available. Multithreading on a multi-CPU machineincreases parallelism.

Page 7: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multicore Programming• Place multiple computing cores on a single chip. Each of these cores appears

as a separate processor to the operating system

• Multithreaded programming provides a mechanism for more efficient use ofmultiple cores and improved concurrency.

• Designers of OS must write scheduling algorithms that use multipleprocessing cores to allow the parallel execution.

• Multicore systems challenges on programmer::• Dividing activities: divide the program into separate tasks that can be run in

parallel.

• Balance: The tasks must perform equal work of equal value

• Data splitting: The data accessed and manipulated by the tasks must be divided torun on separate cores.

• Data dependency: The data accessed by the tasks must be examined fordependencies between two or more tasks.

• Testing and debugging: On multiple cores, there are many different executionpaths. Testing and debugging such concurrent programs is more difficult.

Page 8: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Concurrent Execution on a Single-core System

Parallel Execution on a Multicore System

Page 9: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multithreading Models•User threads

• Provided either at the user level• Supported above the kernel • Managed without kernel support

• kernel threads:• Provided by the kernel Level• Supported and managed directly by the operating system.

• Most operating systems—including Windows XP , Windows Vista, Linux, Mac OS X, Solaris, and Tru64 UNIX support kernel threads.

• A relationship must exist between user threads and kernel threads.

Page 10: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multithreading Models1. Many-to-One Model

• Many user-level threads mapped to single kernel thread

• Examples:• Solaris Green Threads

• GNU Portable Threads

Page 11: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multithreading Models2. One-to-One Model

• Each user-level thread maps to kernel thread

• Examples• Windows NT/XP/2000

• Linux

• Solaris 9 and later

Page 12: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multithreading Models3. 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

• Solaris prior to version 9

• Windows NT/2000 with the Thread Fiber package

Page 13: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Multithreading Models4. Two Level Model• Similar to M:M, except that it allows a user thread to be bound to kernel thread

• Examples• IRIX• HP-UX• Tru64 UNIX• Solaris 8 and earlier

Page 14: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Thread Libraries• A thread library provides the programmer with an API for creating and managing

threads.

• There are two primary ways of implementing a thread library:

• User Level Thread Library:• Provide a library entirely in user space with no kernel support.

• All code and data structures for the library exist in user space.

• Invoking a function in the library as a local function call in user space and not a system call.

• Kernel Level Thread Library:

• Implement a kernel-level library supported directly by the OS.

• Code and data structures for the library exist in kernel space.

• Invoking a function in the API for the library typically results in a system call to the kernel.

• Three main thread libraries are in use today: 1. POSIX Pthreads: may be provided as either a user- or kernel-level library.

2. Win32: is a kernel-level library available on Windows systems.

3. Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine (JVM) is running on top of ofthe OS, Java threads depends on the OS system that is running on it.

Page 15: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Pthreads• Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for thread

creation and synchronization.

• May be provided either as user-level or kernel-level

• 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 operating systems (Solaris, Linux, Mac OS X)

Page 16: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Pthreads Example

Page 17: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Win32 API Multithreaded C Program (Example)

Page 18: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Java thread• Threads are the fundamental model of program execution in a Java program.• Java language provide a rich set of features for create and manage threads. • All Java programs have at least a single thread of control that begins execution in the program’s

main() method.• Creating Java Thread:

• Create a new class from the Thread class and to override its run() method. • Define a class that implements the Runnable interface.

• JVM and the underlying Host• JVM is implemented on top of a host OS ( hide the details of the underlying OS )• allows Java programs to operate on any platform that supports a JVM. • JVM does not indicate how Java threads are to be mapped to the underlying OS• For example, the Windows XP OS uses the one-to-one model; therefore, each Java thread for a

JVM running on such a system maps to a kernel thread. • On operating systems that use the many-to-many model (such as Tru64 UNIX), a Java thread is

mapped according to the many-to-many model. • Solaris initially implemented the JVM using the many-to-one model (the green threads library

mentioned earlier).

Page 19: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Java Multithreaded Program

Page 20: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Java thread States• A Java thread may be in one of six possible states in the JVM:

1. New. A thread object for is created with the new command but has not yet started.

2. Runnable. Calling the start() method allocates memory for the new thread in the JVM and calls the run() method for the thread object. When a thread’s run() method is invoked, the thread moves from the new to the runnable state.

3. Blocked. A thread is in this state as it waits to acquire a lock—a tool used for thread synchronization.

4. Waiting. A thread invoking the join() method enters this state as it waits for the thread it is joining on to terminate.

5. Timed waiting. is similar to waiting, except a thread specifies the maximum amount of time it will wait. For example, the join() method has an optional parameter that the waiting thread can use to specify how long it will wait until the other thread terminates.

6. Terminated. A thread moves to the this state when its run() method terminates.

Page 21: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Java thread States

Page 22: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

A Multithreaded Solution to the Producer–Consumer Problem

Page 23: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

A Multithreaded Solution to the Producer–Consumer Problem

Page 24: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threading Issues with multi-thread programs1. When creating new Process [Semantics of fork() and exec() system calls]

• If one thread in a program calls fork(), • does the new process duplicate all threads, or is the new process single-threaded? • Some OS two versions of fork(), one that duplicates all threads and another that duplicates

only the thread that invoked the fork() system call.

2. Thread Cancellation :Terminating a thread before it has finished• Two general approaches:

• Asynchronous cancellation terminates the target thread immediately.• Deferred cancellation allows the target thread to periodically check if it should be

cancelled.

3. Signal Handling : Signals are used to notify a process that a particular event has occurred.

• A signal handler is used to process signals1. Signal is generated by particular event2. Signal is delivered to a process3. Signal is handled

• Options:1. Deliver the signal to the thread to which the signal applies2. Deliver the signal to every thread in the process3. Deliver the signal to certain threads in the process4. Assign a specific thread to receive all signals for the process

Page 25: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threading Issues with multi-thread programs1. When creating new Process [Semantics of fork() and exec() system calls]

• If one thread in a program calls fork(), • does the new process duplicate all threads, or is the new process single-threaded? • Some OS two versions of fork(), one that duplicates all threads and another that duplicates

only the thread that invoked the fork() system call.

2. Thread Cancellation :Terminating a thread before it has finished• Two general approaches:

• Asynchronous cancellation terminates the target thread immediately.• Deferred cancellation allows the target thread to periodically check if it should be

cancelled.

3. Signal Handling : Signals are used in UNIX systems to notify a process that a particular event has occurred.

• A signal handler is used to process signals1. Signal is generated by particular event2. Signal is delivered to a process3. Signal is handled

• Options:• Deliver the signal to the thread to which the signal applies• Deliver the signal to every thread in the process• Deliver the signal to certain threads in the process• Assign a specific thread to receive all signals for the process

Page 26: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threading Issues with multi-thread programs

4. Thread Pools• in a Web server, whenever the server receives a request, it creates a separate thread to

service the request. • The amount of time required to create the thread prior to servicing the request.

• The number of threads concurrently active in the system. Unlimited threads could exhaust system resources, such as CPU time or memory. One solution to this issue is to use a thread pool.

• How does Thread Pools works?1. Create a number of threads at process startup and place them into a pool, an wait for work.

2. When a server receives a request, it assign the request for a thread from this pool to service (if one is available).

3. Once the thread completes its service, it returns to the pool and awaits more work.

4. If the pool contains no available thread, the server waits until one becomes free.

Page 27: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Threading Issues with multi-thread programs

5. Thread Specific Data• Threads belonging to the same process share the data of the process.

• Some times, each thread might need its own copy data( thread-specific data).

• For example, in a transaction-processing system, we might service each transaction in aseparate thread.

Page 28: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Windows XP Threads• Implements the one-to-one mapping, kernel-level, Consists of:

• Thread ID identifying the thread.• Thread Context consists of :

• Register set representing the status of the processor• User stack, employed when the thread is running in user mode.• kernel stack, employed when the thread is running in kernel mode.• Private storage area used by various run-time libraries and dynamic link libraries (DLLs)

• The primary data structures of a thread include:• ETHREAD—Executive Thread Block, include:

• A pointer to the process to which the thread belongs• The address of the routine in which the thread starts control. • a pointer to the corresponding KTHREAD.

• KTHREAD—Kernel Thread Block – includes:• scheduling and synchronization information for the thread.• The kernel stack (used when the thread is running in kernel mode) .• a pointer to the TEB

• TEB—Thread Environment Block- The TEB is a user-space data structure that is accessed when the thread is running in user mode. The TEB contains:• the thread identifier,• a user-mode stack, • and an array for thread specific dat.

Page 29: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Windows XP Threads Data Structures

Page 30: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

Linux Threads fork() and clone() system calls

Doesn’t distinguish between process and thread

Uses term task rather than thread

clone() takes options to determine sharing on process create

struct task_struct points to process data structures (shared or unique)

Page 31: Chapter 4: Threads - WordPress.com · Java thread API: allows threads to be created and managed directly in Java programs. However, because in most instances the Java virtual machine

End of Chapter 4