+ All Categories
Home > Documents > Jerry Breecher

Jerry Breecher

Date post: 03-Jan-2016
Category:
Upload: teagan-rich
View: 57 times
Download: 2 times
Share this document with a friend
Description:
OPERATING SYSTEMS Threads. Jerry Breecher. OPERATING SYSTEM Threads. What Is In This Chapter? Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. THREADS. Single and Multithreaded Processes. THREADS. Benefits. Responsiveness - PowerPoint PPT Presentation
Popular Tags:
25
Jerry Breecher 4: Threads 1 OPERATING SYSTEMS Threads
Transcript
Page 1: Jerry Breecher

Jerry Breecher

4: Threads 1

OPERATING SYSTEMS

Threads

Page 2: Jerry Breecher

What Is In This Chapter?

O OverviewO Multithreading ModelsO Threading IssuesO PthreadsO Windows XP ThreadsO Linux ThreadsO Java Threads

4: Threads 2

OPERATING SYSTEM Threads

Page 3: Jerry Breecher

Single and Multithreaded

Processes

4: Threads 3

THREADS

Page 4: Jerry Breecher

Benefits

O Responsiveness

O Resource Sharing

O Economy

O Utilization of MP Architectures

4: Threads 4

THREADS

Page 5: Jerry Breecher

User Threads

O Thread management done by user-level threads library

O Examples- POSIX Pthreads- Mach C-threads- Solaris threads

4: Threads 5

THREADS

Kernel Threads• Supported by the Kernel

• Examples

- Windows 95/98/NT/2000

- Solaris

- Tru64 UNIX

- BeOS

- Linux

Page 6: Jerry Breecher

Multithreading Models

OMany-to-One

OOne-to-One

OMany-to-Many

4: Threads 6

THREADS

How do user and kernelthreads map into each other?

Page 7: Jerry Breecher

Many-to-One

O Many user-level threads mapped to single kernel thread.

O Used on systems that do not support kernel threads.

O Examples:Solaris Green

ThreadsGNU Portable

Threads

4: Threads 7

THREADS

Page 8: Jerry Breecher

One-to-One

O Each user-level thread maps to kernel thread.

O Examples- Windows 95/98/NT/2000- Linux

4: Threads 8

THREADS

Page 9: Jerry Breecher

Threading Issues

Semantics of fork() and exec() system callsO Does fork() duplicate only the calling thread or all

threads?

Thread cancellationO Terminating a thread before it has finishedO Two general approaches:

O Asynchronous cancellation terminates the target thread immediately

O Deferred cancellation allows the target thread to periodically check if it should be cancelled

4: Threads 9

THREADS

Page 10: Jerry Breecher

Threading Issues

Signal handlingO Signals are used in UNIX systems to notify a process that a particular event

has occurredO A signal handler is used to process signals

1. Signal is generated by particular event2. Signal is delivered to a process3. Signal is handled

O Options:O Deliver the signal to the thread to which the signal appliesO Deliver the signal to every thread in the processO Deliver the signal to certain threads in the processO Assign a specific threa to receive all signals for the process

Thread poolsO Create a number of threads in a pool where they await workO Advantages:

O Usually slightly faster to service a request with an existing thread than create a new thread

O Allows the number of threads in the application(s) to be bound to the size of the pool

4: Threads 10

THREADS

Page 11: Jerry Breecher

Threading Issues

Thread specific dataO Allows each thread to have its own copy of dataO Useful when you do not have control over the thread creation

process (i.e., when using a thread pool)

Scheduler activationsO Many:Many models require communication to maintain the

appropriate number of kernel threads allocated to the application

O Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library

O This communication allows an application to maintain the correct number kernel threads

4: Threads 11

THREADS

Page 12: Jerry Breecher

Various Implementations

PThreadsO A POSIX standard (IEEE 1003.1c) API for thread creation and

synchronizationO API specifies behavior of the thread library, implementation is

up to development of the libraryO Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Windows ThreadsO Implements the one-to-one mappingO Each thread contains

O A thread idO Register setO Separate user and kernel stacksO Private data storage area

O The register set, stacks, and private storage area are known as the context of the threads4: Threads 12

THREADS

Page 13: Jerry Breecher

Various ImplementationsLinux ThreadsO Linux refers to them as tasks rather than threadsO Thread creation is done through clone() system callO clone() allows a child task to share the address space of the parent task

(process)

Java ThreadsO Java threads may be created by:

O Extending Thread classO Implementing the Runnable interface

O Java threads are managed by the JVM.

4: Threads 13

THREADS

Page 14: Jerry Breecher

4: Threads 14

WRAPUP

Threads

We’ve looked in detail at how threads work. Specifically we’ve looked at:

•Multithreading Models•Threading Issues•Pthreads•Windows XP Threads•Linux Threads•Java Threads

Page 15: Jerry Breecher

ThreadsIn computer science, a thread of execution is

the smallest unit of processing that can be scheduled by an operating system. A thread is a lightweight process. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources. In particular, the threads of a process share the latter's instructions (its code) and its context (the values that its variables reference at any given moment).

4: Threads 15

Page 16: Jerry Breecher

On a single processor, multithreading generally occurs

by time-division multiplexing (as in multitasking): the processor switches between different threads. This context switching generally happens frequently

enough that the user perceives the threads or tasks as running at the same

time. On a multiprocessor (including multi-core system), the threads or tasks will

actually run at the same time, with each processor or core running a particular

thread or task.4: Threads 16

Page 17: Jerry Breecher

Many modern operating systems directly support both time-sliced and multiprocessor threading with a process scheduler. The kernel of an operating system allows programmers to manipulate threads via the system call interface. Some implementations are called a kernel thread, whereas a lightweight process (LWP) is a specific type of kernel thread that shares the same state and information.

4: Threads 17

Page 18: Jerry Breecher

Programs can have user-space threads when threading with timers, signals, or other methods to interrupt their own execution, performing a sort of ad-hoctime-slicing.

4: Threads 18

Page 19: Jerry Breecher

4: Threads 19

Page 20: Jerry Breecher

4: Threads 20

Threads differ from traditional multitasking operating system processes in that:

O processes are typically independent, while threads exist as subsets of a process

O processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources

O processes have separate address spaces, whereas threads share their address space

O processes interact only through system-provided inter-process communication mechanisms

O context switching between threads in the same process is typically faster than context switching between processes.

O Systems like Windows NT and OS/2 are said to have "cheap" threads and "expensive" processes; in other operating systems there is not so great a difference except the cost of address space switch which implies a TLB flush.

Page 21: Jerry Breecher

Multithreading

4: Threads 21

Multithreading as a widespread programming and execution model allows multiple threads to exist within the context of a single process. These threads share the process' resources but are able to execute independently. The threaded programming model provides developers with a useful abstraction of concurrent execution. However, perhaps the most interesting application of the technology is when it is applied to a single process to enable parallel execution on a multiprocessing system.

Page 22: Jerry Breecher

This advantage of a multithreaded program allows it to operate faster on computer systems that have multiple CPUs, CPU s with multiple cores, or across a cluster of machines — because the threads of the program naturally lend themselves to truly concurrent execution. In such a case, the programmer needs to be careful to avoid race conditions, and other non-intuitive behaviors. In order for data to be correctly manipulated, threads will often need to rendezvous in time in order to process the data in the correct order. Threads may also require mutually exclusive operations (often implemented using semaphores) in order to prevent common data from being simultaneously modified, or read while in the process of being modified. Careless use of such primitives can lead to deadlocks.Another use of multitasking, applicable even for single-CPU systems, is the ability for an application to remain responsive to input. In a single-threaded program, if the main execution thread blocks on a long-running task, the entire application can appear to freeze. By moving such long-running tasks to a worker thread that runs concurrently with the main execution thread, it is possible for the application to remain responsive to user input while executing tasks in the background. On the other hand, in most cases multithreading is not the only way to keep a program responsive, with non-blocking I/O and/or Unix signals being available for gaining similar results.

4: Threads 22

Page 23: Jerry Breecher

Operating systems schedule threads in one of two ways:Preemptive multitasking is generally considered the superior approach, as it allows the operating system to determine when a context switch should occur. The disadvantage to preemptive multithreading is that the system may make a context switch at an inappropriate time, causing lock convoy, priority inversion or other negative effects which may be avoided by cooperative multithreading.Cooperative multithreading, on the other hand, relies on the threads themselves to relinquish control once they are at a stopping point. This can create problems if a thread is waiting for a resource to become available.Until late 1990s, CPU s in desktop computers did not have much support for multithreading, although threads were still used on such computers because switching between threads was generally still quicker than full process context switches. Processors in embedded systems, which have higher requirements for real-time behaviors, might support multithreading by decreasing the thread-switch time, perhaps by allocating a dedicated register file for each thread instead of saving/restoring a common register file. In the late 1990s, the idea of executing instructions from multiple threads simultaneously, known as simultaneous multithreading, had reached desktops with Intel's Pentium 4 processor, under the name hyper threading. It has been dropped from Intel Coreand Core 2 architectures, but later was re-instated in Core i5 and Core i7 architectures.

4: Threads 23

Page 24: Jerry Breecher

Model1:1 (Kernel-level threading)OThreads created by the user are in 1-1 correspondence with schedulable entities in the kernel. This is the simplest possible threading implementation.Win32 used this approach from the start. On Linux, the usual C library implements this approach (via the NPTL or older LinuxThreads). The same approach is used by Solaris, NetBSD and FreeBSD.N:1 (User-level threading)OAn N:1 model implies that all application-level threads map to a single kernel-level scheduled entity; the kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading. One of the

4: Threads 24

Page 25: Jerry Breecher

major drawbacks however is that it cannot benefit from the hardware acceleration on multi-threaded processors or multi-processor computers: there is never more than one thread being scheduled at the same time. For example: If one of the threads needs to execute an I/O request, the whole process is blocked and the threading advantage cannot be utilized. The GNU Portable Threads uses User-level threading.

M:N (Hybrid threading)OM:N maps some N number of application threads onto some M number of kernel entities, or "virtual processors." This is a compromise between kernel-level ("1:1") and user-level ("N:1") threading. In general, "M:N" threading systems are more complex to implement than either kernel or user threads, because changes to both kernel and user-space code are required. In the M:N implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. However, this increases complexity and the likelihood of priority inversion, as well as suboptimal scheduling without extensive (and expensive) coordination between the userland scheduler and the kernel scheduler.

4: Threads 25


Recommended