Threads in Linux

download Threads in Linux

of 16

Transcript of Threads in Linux

  • 8/10/2019 Threads in Linux

    1/16

    Threads

  • 8/10/2019 Threads in Linux

    2/16

    Multiple strands of execution ina single program are calledthreads.

    A more precise defnition is thata thread is a sequence of controlwithin a process.

    threads, which allow a singleprocess to multitask.

  • 8/10/2019 Threads in Linux

    3/16

    When a process executes a forkcall, a new copy of the process is

    created with its own variables andits own P!.

    When we create a new threadina process, in contrast, the newthread of execution gets its ownstack "and hence local variables#but shares global variables, $ledescriptors, signal handlers withthe rocess that created it.

  • 8/10/2019 Threads in Linux

    4/16

    Advantages

    % &ometimes it is very useful to make a

    program appear to do two things atonce. The classic example is to performa real'time word count on a documentwhile still editing the text. (ne thread

    can manage the user)s input andperform editing.

    % The performance of an applicationthat mixes input, calculation, andoutput may be improvedby running these as three separate

    threads. While the input or output

  • 8/10/2019 Threads in Linux

    5/16

    Advantages

    % better utili*ation of thehardware resources available.

    % n general, switchingbetween threads requires the

    operating system to do muchless work than switchingbetween processes.

  • 8/10/2019 Threads in Linux

    6/16

    Drawbacks

    % Writing multithreadedprograms requires very carefuldesign.

    % !ebugging a multithreadedprogram is much, much harderthan debugging a single'threaded one

  • 8/10/2019 Threads in Linux

    7/16

    pthread_create creates anew thread

    +include pthread.h-

    int pthreadcreate"pthreadt/thread, pthreadattrt /attr,

    void/"/startroutine#"void /#, void/arg#0

  • 8/10/2019 Threads in Linux

    8/16

    #include void pthread_exit(void*retval!

    pthread1oin is the threadequivalent of wait that processesuse to collect child processes.

    This function is declared asfollows2#include

    int pthread_"oin(pthread_t th**

  • 8/10/2019 Threads in Linux

    9/16

    +include stdio.h-+include unistd.h-+include stdlib.h-

    +include string.h-+include pthread.h-void /threadfunction"void /arg#0char message34 5 67ello World80int main"# 9int res0pthreadt athread0void /threadresult0res 5 pthreadcreate":athread, ;5 ?# 9perror"6Thread creation failed8#0exit"@ATBC=

  • 8/10/2019 Threads in Linux

    10/16

    printf"6Waiting for thread to $nish...Fn8#0res 5 pthread1oin"athread, :threadresult#0if "res >5 ?# 9

    perror"6Thread 1oin failed8#0exit"@ATBC=

  • 8/10/2019 Threads in Linux

    11/16

    cc -D_REENTRANT thread1.c othread1 lpthread

    K .$thread%Waiting for thread to $nish...

    threadfunction is running. Crgumentwas 7ello WorldThread 1oined, it returned Thank youfor the HP< time

    Message is now Jye>

  • 8/10/2019 Threads in Linux

    12/16

    &imultaneous @xecution of Two Threadsint runnow 5 L0int printcountL 5 ?0while"printcountL N?# 9

    if "runnow 55 L# 9printf"6L8#0runnow 5 N0Eelse 9

    sleep"L#0EEint printcountN 5 ?0while"printcountN N?# 9if "runnow 55 N# 9

    printf"6N8#0runnow 5 L0Eelse 9sleep"L#0EE

  • 8/10/2019 Threads in Linux

    13/16

    K cc &D_')'A) thread+.c &othread+ &lpthread

    K .$thread+

    LNLNLNLNLNLNLNLNLNLNWaiting for thread to $nish...

    Thread 1oined

  • 8/10/2019 Threads in Linux

    14/16

    ,-nchroniation

    Synchronization with Semaphores

    Synchronization with Mutexes

  • 8/10/2019 Threads in Linux

    15/16

    Semaphores

    C semaphore is nothing but a term

    used in

  • 8/10/2019 Threads in Linux

    16/16

    Mutexes

    Mutexes are typically used toserialise access to a section of re'entrant code that cannot be executedconcurrently by more than onethread.

    C mutex ob1ect only allows one

    thread into a controlled section,forcing other threads which attemptto gain access to that section to wait

    until the $rst thread has exited from8