Chapter 4: Threads - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 4: Threads

Description:

Shares address space and other global information with its process ... A program counter. A register set. A stack. Process vs Threads ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 26
Provided by: marily326
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4: Threads


1
Chapter 4 Threads
2
Chapter 4 Threads
  • Overview
  • Multithreading Models
  • Threading Issues
  • Pthreads
  • Windows XP Threads
  • Linux Threads
  • Java Threads

3
Thread Definition
  • A Thread is a basic unit of CPU utilization.
  • A thread of execution.
  • A placeholder information associated with a
    single use of a program that can handle multiple
    concurrent users
  • Thread
  • Lightweight process (LWP)
  • Threads of instructions or thread of control
  • Shares address space and other global information
    with its process
  • Registers, stack, signal masks and other
    thread-specific data are local to each thread

4
Thread Composition
  • A Thread is comprised of
  • A thread ID.
  • A program counter.
  • A register set.
  • A stack.

5
Process vs Threads
  • A traditional or heavyweight process has a single
    thread of control.
  • If a process has multiple threads of control, it
    can perform more than one task at a time, thus a
    thread is a lightweight process.

6
Single and Multithreaded Processes
7
Benefits
  • Responsiveness
  • A program may continue even if parts of it are
    blocked.
  • Ex web browser loading some images while others
    take more time.
  • Resource Sharing
  • By default, threads share memory and the
    resources of the process to which they belong.
  • Economy
  • Easier to create and context switch between
    threads since they share resources.
  • Utilization of MP Architectures
  • Run multiple threads in parallel, taking
    advantage of multiple processors.

8
Benefits
  • Takes less time to create a new thread than a
    process.
  • Less time to terminate a thread than a process.
  • Less time to switch between two threads within
    the same process.
  • Since threads within the same process share
    memory and files, they can communicate with each
    other without invoking the kernel

9
Multithreading Modes
  • Provided in different levels
  • User Threads.
  • Supported above the kernel and managed without
    kernel support.
  • Kernel Threads.
  • Supported and managed directly by the operating
    system.

10
User Threads
  • Thread management done by user-level threads
    library.
  • The kernel is not aware of the existence of
    threads.
  • Three primary thread libraries
  • POSIX Pthreads
  • Win32 threads
  • Java threads

11
Kernel Threads
  • Supported by the Kernel
  • Kernel maintains context information for the
    process and the threads
  • Examples
  • Windows XP/2000
  • Solaris
  • Linux
  • Tru64 UNIX
  • Mac OS X

12
Multithreading Models
  • Many-to-One
  • Maps many user-level threads to a single kernel
    thread.
  • One-to-One
  • Maps each user-level thread to a kernel thread.
  • Many-to-Many
  • Multiplexes many user-level threads to a smaller
    or equal number of kernel threads.

13
Many-to-One
  • Many user-level threads mapped to single kernel
    thread.
  • Thread management is done by the thread library
    in user space.
  • Will block if thread makes a blocking system
    call.
  • Multiple threads are unable to run in parallel on
    multiprocessors.
  • Examples
  • Solaris Green Threads
  • GNU Portable Threads

14
Many-to-One Model
15
One-to-One
  • Each user-level thread maps to kernel thread.
  • Provides more concurrency allowing another thread
    to run when one makes a blocking system call.
  • Allows multiple threads to run in parallel in
    multiprocessors.
  • Developer must be careful not to make too many
    threads per application.
  • Examples
  • Windows NT/XP/2000
  • Linux
  • Solaris 9 and later

16
One-to-one Model
17
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.
  • Developers can create as many threads as
    necessary.
  • Solaris prior to version 9.
  • Windows NT/2000 with the ThreadFiber package.

18
Many-to-Many Model
19
Two-level Model
  • Similar to MM, except that it allows a user
    thread to be bound to kernel thread
  • Examples
  • IRIX
  • HP-UX
  • Tru64 UNIX
  • Solaris 8 and earlier

20
Two-level Model
21
Thread State Life Cycle of a Thread
  • Thread states
  • Born state
  • Ready state (runnable state)
  • Running state
  • Dead state
  • Blocked state
  • Waiting state
  • Sleeping state
  • Sleep interval specifies for how long a thread
    will sleep

22
Thread State Life Cycle of a Thread
  • Thread Life Cycle

23
Thread Libraries
  • A thread library provides the programmer an API
    for creating and mapping threads.
  • Two ways of implementing a thread library
  • Library entirely in user space with no kernel
    support.
  • Invoking a function in the library results in a
    local call function not a system call.
  • Kernel-level library supported directly by the
    operating system.
  • Code and structures reside in kernel space.
  • Invoking a function in the API typically results
    in a system call to the kernel.

24
Thread Libraries
  • Three main libraries are used today
  • POSIX Pthreads.
  • User or kernel level.
  • Win32.
  • Kernel level only on windows systems.
  • Java.
  • Java programs.

25
Pthreads
  • 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)
  • Third party support for Windows Systems.
Write a Comment
User Comments (0)
About PowerShow.com