Chapter 5: Threads - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Chapter 5: Threads

Description:

If the process has multiple threads of control, it can do more than one task at a time. ... User-level threads are generally fast to create and manage. ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 30
Provided by: marily227
Category:
Tags: chapter | threads

less

Transcript and Presenter's Notes

Title: Chapter 5: Threads


1
Chapter 5 Threads
  • Overview
  • Multithreading Models
  • Threading Issues
  • Pthreads
  • Solaris 2 Threads
  • Windows 2000 Threads
  • Linux Threads
  • Java Threads

2
Overview
  • A thread (or lightweight process) is a basic unit
    of CPU utilization
  • It consists of program counter, register set, and
    stack space.
  • A thread shares with other threads belonging the
    same process its code section, data section, and
    operating-system resources, such as open files
    and signals.
  • A traditional (or heavyweight) process is equal
    to a task with one thread.
  • If the process has multiple threads of control,
    it can do more than one task at a time.

3
Motivation
  • Many software packages that run on modern desktop
    PCs are multithreaded.
  • Examples
  • A web browser might have one thread displaying
    images or text while another thread retrieves
    data from the network.
  • A word processor may have a thread for displaying
    graphics, another thread for reading keystrokes
    for the user, and a third thread for performing
    spelling and grammar checking in the background.
  • When a web server receives a request, it creates
    a separate process to serve that request.
  • Process creation is very heavyweight.
  • It is generally more efficient for one process
    that contains multiple threads to serve the same
    purpose.

4
Single and Multithreaded Processes
5
Benefits
  • Responsiveness
  • multithreading an interactive application may
    allow a program to continue running even if part
    of it is blocked or is performing a lengthy
    operation, thereby increasing responsiveness to
    the user.
  • (e.g.) Web broser
  • Resource sharing
  • By default, threads share the memory and the
    resources of the process to which they belong
  • Economy
  • Allocating memory and resources for process
    creation is costly.
  • Since threads share resources of the process, it
    is more economical to create and context switch
    threads.
  • Utilization of MP Architectures
  • Each threads may be running in parallel on a
    different processor

6
User Threads
  • Thread is implemented by a thread library at
    user-level.
  • The library provides support for thread creation,
    scheduling, and management with no support from
    the kernel.
  • User-level threads are generally fast to create
    and manage.
  • If the kernel is single-threaded, then any
    user-level thread performing a blocking system
    call will cause the entire process to block, even
    if other threads are available to run.
  • Examples
  • - POSIX Pthreads
  • - Mach C-threads
  • - Solaris threads

7
Kernel Threads
  • Kernel threads are directly supported by the OS.
  • The kernel performs thread creation, scheduling,
    and management in kernel space.
  • The kernel threads are generally slower to create
    and manage.
  • If a thread performs a blocking system call, the
    kernel can schedule another thread.
  • Examples
  • - Windows 95/98/NT/2000
  • - Solaris
  • - Tru64 UNIX
  • - BeOS
  • - Linux

8
Multithreading Models
  • Many systems provides support for both user and
    kernel threads, resulting in different
    multhreading models
  • Three common types of threading implementation
  • Many-to-one model
  • One-to-one model
  • Many-to-many model

9
Many-to-One Model
  • Many user-level threads mapped to single kernel
    thread.
  • Advantage
  • Efficient,
  • Disadvantage
  • The entire process will block if a thread makes a
    blocking system call
  • Multiple threads are unable to run in parallel on
    multiprocessors
  • Example
  • Green thread -- thread library available for
    Solalis 2
  • Used on systems that do not support kernel
    threads.

10
One-to-One Model
  • Each user-level thread maps to a kernel thread.
  • Advantage
  • More concurrency than the many-to-one model by
    allowing another thread to run when a thread
    makes a blocking system call
  • Allows multiple threads to run in parallel on
    multiprocessors
  • Drawback
  • Creating a user thread requires creating the
    corresponding kernel thread
  • Because of the overhead, most implementations
    restrict the number of threads supported by the
    systme
  • Examples
  • Windows 95/98/NT/2000, OS/2

11
Many-to-Many Model
  • Multiplexes many user-level threads to a smaller
    or equal number of kernel threads.
  • The number of kernel threads may be specific to
    either a particular application or a particular
    machine.
  • Developers can create as many user threads as
    necessary, and the corresponding kernel threads
    can run in parallel on a multiprocessor
  • When a thread performs a blocking system call,
    the kernel can schedule another thread for
    execution
  • Example
  • Solaris 2, IRIX, HP-UX, True64 UNIX

12
Threading Issues
  • Semantics of fork() and exec() system calls.
  • Thread cancellation.
  • Signal handling
  • Thread pools
  • Thread specific data

13
The 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 UNIX systems have chosen to have two
    versions of fork
  • One that duplicates all threads,
  • And another that duplicates only the thread that
    invoked the fork system call
  • If exec is called immediately after forking,
    duplicating only the calling thread is appropriate

14
Cancellation
  • Thread cancellation is the task of terminating a
    thread before it has completed.
  • Examples
  • If multiple threads are concurrently searching
    through a database and one thread returns the
    result, the remaining threads might be cancelled.
  • Assume that a web page is loaded in a separate
    thread. When a user presses the stop button, the
    thread loading the page is cancelled.
  • Asynchronous cancellation One thread immediately
    terminates the target thread
  • The difficulty occurs in situations where
    resources have been allocated to a cancelled
    thread or if a thread was cancelled while in the
    middle of updating data it is sharing with other
    threads.
  • Deferred cancellation The target thread can
    periodically check if it should terminate,
    allowing the target thread an opportunity to
    terminate itself in an orderly fashion.
  • Tthread API allow deferred cancellation at
    cancellation points

15
Signal Handling
  • Signal is used in UNIX systems to notify a
    process that a particular even has occurred.
  • Signal handling procedure
  • A signal is generated by the occurrence of a
    particular event
  • A generated signal is delivered to a process
  • Once delivered, the signal must be handled.
  • Examples of synchronous signal
  • Illegal memory access, Division by zero
  • Synchronous signals are delivered to the same
    process that performed the operation causing the
    signal
  • Examples of asynchronous signal
  • Terminating a process with a specific keystroke
    (C)
  • Typically an asynchronous signal is sent to
    another process

16
Signal Handling (cont.)
  • Two possible signal handlers
  • A default signal handler
  • A user-defined signal handler
  • Delivering signals in multithreaded programs.
  • 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 signal
    for the process.
  • The method for delivering a signal depends upon
    the type of signal generated
  • Synchronous signal is delivered to the thread
    that generated the signal
  • A signal that terminates a process (C) should be
    sent to all threads
  • Solalis 2 creates a specific thread within each
    process solely for signal handling

17
Thread Pools
  • The general idea is to create a number of threads
    at process startup and place them into a pool,
    where they sit and wait for work.
  • Benefits of thread pools
  • It is usually faster to service a request with an
    existing thread than waiting to cerate a thread.
  • A thread pool limits the number of threads that
    exist at any one point. This is particularly
    important on systems that cannot support a large
    number of concurrent threads.
  • The number of threads in the pool can be set
    heuristically based upon factors such as the
    number of CPUs in the system, the amount of
    physical memory, and the expected number of
    concurrent client requests

18
Thread-Specific Data
  • Thread belonging to a process share the data of
    the process.
  • However, each thread might need its own copy of
    certain data in some circumstance.
  • We will call such data thread-specific data
  • Example
  • Each transaction may be assigned a unique
    identifier in a transaction-processing system. To
    associate each thread with its unique identifier
    we could use thread-specific data

19
Pthreads
  • Pthread refers to the POSIX standard (IEEE
    1003.1c) defining an 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
  • The Windows operating systems have generally not
    supported Pthreads
  • Example
  • The program in the following page demonstrates
    the basic Pthread API for constructing a
    multithreaded program

20
Multithreaded C program using Pthread API
/ This program implements the summation function
where the summation operation is run as a
separate thread. Usage on Solaris/Linux/Mac OS
X gcc thrd.c -lpthread a.out ltnumbergt / include
ltpthread.hgt include ltstdio.hgt int sum
/ this data is shared
by the thread(s) / void runner (void param)
/ the thread / main (int argc, char
argv) pthread_t tid / the
thread identifier / pthread_attr_t attr
/ set of attributes for the thread / if
(argc ! 2) fprintf (stderr, "usage a.out
ltinteger valuegt\n") exit ()
if (atoi (argv1) lt 0) fprintf (stderr,
"Argument d must be non-negative\n", atoi
(argv1)) exit ()
21
Multithreaded C program (cont.)
/ get the default attributes /
pthread_attr_init (attr) / create the
thread / pthread_create (tid, attr, runner,
argv1) / now wait for the thread to exit
/ pthread_join (tid, NULL) printf ("sum
d\n", sum) / The thread will begin
control in this function / void runner(void
param) int upper atoi (param) int I
sum 0 if (upper gt 0) for (i 1 i
lt upper i) sum I
pthread_exit (0)
22
Solaris 2 Threads
23
Threads Support in Solaris 2
  • Solaris 2 is a version of UNIX with support for
    threads at the kernel and user levels, symmetric
    multiprocessing (SMP), and real-time scheduling.
  • Light weight process (LWP ?? ????)
    intermediate level between user-level threads and
    kernel-level threads.
  • The thread library multiplexes user-level threads
    on the pool of LWPs for the process.
  • Solaris 2 implements the many-to-many model.
  • User-level threads may be either bounded or
    unbounded
  • A bounded user-level thread is permanently
    attached to an LWP
  • An unbounded threads is not permanently attached
    to any LWP.
  • All unbounded threads in an application are
    multiplexed onto the pool of available LWPs for
    the application.
  • Threads are unbounded by default.
  • User-level threads may be scheduled and switched
    among the LWPs by the thread library without
    kernel intervention ? Efficient

24
Threads Support in Solaris 2 (cont.)
  • ? LWP? ??? ??? Kernel-level thread? ???.
  • The kernel threads are scheduled by the kernels
    scheduler and execute on the CPU or CPUs in the
    system.
  • ? LWP? ?? User-level thread? ??? ? ???, ? LWP?
    ???? ?? ??? ?? User-level thread? ???.
  • The thread library dynamically adjusts the number
    of LWPs in the pool to ensure the best
    performance.
  • ? Thread ??? ??? ????
  • User-level thread contains a thread ID, register
    set, stack, and priority. All exists within user
    space
  • LWP has a register set for the user-level thread
    it is running, as well as memory and accounting
    information. An LWP is a kernel data structure.
  • A kernel thread has only small data structure and
    a stack. The data structure includes a copy of
    the kernel registers, a pointer to the LWP to
    which it is attached, and priority and scheduling
    information.

25
Solaris 2 Process
26
Windows 2000 Threads
  • Windows 2000 implements the Win32 API
  • Implements the one-to-one mapping.
  • General components of a thread include
  • Thread id
  • Register set representing the status of the
    processor
  • Separate user and kernel stacks
  • Private data storage area used by various
    run-time libraries and dynamic link libraries
    (DLLs)
  • Primary data structures of a thread include
  • ETHREAD (executive thread block)
  • KTHREAD (kernel thread block)
  • TEB (thread environment block)

27
Linux Threads
  • Linux refers to them as tasks rather than
    threads.
  • Thread creation is done through clone() system
    call.
  • Clone() allows a child task to share the address
    space of the parent task (process)

28
Java Threads
  • Java threads may be created by
  • Extending Thread class
  • Implementing the Runnable interface
  • Java threads are managed by the JVM.

29
Java Thread States
Write a Comment
User Comments (0)
About PowerShow.com