Chapter 5 Threads - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Chapter 5 Threads

Description:

All threads of a process share this ... SMP machine: multiple threads can possibly be ... Less time to switch between two threads within the same process ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 58
Provided by: ccNct
Category:
Tags: chapter | threads

less

Transcript and Presenter's Notes

Title: Chapter 5 Threads


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

3
Overview
4
Process Characteristics
  • Unit of resource ownership - process is
    allocated
  • A virtual address space to hold the process image
    and data
  • Control of some resources (files, I/O devices...)
  • Unit of dispatching or CPU utilization - process
    is a single execution path determined by the PC
  • Dispatch unit of short-term scheduler
  • Execution may be interleaved with other process
  • The process has an execution state (PC,
    registers, stack) and a dispatching priority

5
Process Characteristics (Cont.)
  • The two characteristics are treated independently
    by some modern OS
  • The unit of dispatching is usually referred to a
    thread or a lightweight process
  • The unit of resource ownership is usually
    referred to as a process or task

6
Thread Definition
  • Lightweight process (LW)
  • Basic unit of CPU utilization
  • A thread comprises a thread ID, a program
    counter, a register set, and a stack
  • A thread shares with other threads belonging to
    the same process its code section, data section,
    and other OS resources, such as open files and
    signals
  • A process with multiple threads can do more than
    one task at a time

7
Single and Multithreaded Processes
8
A Very Very Simple Example
  • To do the ?????

Single Thread. for(i0 ilt9 i) for (j0 j
lt 9 j) numberij (i1) (j1) The
only thread is scheduling and dispatching by CPU
scheduler If 9 other processes are ready to run,
this thread may only get 1/10 CPU Time
Multiple Threads. Thread 1 for (j0 j lt 9
j) number0j 1 (j1) Thread 2 for
(j0 j lt 9 j) number1j 2
(j1) The 9 threads are scheduling and
dispatching by CPU scheduler9/(99) CPU Time
????
9
Threads
Process Context-Switch ? Thread CS Process
Scheduling ? Thread Scheduling
  • Has an execution state (running, ready, etc.)
  • Scheduling and dispatching is done on a thread
    basis
  • Saves thread context when not running
  • Independent PC, CPU registers
  • Has an execution stack and some per-thread static
    storage for local variables
  • Has shared access to the memory address space and
    resources of its process
  • All threads of a process share this
  • When one thread alters a (non-private) memory
    item, all other threads (of the process) sees
    that
  • A file opened by one thread is available to others

10
Single Threaded and Multithreaded Process Models
Thread Control Block contains a register image,
thread priority and thread state information
11
Thread Example
  • Example 1 a file server on a LAN
  • Needs to handle several file requests over a
    short period
  • How to do???
  • Process sequentially
  • Create (and destroy) a single process for each
    request
  • Alternative More efficient to create (and
    destroy) a single thread for each request
  • On a SMP machine multiple threads can possibly
    be executing simultaneously on different
    processors
  • Example 2
  • Web browser display images or text, retrieve
    data
  • Web server serve many request

12
Benefits of Threads Over Processes
  • 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

If there is an application that should be
implemented as a set of related units of
execution, it is far more efficient to do so as a
collection of threads rather than a collection of
separate processes.
13
Uses of Threads
  • Foreground to background work
  • One thread display menu and read user input while
    the other thread execute user commands
  • Asynchronous processing
  • One for periodic backup, the other for normal
    operation
  • Speed execution
  • Multiple threads from the same processes can
    execute simultaneously in a multiprocessing
    system
  • Modular program structure
  • Consider an application that consists of several
    independent parts that do not need to run in
    sequence
  • Each part can be implemented as a thread

14
Benefits of Threads
  • Responsiveness
  • Resource Sharing
  • Economy
  • More economical to create and context switch
    threads than processes
  • In Solaris. process vs. thread
  • 30 times slower for creating
  • 5 times slower for context switching
  • Utilization of MP Architectures
  • Each thread may be running in parallel on a
    different processor

15
Remote Procedure Call Using Single Thread
16
Remote Procedure Call Using Threads
17
Threads States
  • Three key states running, ready, waiting
  • Thread operation spawn, block, unblock, finish
  • They have no suspend state because all threads
    within the same process share the same address
    space
  • Indeed suspending (ie. swapping) a single thread
    involves suspending all threads of the same
    process
  • Termination of a process, terminates all threads
    within the process

18
User and Kernel Threads
19
User Threads
  • Managed by user-level Threads Library
  • No support from the kernel (The kernel is not
    aware of the existence of threads)
  • Fast to create and manage
  • Block all threads for a blocking system call if
    the kernel is single threaded
  • Cannot take advantage of multi-processors
  • Examples
  • POSIX Pthreads
  • Mach C-threads
  • Solaris UI-threads

Emulate by Thread Library for the illusion of
separate Thread ID, PC, register set and stack
20
Kernel Threads
  • Supported by the Kernel
  • Kernel maintains context information for the
    process and the threads
  • Scheduling is done on a thread basis
  • Slower to create and manage
  • If a thread performs a blocking system call, the
    kernel can schedule another thread in the
    application for execution
  • Can take advantage of a multi-processor
    environment
  • Examples
  • Windows 95/98/NT/2000
  • Solaris
  • Tru64 UNIX
  • BeOS
  • Linux

Thread is the basic unit for CPU scheduling
21
Multithreading Models
  • How to map user threads to kernel threads
  • Many-to-One Model
  • One-to-One Model
  • Many-to-Many Model

22
Many-to-One Model
  • Many user-level threads mapped to single kernel
    thread
  • Thread management is done in user space
  • Blocking problem
  • No support for running in parallel on MP
  • Used on systems that do not support kernel
    threads.
  • Green-threads library in Solaris 2

Thread Library
23
One-to-One Model
  • Each user-level thread maps to a kernel thread
  • Creating a user thread requires creating the
    corresponding kernel thread
  • Overhead
  • Restrict the number of threads supported by the
    OS
  • Examples
  • Windows NT/2000
  • OS/2

24
Many-to-Many Model
  • Multiplex many user-level threads to a smaller or
    equal number of kernel threads
  • Allows many user level threads to be mapped to
    many kernel threads.
  • Allows OS to create a sufficient number of kernel
    threads.
  • Examples
  • Solaris 2, IRIX, HP-UX, Tru64 UNIX
  • Windows NT/2000 with the ThreadFiber package

25
Threading Issues
26
The fork and exec System Calls
  • If one thread calls fork, does the new process
    duplicate all threads or is the new process
    single-threaded?
  • Some system supports both
  • Depend on the application
  • If a thread invokes the exec system call, the
    program specified will replace the entire process
    including all threads and LWPs

27
Thread Cancellation
  • Terminate a thread before it has completed
  • Scenarios for the cancellation of a target thread
  • Asynchronous cancellation one thread immediate
    terminates the target thread
  • 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
  • Difficulty with cancellation resource reclaim
  • Canceling a thread asynchronously may not free a
    necessary system-wide resource
  • Deferred cancellation allows the target thread to
    check if it should be cancelled at a point when
    it can safely be cancelled

28
Signal Handling
  • Signal used in UNIX to notify a process that a
    particular event has occurred
  • Synchronous signals are delivered to the same
    process that performed the operation causing the
    signal
  • Example illegal memory access, divide by zero
  • Asynchronous signals are generated by events
    external to a running process
  • Terminate a process with specific keystrokes,
    timer expires
  • Signals in a single-threaded system are always
    delivered to a process

29
Signal Handling and Threads
  • Where should a signal be delivered when a process
    has several threads?
  • Deliver the signal to the thread to which the
    signal applies
  • Synchronous signals
  • Deliver the signal to every thread in the process
  • Deliver the signal to certain threads in the
    process
  • Threads that do not block the signals
  • Because signals need to be handled only once,
    typically a signal is delivered only to the first
    thread that is not blocking the signal
  • Assign a specific thread to receive all signals
    for the process
  • Solaris 2

30
Asynchronous Procedure Calls (APC) in Windows 2000
  • Emulate asynchronous signals in UNIX
  • APC allows a user thread to specify a function
    that is to be called when the user thread
    receives notification of a particular event
  • APC is delivered a particular thread rather than
    process

31
Thread Pool
  • Scenario of multithreading a web server
  • Creating and destroy threads dynamically time
    consuming
  • Unlimited threads could exhaust system resources
  • Thread pool
  • Create a number of threads at process startup and
    place them into a pool, where they sit and wait
    for work
  • When a server receives a request, awaken a thread
    from the pool and pass it the request to service
  • Once the thread completes its service, return to
    the pool
  • If the pool contains no available thread, the
    server waits until one becomes free

32
Thread Pool (Cont.)
  • Benefit
  • Faster to service a request with an existing
    thread than waiting to create 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
  • of CPUs, physical memory, concurrent requests
  • Dynamically adjust according to usage patterns

33
Thread-Specific Data
  • Each tread might need its own copy of certain
    data
  • Example Service each transaction in a separate
    thread
  • Most thread libraries provide some form of
    support for thread-specific data
  • Win32
  • Pthreads
  • Java

34
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
  • User-level thread library
  • Example program Figure 5.5 (p. 140)

35
Solaris 2 Threads
Many-to-Many
36
Solaris Threads (Cont.)
  • User-level threads API library for thread
    management
  • Pthread and UI-threads
  • Lightweight process (LWP) intermediate level
  • Each process contains at least one LWP
  • Thread library multiplexes user threads on the
    pool of LWPs
  • Only user-level threads connecting to an LWP
    accomplish work
  • Thread library dynamically adjust of LWP
  • Kernel-level threads
  • A kernel-level thread for each LWP
  • Some kernel threads for kernel tasks (no LWP)
  • Kernel threads are the only objects scheduled
    within the system

37
Bounded and Unbounded User-Level Threads in
Solaris
  • Bound permanently attach to a LWP
  • For processes required quick response time
  • Unbound multiplex onto the pool of available
    LWPs
  • Threads are unbound by default

38
Solaris Thread Implementation
  • User-level thread
  • Thread ID, register set, stack, and priority
  • User-level data structures
  • LWP
  • A register set for the user-level thread it is
    running
  • Memory and accounting information
  • Kernel data structure
  • Kernel thread
  • Kernel registers, pointer to the LWP, priority
    and scheduling information, and a stack

39
PCB of A Solaris Process
40
(No Transcript)
41
Windows 2000 Threads
  • Implements the one-to-one mapping
  • Each thread contains
  • A thread ID
  • Register set
  • Separate user and kernel stacks for user and
    kernel modes
  • Private data storage area used by various
    run-time libraries and dynamic link libraries
    (DDLs)
  • The latter three are known as the context of the
    thread and are architecture-specific to HW

42
Windows 2000 Threads Data Structure
  • ETHREAD (Executive thread block) kernel-space
    DS
  • Pointer to the process to which the thread
    belongs
  • The address of the routine in which the thread
    starts control
  • Pointer to the corresponding KTHREAD
  • KTHREAD (kernel thread block) kernel-space DS
  • Scheduling and synchronization information
  • Kernel-mode stack
  • Pointer to the TEB
  • TEB (thread environment block) user-space DS
  • User-mode stack
  • An array for thread-specific data (thread-local
    storage)

43
Linux Thread
  • 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)
  • Clone() allow various levels of sharing between
    nothing and all
  • Linux PCB contains pointers to other DS where the
    process data (open files, page tables) is stored
  • Fork a new process is created along with a copy
    of all the associated data structure of the
    parent process
  • Clone a new process that points to the data
    structures of the parent process is created

44
Process Control Block (PCB)
45
Illustration of Linux PCB
Page Table
Open FileTable
Registers
46
Fork and Linux PCB
Page Table
Page Table
Open FileTable
Open FileTable
Copy
Registers
Registers
New Task
47
Clone and Linux PCB
Page Table
Open FileTable
New Task
Registers
48
Java Thread
49
Java Threads
  • Language-level support for creation and
    management of threads
  • All Java programs comprise at least one single
    thread of control
  • Java threads are managed by the JVM
  • Difficult to classify Java threads as either
    user- or kernel-level
  • Java Threads may be created by
  • Extend Thread class and override the run() method
  • Implementing the Runnable interface

50
Extending the Thread Class
  • class Worker1 extends Thread
  • public void run()
  • System.out.println(I am a Worker Thread)

class A extends B Declare A is a sub-class of B
(1) A has all the variables and methods defined
in B (2) A can override methods already defined
in B (3) A can declare new variables and methods
51
Creating the Thread
  • public class First
  • public static void main(String args)
  • Worker runner new Worker1()
  • runner.start() // Initialization call
    Threads run()
  • System.out.println(I am the main thread)

(1) Non-Java function call write(filename,
.)(2) Java method call filename.write(...)
(write is a method defined in the class of
File, and filename is an instance of File
52
Java Thread Management
  • start() starts execution of a thread
  • Allocate memory and initialize a new thread in
    JVM
  • It calls the run() method, making the thread
    eligible to be run by JVM
  • suspend() suspends execution of the currently
    running thread.
  • sleep() puts the currently running thread to
    sleep for a specified amount of time.
  • resume() resumes execution of a suspended
    thread.
  • stop() stops execution of a thread.

53
Java Thread States
54
The Runnable Interface
  • public interface Runnable
  • public abstract void run()

55
Implementing the Runnable Interface
  • class Worker2 implements Runnable
  • public void run()
  • System.out.println(I am a Worker Thread)

56
Creating the Thread
  • public class Second
  • public static void main(String args)
  • Runnable runner new Worker2()
  • Thread thrd new Thread(runner)
  • thrd.start()
  • System.out.println(I am the main thread)

57
Thread, JVM, Host OS
  • Threads for JVM to handle system-level tasks
  • Garbage-collector thread
  • Time events, graphics controls, update the screen
  • JVM and Host OS
  • No indication how Java threads are to be mapped
    to the underlying OS
  • Typically, a Java thread is a user-level thread
    and the JVM is responsible for thread management
  • Windows 95/98/NT/2000 One-to-One model
  • JVM 1.1 with Solaris 2.6 Many-to-Many model/
Write a Comment
User Comments (0)
About PowerShow.com