Module 5: Threads 9/29/03 - PowerPoint PPT Presentation

About This Presentation
Title:

Module 5: Threads 9/29/03

Description:

Mach C-threads - Solaris threads ... but not totally kernel ... Number of kernel threads may be specific to either a particular application or machine ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 33
Provided by: marily189
Category:
Tags: mach | module | number | threads

less

Transcript and Presenter's Notes

Title: Module 5: Threads 9/29/03


1
Module 5 Threads9/29/03
  • Overview
  • Benefits
  • User and Kernel Threads
  • Multithreading Models
  • Solaris 2 Threads
  • Java ThreadsNOTE Instructor annotations in
    BLUE

2
Threads - Overview
  • A thread (or lightweight process) is a basic unit
    of CPU utilization it consists of
  • program counter
  • register set
  • stack space
  • A thread shares with its peer threads (in same
    process) its
  • code section
  • data section
  • operating-system resources
  • collectively know as a task.Actually the three
    things above belong to the process Threads in
    the process share this stuff.
  • A traditional or heavyweight process is equal to
    a task with one thread

3
Threads (- Overview Cont.)
  • In a multiple threaded task, while one server
    thread is blocked and waiting, a second thread in
    the same task can run. (assuming the blocked
    thread doesnt block the process - if so,
    everything comes to a screeching halt!)
  • Cooperation of multiple threads in same job
    confers higher throughput and improved
    performance.
  • Applications that require sharing a common buffer
    (i.e., producer-consumer) benefit from thread
    utilization.
  • Threads provide a mechanism that allows
    sequential processes to make blocking system
    calls while also achieving parallelism. a
    thread within the process makes the call only
    it blocks while other threads in the process
    still run assuming kernel supported threads
  • Kernel-supported threads can reside in user or
    kernel space, but kernel is involved in their
    control.
  • User-level (supported) threads supported from a
    set of library calls at the user level. Resides
    in user space.
  • Hybrid approach implements both user-level and
    kernel-supported threads (Solaris 2).

4
Multiple Threads within a Task
5

Benefits
  • Responsiveness - Opens the possibility of
    blocking only one thread in a process on a
    blocking call rather than the entire process.
  • Resource Sharing Threads share resources of
    process to which they belong code sharing allow
    multiple instantiations of code execution
  • Economy low overhead in thread context
    switching thread management compared to process
    context switching management
  • Utilization of MP Architectures can be applied
    to a multi-processor. In a uniprocessor, task
    switching is so fast that it gives illusion of
    parallelism.

6
Single and Multithreaded Processes
7
User Level (supported) Threads (ULTs)
  • Thread Management Done by User-Level Threads
    Library
  • Kernel unaware of ULTsNo kernel intervention
    needed for thread management.Thus fast to
    create and manage
  • If thread blocks, the kernel sees it as the
    process blocking, and may block the entire
    process (all threads).
  • Examples
  • - POSIX Pthreads
  • - Mach C-threads
  • - Solaris threads but not totally kernel
    independent

8
Kernel Level (supported) Threads (KLTs)
  • Supported by the Kernel thread management done
    by kernel in kernel space.
  • Since kernel managing threads, kernel can
    schedule another thread if a given thread blocks
    rather than blocking the entire processes.
  • Examples
  • - Windows 95/98/NT
  • - Solaris
  • - Digital UNIX

9
Multithreading Models
  • User Threads / Kernel Threads models.
  • Three common types of threading implementation
  • Many-to-One
  • One-to-One
  • Many-to-Many

10
Many-to-One
  • Many User-Level Threads Mapped to Single Kernel
    Thread.
  • Thread management done at user level - efficient
  • Entire user processes blocks if one of its
    threads block kernel only sees the process.
  • Bad for multiprocessor (parallel) architectures
    because only one thread at a time can access
    kernel.
  • Used on Systems That Do Not Support Kernel
    Threads. - Single threaded kernel

11
Many-to-one Model
12
One-to-One
  • Each User-Level Thread Maps to a distinct Kernel
    Thread.
  • Other threads can run when a particular thread
    makes a blocking call - better concurrency.
  • Multiple threads can run in parallel in a
    multiprocessing architecture.
  • Drawback is that creating a user thread requires
    creating a kernel thread overhead factor
    number of threads restricted
  • Examples
  • - Windows 95/98/NT
  • - OS/2

13
One-to-one Model
14
Many-to-Many Model
  • Time Multiplexes many ULTs to a smaller of equal
    number of KLTs.
  • Number of kernel threads may be specific to
    either a particular application or machine
    typically a multiprocessor may get more k-threads
    than a uniprocessor.
  • Programmer may create as many user threads as
    desired (compare to 1-to-1 in which there my be a
    shortage of k-threads.
  • True concurrency limited to scheduling
    (multiplexing) many user threads against a less
    number of k-threads. But the multiple k-threads
    can be run in parallel on a multiprocessor
    machine.

15
Many-to-many Model
16
Pthreads
  • Example of user controlled threads
  • Refers to the POSIX standard (IEEE 1003.1c) API -
    specifies behavior, not implementation.
  • More commonly implemented on UNIX based system
    such as Solaris
  • Runs from a Pthead user level library with not
    specified relationship and any associated kernel
    threads.
  • Some implementations may allow some kernel
    control - ex scheduling???
  • The main function in the C application program
    is the initial thread created by default, all
    other threads are created using library calls
    from the application program- each thread has
    stack

17
Pthreads (continued)
  • Two typical Pthread library calls
  • pthread_create
  • creates a new thread
  • allows you to pass the name of a function which
    has will be the behavior or function that this
    thread will do.
  • Allows you to pass parameters to the function of
    the new thread.
  • sets a thread id variable to the unique tid.
  • Pthread_join
  • allows a thread to wait for another specified
    thread to complete before continuing on - a
    synchronization tedchique
  • There is a very large and rich set of library
    function calls to use in Pthread programming -
    Lots of calls for thread synchronization - more
    later!
  • See example in Figure 5.5, page 140.

18
Threads Support in Solaris 2
  • Solaris 2 is a version of UNIX with support for
    threads at both the kernel and user levels,
    symmetric multiprocessing, and real-time
    scheduling.
  • Solaris 2 implements the pthread API, in addition
    to supporting userlevel threadswith a thread
    library with APIs for thread creation
    management gt Solaris threads
  • LWP intermediate level between user-level
    threads and kernel-level threads.
  • Resource needs of thread types
  • Kernel thread (are in the kernel - does OS stuff
    or associates with an LWP) small data
    structure and a stack thread switching does not
    require changing memory access information
    relatively fast.
  • LWP Associated with a both a user process (PCB)
    and a kernel thread. Has register data,
    accounting, and memory information switching
    between LWPs is relatively slow. - because of
    kernel intervention. Scheduled by kernel
  • User-level thread only need stack and program
    counter no kernel involvement means fast
    switching (multiplexing to LPW) . Kernel only
    sees the LWPs that are dedicated to user-level
    threads.

19
Solaris 2 Threads
20
Solaris Threads
  • Threads in user space similar to pure Pthreads
    but with required kernel control
  • Process Structure control information completely
    in kernel space
  • LWP in kernel space but share the user process
    resources - can interface with user threads - is
    visible to user threads.
  • LWP can be thought of as a virtual CPU that is
    available for executing code .. . We have a
    single process in one memory space with many
    virtual CPUs running different parts of the
    process concurrently (Lewis Berg).
  • Each LWP is separately scheduled by kernel -
    kernel schedules LWPs not user threads.
  • The thread library for user threads is in user
    space - the user level thread library schedules
    (multiplexes) user threads onto LWPs, and LWPs,
    in turn, are implemented by kernel threads and
    scheduled by the kernel.

21
Solaris Process
22
Java Threads
  • Java Threads May be Created by
  • Extending Thread class
  • Implementing the Runnable interface

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

24
Creating the Thread
  • public class First
  • public static void main(String args)
  • Worker runner new Worker1()
  • runner.start()
  • System.out.println(I am the main thread)

25
The Runnable Interface
  • public interface Runnable
  • public abstract void run()

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

27
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)

28
Java Thread Management
  • 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.

29
Java Thread States
Runnable state has both actively executing
threads, and ready threads
30
Producer Consumer Problem
  • public class Server
  • public Server()
  • MessageQueue mailBox new MessageQueue()
  • Producer producerThread new
    Producer(mailBox)
  • Consumer consumerThread new
    Consumer(mailBox)
  • producerThread.start()
  • consumerThread.start()
  • public static void main(String args)
  • Server server new Server()

31
Producer Thread
  • class Producer extends Thread
  • public Producer(MessageQueue m)
  • mbox m
  • public void run()
  • while (true)
  • // produce an item enter it into the buffer
  • Date message new Date()
  • mbox.send(message)
  • private MessageQueue mbox

32
Consumer Thread
  • class Consumer extends Thread
  • public Consumer(MessageQueue m)
  • mbox m
  • public void run()
  • while (true)
  • Date message (Date)mbox.receive()
  • if (message ! null)
  • // consume the message
  • private MessageQueue mbox
Write a Comment
User Comments (0)
About PowerShow.com