thread programming 김도형

Upload: iiiiiksh

Post on 30-May-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Thread Programming

    1/38

    Thread Programming

  • 8/14/2019 Thread Programming

    2/38

    2

    Table of Contents

    What is a Threads?

    Designing Threaded Programs

    Synchronizing Threads Managing Threads

  • 8/14/2019 Thread Programming

    3/38

    3

    What is a Thread?

    Relationship between a process and a thread.

    A process is created by the operating system. Processes containinformation about program resources and program executionstate, including: Process ID, process group ID, user ID, and group ID

    Environment Working directory.

    Program instructions

    Registers

    Stack

    Heap

    File descriptors

    Signal actions

    Shared libraries

    Inter-process communication tools (such as message queues, pipes,semaphores, or shared memory).

  • 8/14/2019 Thread Programming

    4/38

    4

    A Process

  • 8/14/2019 Thread Programming

    5/38

    5

    A Thread

    Threads use and exist within these process resources,yet are able to be scheduled by the operating systemand run as independent entities within a process.

    A thread can possess an independent flow of control

    and be schedulable because it maintains its own: Stack pointer

    Registers

    Scheduling properties (such as policy or priority)

    Set of pending and blocked signals

    Thread specific data.

  • 8/14/2019 Thread Programming

    6/38

    6

    Threads

  • 8/14/2019 Thread Programming

    7/387

    Threads

    A process can have multiple threads, all of whichshare the resources within a process and all ofwhich execute within the same address space.Within a multi-threaded program, there are at any

    time multiple points of execution. Because threads within the same process share

    resources: Changes made by one thread to shared system resources (such

    as closing a file) will be seen by all other threads. Two pointers having the same value point to the same data.

    Reading and writing to the same memory locations is possible,and therefore requires explicit synchronization by theprogrammer.

  • 8/14/2019 Thread Programming

    8/388

    User Level Thread

    Usually run on top of operating systemThe threads within the process are invisible to the

    kernel Visible only from within the process

    These threads compete among themselves for the resourcesallocated to a process

    Scheduled by a thread runtime system which is part of theprocess code

    Advantage Inexpensive, low overhead

    Need not create their own address space

    Switching between thread does not involve switching addressspace

  • 8/14/2019 Thread Programming

    9/389

    Kernel Level Thread

    Kernel is aware of each thread as a schedulableentity

    Threads compete for processor resources on a

    system wide basis

    Advantage Multiple processor

  • 8/14/2019 Thread Programming

    10/3810

    Hybrid Thread

    Task advantage of both user-level and kernel-levelthread

    User-level threads are mapped into the kernel-

    schedulable entities as the process is running to

    achieve parallelism

    Sun Solaris The user-level thread called thread

    Kernel schedulable entities are called lightweight processes

    User can specify that a particular thread have a dedicated

    lightweight process

  • 8/14/2019 Thread Programming

    11/3811

    Why Use Threads Over Processes?

    Creating a new process can be expensive Time

    A call into the operating system is needed

    The operating systems context-switching mechanism will be involved

    Memory The entire process must be replicated

    The cost of inter-process communication and synchronization of

    shared data May involve calls into the operation system kernel

    Threads can be created without replicating anentire process Creating a thread is done in user space rather than kernel

  • 8/14/2019 Thread Programming

    12/3812

    Benefits of Thread Programming

    Inter-thread communication is more efficient and in manycases, easier to use than inter-process communication.

    Threaded applications offer potential performance gains andpractical advantages over non-threaded applications in severalother ways:

    Overlapping CPU work with I/O: For example, a program may havesections where it is performing a long I/O operation. While one threadis waiting for an I/O system call to complete, CPU intensive work can beperformed by other threads.

    Priority/real-time scheduling: tasks which are more important can bescheduled to supersede or interrupt lower priority tasks.

    Asynchronous event handling: tasks which service events ofindeterminate frequency and duration can be interleaved. For example, aweb server can both transfer data from previous requests and managethe arrival of new requests.

    SMP machine perfromance

  • 8/14/2019 Thread Programming

    13/3813

    What are Pthreads?

    IEEE POSIX Section 1003.1c IEEE ( Institute of Electric and Electronic Engineering )

    POSIX ( Portable Operating System Interface )

    Pthread is a standardized model for dividing a program into

    subtasks whose execution can be interleaved or run in parallel Specifics are different for each implementation

    Mach Threads and NT Threads

    Pthread of Linux is kernel level thread Implemented by clone() systemcall

  • 8/14/2019 Thread Programming

    14/38

    14

    Process/Thread Creation Overhead

    Platform

    fork() pthread_create()

    real user sys real user sys

    IBM 332 MHz 604e4 CPUs/node512 MB Memory

    AIX 4.3

    92.4 2.7 105.3 8.7 4.9 3.9

    IBM 375 MHz POWER316 CPUs/node16 GB MemoryAIX 5.1

    173.6 13.9 172.1 9.6 3.8 6.7

    INTEL 2.2 GHz Xeon2 CPU/node2 GB MemoryRedHat Linux 7.3

    17.4 3.9 13.5 5.9 0.8 5.3

    fork_vs_thread.txt

    http://www.llnl.gov/computing/tutorials/pthreads/fork_vs_thread.txt
  • 8/14/2019 Thread Programming

    15/38

    15

    The Pthreads API

    The Pthreads API is defined in the ANSI/IEEE POSIX1003.1 - 1995 standard. The subroutines which comprise the Pthreads API can be

    informally grouped into three major classes:1. Thread management: The first class of functions work directly on

    threads - creating, detaching, joining, etc. They include functions toset/query thread attributes (joinable, scheduling etc.)

    2. Mutexes: The second class of functions deal with synchronization,called a "mutex", which is an abbreviation for "mutual exclusion". Mutexfunctions provide for creating, destroying, locking and unlockingmutexes. They are also supplemented by mutex attribute functions thatset or modify attributes associated with mutexes.

    3. Condition variables: The third class of functions address

    communications between threads that share a mutex. They are basedupon programmer specified conditions. This class includes functions tocreate, destroy, wait and signal based upon specified variable values.Functions to set/query condition variable attributes are also included.

  • 8/14/2019 Thread Programming

    16/38

    16

    The Pthreads API

    The Pthreads API contains over 60 subroutines.This tutorial will focus on a subset of these -specifically, those which are most likely to beimmediately useful to the beginning Pthreadsprogrammer.

    The pthread.h header file must be included in eachsource file using the Pthreads library.

    The current POSIX standard is defined only forthe C language.

  • 8/14/2019 Thread Programming

    17/38

    17

    Creating Threads

    Initially, your main() program comprises a single,default thread. All other threads must be explicitly

    created by the programmer.

    Routines:

    pthread_create (thread,attr,start_routine,arg)

    Creates a new thread and makes it executable. Once created,

    threads are peers, and may create other threads.

    Returns: the new thread ID via the threadargument. This ID should be

    checked to ensure that the thread was successfully created.

    http://www.llnl.gov/computing/tutorials/pthreads/man/pthread_create.htmlhttp://www.llnl.gov/computing/tutorials/pthreads/man/pthread_create.html
  • 8/14/2019 Thread Programming

    18/38

    18

    Creating Threads

    attr: is used to set thread attributes. You can specify a thread

    attributes object, or NULL for the default values. Threadattributes are discussed later.

    start_routine: is the C routine that the thread will execute once it is created.

    arg : An argument may be passed to start_routinevia arg. It must be

    passed by reference as a pointer cast of type void.

    The maximum number of threads that may becreated by a process is implementation dependent.

  • 8/14/2019 Thread Programming

    19/38

    19

    Terminating Thread Execution

    There are several ways in which a Pthread may beterminated: The thread returns from its starting function

    The thread makes a call to the pthread_exit function

    The thread is canceled by another thread via thepthread_cancel function

    The entire process is terminated due to a call to either the

    exec or exit

    Routines: pthread_exit (status)

    http://www.llnl.gov/computing/tutorials/pthreads/man/pthread_exit.htmlhttp://www.llnl.gov/computing/tutorials/pthreads/man/pthread_exit.html
  • 8/14/2019 Thread Programming

    20/38

    20

    Terminating Thread Execution

    Routines: pthread_exit (status) If main() finishes before the threads it has created, and exits

    with pthread_exit(), the other threads will continue to execute.

    Otherwise, they will be automatically terminated when main()

    finishes.

    Status: The programmer may optionally specify a termination

    status, which is stored as a void pointer for any thread that

    may join the calling thread.

    Cleanup: the pthread_exit() routine does not close files; any

    files opened inside the thread will remain open after the threadis terminated.

    Recommendation: Use pthread_exit() to exit from all

    threads...especially main().

    http://www.llnl.gov/computing/tutorials/pthreads/man/pthread_exit.htmlhttp://www.llnl.gov/computing/tutorials/pthreads/man/pthread_exit.html
  • 8/14/2019 Thread Programming

    21/38

    21

    Pthread Creation and Termination

    #include

    #define NUM_THREADS 5

    void *PrintHello(void *threadid){ printf("\n%d: Hello World!\n", threadid);

    pthread_exit(NULL);}

    int main (int argc, char *argv[]){pthread_t threads[NUM_THREADS];int rc, t;

    for(t=0;t < NUM_THREADS;t++) {printf("Creating thread %d\n", t);rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);if (rc) {

    printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1);}

    }pthread_exit(NULL);

    }

  • 8/14/2019 Thread Programming

    22/38

    22

    Passing Arguments to Threads

    The pthread_create() routine permits theprogrammer to pass one argument to the thread

    start routine. For cases where multiple arguments

    must be passed, this limitation is easily overcome

    by creating a structure which contains all of thearguments, and then passing a pointer to that

    structure in the pthread_create() routine.

    All arguments must be passed by reference andcast to (void *).

  • 8/14/2019 Thread Programming

    23/38

    Designing Threaded Programs

  • 8/14/2019 Thread Programming

    24/38

    24

    Designing Threaded Programs

    In order for a program to take advantage ofPthreads, it must be able to be organized into

    discrete, independent tasks which can execute

    concurrently.

  • 8/14/2019 Thread Programming

    25/38

    25

    Designing Threaded Programs

  • 8/14/2019 Thread Programming

    26/38

    26

    Designing Threaded Programs

    Splitting CPU-based (e.g., computation)

    I/O-based (e.g., read data block from a file on disk)

    Potential parallelism

    The property that statements can be executed in any orderwithout changing the result

  • 8/14/2019 Thread Programming

    27/38

    27

    Potential Parallelism

    Reasons for exploiting potential parallelism Obvious : make a program run faster on a multiprocessor

    Additional reasons Overlapping I/O

    Parallel executing of I/O-bound and CPU-bound jobs

    E.g., word processor -> printing & editing

    Asynchronous events

    If one more tasks is subject to the indeterminate occurrence of events

    of unknown duration and unknown frequency, it may be more efficient to

    allow other tasks to proceed while the task subject to asynchronous

    events is in some unknown state of completion. E.g., network-based server

  • 8/14/2019 Thread Programming

    28/38

    28

    Designing Threaded Programs

    Several common models for threaded programsexist: Manager/worker: a single thread, the managerassigns work to

    other threads, the workers. Typically, the manager handles all

    input and parcels out work to the other tasks. At least twoforms of the manager/worker model are common: static worker

    pool and dynamic worker pool.

    Pipeline: a task is broken into a series of suboperations, each of

    which is handled in series, but concurrently, by a different

    thread. An automobile assembly line best describes this model. Peer: similar to the manager/worker model, but after the main

    thread creates other threads, it participates in the work.

  • 8/14/2019 Thread Programming

    29/38

    29

    Manager/Worker Model

    taskX

    taskY

    taskZ

    WorkersProgram

    Files

    Disks

    Databases

    SpecialDevices

    Input (stream) M a i n ( )Boss

  • 8/14/2019 Thread Programming

    30/38

    30

    Manager/Worker Model

    Example 1 (manager/worker model program)Main ( void ) /* the manager */{

    forever {

    get a request;

    switch request

    case X : pthread_create ( taskX);case Y : pthread_create ( taskY);

    ..

    }

    }

    taskX () /* Workers processing requests of type X */

    {

    perform the task, synchronize as needed if accessing shared resources;

    done;

    }

  • 8/14/2019 Thread Programming

    31/38

    31

    Manager/Worker Model

    Thread pool A variant of the Manager/worker model The manager could save some run-time overhead by creating all worker threads up

    front

    example 2 (manager/worker model with a thread pool )

    Main(void) /* manager */

    {for the number of workers

    pthread_create( .pool_base );

    forever {

    get a request;

    place request in work queue;

    signal sleeping threads that work is available;

    }

    }

  • 8/14/2019 Thread Programming

    32/38

    32

    Manager/Worker Model

    Example 2 (contd)pool_base() /* All workers */

    {

    forever {

    sleep until awoken by boss;

    dequeue a work request;

    switch {

    case request X : taskX();

    case request Y : taskY();

    .

    }

    }

    }

  • 8/14/2019 Thread Programming

    33/38

    33

    Pipeline Model

    Files

    Disks

    Databases

    Special Devices

    Stage1 stage2 stage3Input (stream)

    Filter ThreadsProgram

    ResourcesFiles

    Disks

    Databases

    Special Devices

    Files

    Disks

    Databases

    Special Devices

  • 8/14/2019 Thread Programming

    34/38

    34

    Pipeline Model

    Example (pipeline model program)Main (void)

    {

    pthread_create( stage1 );

    pthread_create( stage2);

    wait for all pipeline threads to finish;do any clean up;

    }

    Stage1 ()

    {

    forever {

    get next input for the program;do stage1 processing of the input;

    pass result to next thread in pipeline;

    }

    }

  • 8/14/2019 Thread Programming

    35/38

    35

    Pipeline Model

    Example (contd)Stage2 ()

    {

    forever {

    get input from previous thread in pipeline;

    do stage2 processing of the input;

    pass result to next thread in pipeline;}

    }

    stageN ()

    {

    forever {

    get input from previous thread in pipeline;do stageN processing of the input;

    pass result to program output;

    }

    }

  • 8/14/2019 Thread Programming

    36/38

    36

    Peer Model

    taskX

    taskY

    taskZ

    WorkersProgram

    Files

    Disks

    Databases

    SpecialDevices

    Input

    (static)

    ------------

    ------------------------------------

    ------------------------

    ------------------------

    ------------------------

    ------------

  • 8/14/2019 Thread Programming

    37/38

    37

    Peer Model

    Example (peer model program)

    Main (void)

    {

    pthread_create ( thread1 task1 );

    pthread_create ( thread2 task2 );

    signal all workers to start;wait for all workers to finish;

    do any clean up;

    }

    task1 ()

    {

    wait for start;

    perform task, synchronize as needed if accessing shared resources;

    done;

    }

  • 8/14/2019 Thread Programming

    38/38

    To be continue