Chapter 4: Threads (Adapted by J. Cole) - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Chapter 4: Threads (Adapted by J. Cole)

Description:

Overview - Threads. Threads are a way of performing concurrent or parallel execution within a single ... Linux refers to them as tasks rather than threads ... – PowerPoint PPT presentation

Number of Views:121
Avg rating:3.0/5.0
Slides: 24
Provided by: marily233
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4: Threads (Adapted by J. Cole)


1
Chapter 4 Threads(Adapted by J. Cole)
2
Overview Single Path
  • Traditionally single path of control through a
    program
  • O/S keeps process information in a Process
    Control Block
  • Information about a process
  • Text section (the code)
  • Data area (globals, statics, constants)
  • Stack
  • CPU Registers (including SP and PC)
  • Structures for management of resources (files
    etc)

3
Overview - Processes
  • When multiple processes are running, we have
    concurrent program execution.
  • If multiple processors present, we can have
    parallel execution
  • O/S will perform context switches between
    processes
  • O/S gains control via timer, system call,
    interrupt etc.
  • Things like processor registers must be swapped
  • Many other things as well (future lectures will
    explore this)
  • Other issues when processes need to co-operate
    are
  • Communication, Data sharing, Synchronization
  • Handling these is major subject and another
    future lecture.

4
Overview - Threads
  • Threads are a way of performing concurrent or
    parallel execution within a single process
  • This reduces some of the above complexities as
    well as the overhead of performing a context
    switch
  • Each thread will have its own
  • Thread ID
  • Processor registers (including PC and SP)
  • stack
  • All threads making up a process will share
  • The code section
  • The data area (globals, statics, constants)
  • Other resources such as files

5
Summary
  • Program with single flow of control is
    single-threaded process
  • Can perform one task at a time
  • By using interrupts and clever programming one
    can make such a process appear to do several
    things at once
  • Multiple processes can co-operate to perform more
    than one task at a time, but this is a
    heavyweight solution
  • Multi-threaded processes can perform more than
    one task at a time in a lightweight manner

6
Single and Multithreaded Processes
7
Benefits
  • Responsiveness to user
  • Resource Sharing is made easier
  • Economy thread startup and switching is
    efficient
  • Utilization of MP Architectures

8
User and Kernel Threads
  • User threads Thread management done by
    user-level threads library (or without a library
    at all)
  • Kernel threads supported by the O/S kernel
  • Some thread libraries
  • POSIX Pthreads API standard only. May be
    implemented as user or kernel library.
  • Linux threads older, non-portable thread API
  • Win32 threads
  • Java threads

9
Multi-threading Models
  • There are three main strategies for managing
    kernels in use in various Operating Systems
  • Many-to-One
  • One-to-One
  • Many-to-Many

10
Many-to-One Model
  • This is the mapping of user level threads to a
    single kernel level thread
  • Suffers from same problems of user threads

11
One-to-one Model
  • Each thread in the application is managed as a
    thread in the kernel. Simple.
  • This is the most common (Windows, Linux) and most
    preferred
  • But may lead to a lot of threads

12
Many-to-Many Model
  • O/S decides to manage some user threads as one
    thread
  • Some loss of concurrency but reduces thread count
  • Two-level model lets programmer have some say

13
Two-level Model
14
APIs - 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)
  • See example on page 133
  • pthread_create()
  • pthread_exit()
  • pthread_join()

15
APIs - Windows XP Threads
  • Implements the one-to-one mapping
  • Each thread contains
  • A thread id
  • Register set
  • Separate user and kernel stacks
  • Private data storage area
  • The register set, stacks, and private storage
    area are known as the context of the threads
  • See example on page 135
  • CreateThread()
  • WaitFor.()
  • CloseHandle()

16
APIs - Linux Threads
  • Should use the POSIX API instead
  • Linux refers to them as tasks rather than threads
  • Thread creation is done through clone() system
    call
  • clone() has flexibility to specify what is shared
    between threads
  • clone() allows a child task to share the address
    space of the parent task (process)

17
Threading Issues
  • Semantics of fork() and exec() system calls
  • Thread cancellation
  • Signal handling
  • Thread pools
  • Thread specific data
  • Scheduler activations

18
Semantics of fork() and exec()
  • Does fork() duplicate only the calling thread or
    all threads?

19
Thread Cancellation
  • Terminating a thread before it has finished
  • If the thread finishes then it presumably cleans
    up after itself.
  • Two general approaches to cancellation
  • Asynchronous cancellation terminates the target
    thread immediately
  • Deferred cancellation allows the target thread to
    periodically check if it should be cancelled
  • Often best to request deferred cancellation and
    let the thread tidy up

20
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 signals
  • Signal is generated by particular event
  • Signal is delivered to a process
  • Signal is handled
  • Issue is Which thread to deliver a signal to???
  • 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

21
Thread Pools
  • Create a number of threads in a pool where they
    await work
  • Threads check for work and sleep if none
  • Advantages
  • Usually slightly faster to service a request with
    an existing thread than to create a new thread
  • Allows the number of threads in the
    application(s) to be bound to the size of the pool

22
Thread Specific Data
  • Thread normally has its own stack for local data,
    but may need to have its own global or static
    data.
  • Consider a process which creates N identical
    threads, each of which needs some static data
    space.
  • Thread specific data allows each thread to have
    its own copy of data

23
Lab exercises.A) Record programB) text
examplesC) text project matrix multiplyEnd
of Chapter 4
Write a Comment
User Comments (0)
About PowerShow.com