Threads - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Threads

Description:

A process may fork off a child process which can fork off a child process ... Unstarted: program creates the object and passes a ThreadStart delegate to the ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 14
Provided by: paulde8
Category:

less

Transcript and Presenter's Notes

Title: Threads


1
Threads
2
Process
  • A process is a program in execution
  • It has
  • Code
  • Memory
  • Other resources
  • Files
  • Printer
  • Processor

3
Multiprogramming
  • Many processes run concurrently
  • OS
  • For each user
  • Browser
  • Editor
  • Compiler
  • Etc.
  • These processes must share physical resources

4
An Analogy
  • A collection of threads is to a single process as
    a collection of processes is to a single computer
    system
  • Both run in parallel
  • Both share resources
  • Threads share
  • The process memory
  • The process files

5
Some Background
  • A process may fork off a child process which can
    fork off a child process
  • Each of these processes are clones of one another
    except that each has its own address space
  • Resulting in a tree-like organization of
    processes
  • Communication between processes is done through
    special IPC mechanisms

6
Lightweight Processes
  • Threads are sometimes called lightweight
    processes because
  • They have separate threads of control (as do
    processes)
  • But share memory (as processes do not)

7
Use
  • Suppose we are downloading an audio clip
  • We could either wait until the entire clip is
    downloaded or we could divide the process into
    threads
  • One thread downloads
  • One thread plays
  • The key notion here is that threads are only
    useful when there is a mix of activities, some of
    which may block waiting on resources
  • Threads yield no performance gain when all of
    them are CPU bound, but when there is substantial
    computing and also substantial i/o, having
    threads allows these activities to overlap, thus
    speeding up the application. Tannenbaum, Modern
    Operating Systems, p. 85, (2nd edition,
    Prentice-Hall).

8
Process Creation Examples
  • Parent/Child
  • Creates a child process
  • Overwrites the child process with the name of a
    program gotten from the command line
  • Notice the sequence of execution
  • Pipes
  • Create a childe process
  • Communicate over an IPC mechanism called a pipe.

9
Concurrent Processesthe Downside
  • Processes are expensive to create
  • Historically, the type of concurrency that
    computers perform today generally has been
    implemented as operating system primitives
    available only to highly experienced systems
    programmers.

10
In Steps ADA/GUI
  • Used by DOD
  • Made concurrency widely available
  • Development of GUIs made concurrency more useful
  • Now
  • Multithreading is available to all .NET
    programming languages
  • Also a part of Java

11
A thread object is always in one of several states
  • Unstarted program creates the object and passes
    a ThreadStart delegate to the objects
    constructor
  • Running program calls the objects Start
    method. Now the newly running thread can run
    concurrently with any other threads that are part
    of the process.
  • Blocked thread waits on a resource
  • Stopped ThreadStart delegate terminates
  • Suspended threads Suspend method has been
    called
  • WaitSleepJoin
  • Thread encounters code it cant execute. It
    waits until another thread tells it to resume
  • Thread can decide to sleep for a prescribed time
    interval
  • Thread should not continue until another thread
    finishes at which time it joins the just
    completed thread and resumes.

12
Thread Scheduling
  • Every thread has a priorityThreadPriorityHighest
    to ThreadPriorityLowest
  • Imagine all threads in a given priority on a
    separate list.
  • Each thread in a list gets a burst of processor
    time called a quantum.
  • If it doesnt finished, the next thread gets a
    burst.
  • When all threads at a given priority level
    complete, the scheduler turns its attention to
    the next highest priority list.

13
Examples
  • Ch 14 pgm58thread
  • Program creates three threads of execution, each
    with a default priority Normal.
  • Each thread displays a message indicating that it
    is going to sleep for a random amount of time in
    the interval 0 to 5000 milliseconds.
  • When the thread awakens, it displays its name,
    terminates and enters the Stopped state
  • The application consists of
  • MessagePrinter
  • The class containing the method executed by the
    thread
  • Computes random interval
  • Displays message
  • _tmain (contained in ThreadTester.cpp)
  • Creates three instances of MessagePrinter
  • Creates three threads
  • Starts them using the delegate class ThreadStart
  • Passes each the name of the method in
    MessagePrinter to execute
Write a Comment
User Comments (0)
About PowerShow.com