Chapter 4: Multithreaded Programming - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Chapter 4: Multithreaded Programming

Description:

Threads vs. Processes. Creation of a new process using fork is expensive (time ... Allows the operating system to create a sufficient number of kernel threads ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 44
Provided by: marily227
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4: Multithreaded Programming


1
Chapter 4 Multithreaded Programming
2
Chapter 4 Multithreaded Programming
  • Overview
  • Multithreading Models
  • Threading Issues
  • Pthreads
  • Windows XP Threads
  • Linux Threads
  • Java Threads

3
Threads vs. Processes
Creation of a new process using fork is
expensive (time memory). A thread (sometimes
called a lightweight process) does not require
lots of memory or startup time.
4
fork()
fork()
5
pthread_create()
Process A Thread 1
pthread_create()
Process A Thread 2
Stack
6
Multiple Threads
  • Each process can include many threads.
  • All threads of a process share
  • memory (program code and global data)
  • open file/socket descriptors
  • signal handlers and signal dispositions
  • working environment (current directory, user ID,
    etc.)

7
Thread-Specific Resources
  • Each thread has its own
  • Thread ID (integer)
  • Stack, Registers, Program Counter
  • errno (if not - errno would be useless!)
  • Threads within the same process can communicate
    using shared memory.
  • Must be done carefully!

8
Single and Multithreaded Processes
9
Benefits
  • Responsiveness
  • Resource Sharing
  • Economy
  • Utilization of MP Architectures

10
User Threads
  • Thread management done by user-level threads
    library
  • Three primary thread libraries
  • POSIX Pthreads
  • Win32 threads
  • Java threads

11
Kernel Threads
  • Supported by the Kernel
  • Examples
  • Windows XP/2000
  • Solaris
  • Linux
  • Tru64 UNIX
  • Mac OS X

12
Multithreading Models
  • Many-to-One
  • One-to-One
  • Many-to-Many

13
Many-to-One
  • Many user-level threads mapped to single kernel
    thread
  • Examples
  • Solaris Green Threads
  • GNU Portable Threads

14
Many-to-One Model
15
One-to-One
  • Each user-level thread maps to kernel thread
  • Examples
  • Windows NT/XP/2000
  • Linux
  • Solaris 9 and later

16
One-to-one Model
17
Many-to-Many Model
  • Allows many user level threads to be mapped to
    many kernel threads
  • Allows the operating system to create a
    sufficient number of kernel threads
  • Solaris prior to version 9
  • Windows NT/2000 with the ThreadFiber package

18
Many-to-Many Model
19
Two-level Model
  • Similar to MM, except that it allows a user
    thread to be bound to kernel thread
  • Examples
  • IRIX
  • HP-UX
  • Tru64 UNIX
  • Solaris 8 and earlier

20
Two-level Model
21
Posix Threads
We will focus on Posix Threads - most widely
supported threads programming API. Solaris - you
need to link with -lpthread On many systems
this also forces the compiler to link in
re-entrant libraries (instead of plain vanilla C
libraries).
22
Thread Creation
  • pthread_create(
  • pthread_t tid,
  • const pthread_attr_t attr,
  • void (func)(void ),
  • void arg)
  • func is the function to be called.
  • When func() returns the thread is terminated.

23
pthread_create()
  • The return value is 0 for OK.
  • positive error number on error.
  • Does not set errno !!!
  • Thread ID is returned in tid

24
Multithreaded C program using the Pthreads API (1)
  • include ltpthread.hgt
  • include ltstdio.hgt
  • int sum / this data is shared by the thread(s)
    /
  • void runner( void param ) / the thread /
  • int main( int argc, char argv )
  • pthread_t tid / the thread identifier /
  • pthread_attr_t attr / set of thread
    attributes /
  • if( argc ! 2 )
  • fprintf( stderr, "usage a.out ltinteger
    valuegt\n" )
  • return -1
  • if( atoi(argv1) lt 0 )
  • fprintf( stderr, "d must be gt 0\n", atoi(
    argv1 ) )
  • return -1

25
Multithreaded C program using the Pthreads API (2)
  • / The thread will begin control in this function
    /
  • void runner( void param )
  • int i, upper atoi( param )
  • sum 0
  • for( i 1 i lt upper i )
  • sum i
  • pthread_exit( 0 )

26
Java program for the summation of a non-negative
integer (1)
  • class Sum
  • private int sum
  • public int getSum()
  • return sum
  • public void setSum( int sum )
  • this.sum sum
  • class Summation implements Runnable
  • private int upper
  • private Sum sumValue
  • public Summation( int upper, Sum sumValue )
  • this.upper upper

27
Java program for the summation of a non-negative
integer (2)
  • public class Driver
  • public static void main( String args )
  • if( args.length gt 0 )
  • if( Integer.parseInt( args0 ) lt 0 )
  • System.err.println( args0 " must be gt
    0." )
  • else
  • // create the object to be shared
  • Sum sumObject new Sum()
  • int upper Integer.parseInt( args0 )
  • Thread thrd new Thread( new Summation(
    upper, sumObject ) )
  • thrd.start()
  • try
  • thrd.join()
  • System.out.println( "The sum of " upper
    " is " sumObject.getSum() )
  • catch ( InterruptedException ie )

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

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

30
Thread Cancellation
  • Terminating a thread before it has finished
  • Two general approaches
  • Asynchronous cancellation terminates the target
    thread immediately
  • Deferred cancellation allows the target thread to
    periodically check if it should be cancelled

31
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
  • 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 threa to receive all signals
    for the process

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

33
Thread Specific Data
  • Allows each thread to have its own copy of data
  • Useful when you do not have control over the
    thread creation process (i.e., when using a
    thread pool)

34
Scheduler Activations
  • Both MM and Two-level models require
    communication to maintain the appropriate number
    of kernel threads allocated to the application
  • Scheduler activations provide upcalls - a
    communication mechanism from the kernel to the
    thread library
  • This communication allows an application to
    maintain the correct number kernel threads

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

36
Threads Support in Solaris 2
  • Solaris 2 is a version of UNIX with support for
    threads at the kernel and user levels, symmetric
    multiprocessing, and real-time scheduling
  • LWP intermediate level between user-level
    threads and kernel-level threads.
  • Resource needs of thread types
  • Kernel thread small data structure and a stack
    thread switching does not require changing memory
    access information relatively fast.
  • LWP PCB with register data, accounting and
    memory information switching between LWPs is
    relatively slow
  • User-level thread only need stack and program
    counter no kernel involvement means fast
    switching. Kernel only sees the LWPs that support
    user-level threads.

37
Solaris 2 Threads
38
Solaris Process
39
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
  • The primary data structures of a thread include
  • ETHREAD (executive thread block)
  • KTHREAD (kernel thread block)
  • TEB (thread environment block)

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

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

42
Java Thread States
43
End of Chapter 4
Write a Comment
User Comments (0)
About PowerShow.com