Tutorial4 Threads

3,195 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,195
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
361
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tutorial4 Threads

  1. 1. Threads
  2. 2. Process <ul><li>Processes contain information about program resources and program execution state, including: </li></ul><ul><ul><li>Process ID, process group ID, user ID, and group ID </li></ul></ul><ul><ul><li>Environment </li></ul></ul><ul><ul><li>Working directory. </li></ul></ul><ul><ul><li>Program instructions </li></ul></ul><ul><ul><li>Registers </li></ul></ul><ul><ul><li>Stack </li></ul></ul><ul><ul><li>Heap </li></ul></ul><ul><ul><li>File descriptors </li></ul></ul><ul><ul><li>Signal actions </li></ul></ul><ul><ul><li>Shared libraries </li></ul></ul><ul><ul><li>Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory). </li></ul></ul>
  3. 3. UNIX Process
  4. 4. Threads <ul><li>Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code. </li></ul>
  5. 5. Cont. <ul><li>This independent flow of control is accomplished because a thread maintains its own: </li></ul><ul><ul><li>Stack pointer </li></ul></ul><ul><ul><li>Registers </li></ul></ul><ul><ul><li>Scheduling properties (such as policy or priority) </li></ul></ul><ul><ul><li>Set of pending and blocked signals </li></ul></ul><ul><ul><li>Thread specific data (thread ID) </li></ul></ul>
  6. 6. UNIX Thread
  7. 7. UNIX Threads <ul><li>In the UNIX environment a thread: </li></ul><ul><ul><li>Exists within a process and uses the process resources </li></ul></ul><ul><ul><li>Has its own independent flow of control as long as its parent process exists and the OS supports it </li></ul></ul><ul><ul><li>Duplicates only the essential resources it needs to be independently schedulable </li></ul></ul><ul><ul><li>May share the process resources with other threads that act equally independently (and dependently) </li></ul></ul><ul><ul><li>Dies if the parent process dies - or something similar </li></ul></ul><ul><ul><li>Is &quot;lightweight&quot; because most of the overhead has already been accomplished through the creation of its process. </li></ul></ul>
  8. 8. Pthreads <ul><li>A thread library that provides the programmer an API for creating and managing threads </li></ul><ul><li>Pthreads refers to the POSIX standards defining an API for thread creation and synchronization </li></ul>
  9. 9. Pthreads <ul><li>Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library </li></ul>
  10. 10. Creating threads <ul><li>Initially, your main() program comprises a single, default thread. All other threads must be explicitly created by the programmer. </li></ul>
  11. 11. Creating threads (cont.) <ul><li>int pthread_create (pthread_t * thread , const pthread_attr_t * attr , void *(* start_routine ) (void*), void * arg ); </li></ul><ul><li>Description: </li></ul><ul><li>pthread_create() creates a new thread and makes it executable. This routine can be called any number of times from anywhere within your code. </li></ul>
  12. 12. Creating threads (cont.) <ul><li>pthread_create() arguments: </li></ul><ul><ul><li>thread: An opaque, unique identifier for the new thread returned by the subroutine. </li></ul></ul><ul><ul><li>attr: An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values. </li></ul></ul><ul><ul><li>start_routine: the C routine that the thread will execute once it is created. </li></ul></ul><ul><ul><li>arg: A single argument that may be passed to start_routine . It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed. </li></ul></ul>
  13. 13. Cont. <ul><li>Return value: </li></ul><ul><ul><li>If successful, the pthread_create() routine shall return zero </li></ul></ul><ul><ul><li>other- wise, an error number shall be returned to indicate the error. </li></ul></ul>
  14. 14. Thread attributes <ul><li>Thread attributes include (define members of the struct pthread_attr_t): </li></ul><ul><ul><li>detached state </li></ul></ul><ul><ul><li>scheduling policy </li></ul></ul><ul><ul><li>scheduling parameter </li></ul></ul><ul><ul><li>inheritsched attribute </li></ul></ul><ul><ul><li>Scope </li></ul></ul><ul><ul><li>guard size </li></ul></ul><ul><ul><li>stack address </li></ul></ul><ul><ul><li>stack size </li></ul></ul>
  15. 15. Scheduling threads <ul><li>After a thread has been created, how do you know when it will be scheduled to run by the operating system? </li></ul><ul><ul><li>It is up to the implementation and/or operating system to decide where and when threads will execute. </li></ul></ul>
  16. 16. Terminating threads <ul><li>pthread_exit() is used to explicitly exit a thread. </li></ul><ul><li>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. </li></ul>
  17. 17. Joining Threads <ul><li>int pthread_join (pthread_t thread, void **value_ptr); </li></ul><ul><li>Description: </li></ul><ul><li>The pthread_join() function shall suspend execution of the calling thread until the target thread terminates. </li></ul><ul><li>Return value: </li></ul><ul><ul><li>If successful, the pthread_join() function shall return zero; otherwise, an error number shall be returned to indicate the error. </li></ul></ul>
  18. 18. Joining Threads (Cont.)
  19. 19. Example -1-
  20. 20. <ul><li>#include <pthread.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>#define NUM_THREADS 5 </li></ul><ul><li>void *PrintHello(void *threadid) </li></ul><ul><li>{ </li></ul><ul><li> int tid; </li></ul><ul><li> tid = (int)threadid; </li></ul><ul><li>printf(&quot;Hello World! It's me, thread #%d! &quot;, tid); </li></ul><ul><li>pthread_exit(NULL); </li></ul><ul><li>} </li></ul><ul><li>int main (int argc, char *argv[]) </li></ul><ul><li>{ </li></ul><ul><li>pthread_t threads[NUM_THREADS]; </li></ul><ul><li> int rc, t; </li></ul><ul><li> for(t=0; t<NUM_THREADS; t++){ </li></ul><ul><li> printf(&quot;In main: creating thread %d &quot;, t); </li></ul><ul><li> rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); </li></ul><ul><li> if (rc){ </li></ul><ul><li>printf(&quot;ERROR; return code from pthread_create() is %d &quot;, rc); </li></ul><ul><li>exit(-1); </li></ul><ul><li>} </li></ul><ul><li> } </li></ul><ul><li>pthread_exit(NULL);} </li></ul>
  21. 21. Code Explanation
  22. 22. <ul><ul><ul><li>Defines an array of threads (note: threads are not yet created!) of opaque type pthread_t </li></ul></ul></ul><ul><ul><ul><li>In for loop: </li></ul></ul></ul><ul><ul><ul><ul><li>Threads will be created using pthread_create . The routine PrintHello() is the starting routine for all newly created threads </li></ul></ul></ul></ul><ul><ul><ul><ul><li>The returned value for each thread creation will be checked and prints and error message if the returned value is not zero </li></ul></ul></ul></ul><ul><li>int main (int argc, char *argv[]) </li></ul><ul><li>{ </li></ul><ul><li>pthread_t threads[NUM_THREADS]; </li></ul><ul><li>int rc, t; </li></ul><ul><li>for(t=0; t<NUM_THREADS; t++){ </li></ul><ul><li>printf(&quot;In main: creating thread %d &quot;, t); </li></ul><ul><li>rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); </li></ul><ul><li>if (rc){ </li></ul><ul><li>printf(&quot;ERROR; return code from pthread_create() is %d &quot;, rc); </li></ul><ul><li>exit(-1); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 23. Cont. <ul><li>The routine PrintHello will simply print, </li></ul><ul><li>“ Hello World! It’s me, thread# threadnum </li></ul><ul><li>It will then terminate the thread using pthread_exit. </li></ul><ul><li>void *PrintHello(void *threadid) </li></ul><ul><li>{ </li></ul><ul><li>int tid; </li></ul><ul><li>tid = (int)threadid; </li></ul><ul><li>printf(&quot;Hello World! It's me, thread #%d! &quot;, tid); </li></ul><ul><li>pthread_exit(NULL); </li></ul><ul><li>} </li></ul>
  24. 24. Possible Outputs <ul><li>In main: creating thread 0 </li></ul><ul><li>In main: creating thread 1 </li></ul><ul><li>Hello World! It's me, thread#0! </li></ul><ul><li>In main: creating thread 2 </li></ul><ul><li>Hello World! It's me, thread #1! </li></ul><ul><li>Hello World! It's me, thread #2! </li></ul><ul><li>In main: creating thread 3 </li></ul><ul><li>In main: creating thread 4 </li></ul><ul><li>Hello World! It's me, thread #3! </li></ul><ul><li>Hello World! It's me, thread #4! </li></ul><ul><li>In main: creating thread 0 </li></ul><ul><li>In main: creating thread 1 </li></ul><ul><li>In main: creating thread 2 </li></ul><ul><li>In main: creating thread 3 </li></ul><ul><li>In main: creating thread 4 </li></ul><ul><li>Hello World! It's me, thread#0! </li></ul><ul><li>Hello World! It's me, thread #1! </li></ul><ul><li>Hello World! It's me, thread #2! </li></ul><ul><li>Hello World! It's me, thread #3! </li></ul><ul><li>Hello World! It's me, thread #4! </li></ul>
  25. 25. Example -2-
  26. 26. <ul><li>#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *print_message_function( void *ptr ); </li></ul><ul><li>main() { pthread_t thread1, thread2; char *message1 = &quot;Thread 1&quot;; char *message2 = &quot;Thread 2&quot;; int iret1, iret2; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); pthread_join( thread2, NULL); </li></ul><ul><li>printf(&quot;Thread 1 returns: %d &quot;,iret1); printf(&quot;Thread 2 returns: %d &quot;,iret2); exit(0); } </li></ul><ul><li>void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf(&quot;%s &quot;, message); } </li></ul>
  27. 27. Code Explanation
  28. 28. <ul><ul><ul><li>Defines an array of threads (note: threads are not yet created!) of opaque type pthread_t </li></ul></ul></ul><ul><ul><ul><li>Threads will be created using pthread_create() . The routine print_message_function() is the starting routine for the two newly created routines. Notice that an argument was sent to print_message_function() as an argument to pthread_create() </li></ul></ul></ul><ul><ul><ul><li>The two threads newly created will be joined using pthread_join() </li></ul></ul></ul><ul><li>main() { pthread_t thread1, thread2; char *message1 = &quot;Thread 1&quot;; char *message2 = &quot;Thread 2&quot;; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); pthread_join( thread1, NULL); pthread_join( thread2, NULL); </li></ul><ul><li>printf(&quot;Thread 1 returns: %d &quot;,iret1); printf(&quot;Thread 2 returns: %d &quot;,iret2); exit(0); } </li></ul>
  29. 29. Cont. <ul><li>The routine PrintHello will simply print, </li></ul><ul><li>“ Hello World! It’s me, thread# threadnum </li></ul><ul><li>It will then terminate the thread using pthread_exit. </li></ul><ul><li>void * print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf &quot;, message); } (&quot;%s </li></ul>
  30. 30. Output <ul><li>Thread 1 </li></ul><ul><li>Thread 2 </li></ul><ul><li>Thread 1 returns : 0 </li></ul><ul><li>Thread 2 returns : 0 </li></ul>
  31. 31. Example -3- Example 1 while using pthread_join()
  32. 32. <ul><li>#include <pthread.h> </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>#define NUM_THREADS 5 </li></ul><ul><li>void *PrintHello(void *threadid) </li></ul><ul><li>{ </li></ul><ul><li> int tid; </li></ul><ul><li>tid = (int)threadid; </li></ul><ul><li> printf(&quot;Hello World! It's me, thread #%d! &quot;, tid); </li></ul><ul><li> pthread_exit(NULL); </li></ul><ul><li>} </li></ul><ul><li>int main (int argc, char *argv[]) </li></ul><ul><li>{ </li></ul><ul><li> pthread_t threads[NUM_THREADS]; </li></ul><ul><li> int rc, t; </li></ul><ul><li>for(t=0; t<NUM_THREADS; t++){ </li></ul><ul><li> printf(&quot;In main: creating thread %d &quot;, t); </li></ul><ul><li> rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); </li></ul><ul><li>if (rc){ </li></ul><ul><li> printf(&quot;ERROR; return code from pthread_create() is %d &quot;, rc); </li></ul><ul><li> exit(-1); </li></ul><ul><li>} </li></ul><ul><li>pthread_join(threads[t], NULL) ;} </li></ul><ul><li>pthread_exit(NULL);} </li></ul>
  33. 33. Possible Outputs <ul><li>In main: creating thread 0 </li></ul><ul><li>Hello World! It's me, thread#0! </li></ul><ul><li>In main: creating thread 1 </li></ul><ul><li>Hello World! It's me, thread #1! </li></ul><ul><li>In main: creating thread 2 </li></ul><ul><li>Hello World! It's me, thread #2! </li></ul><ul><li>In main: creating thread 3 </li></ul><ul><li>Hello World! It's me, thread #3! </li></ul><ul><li>In main: creating thread 4 </li></ul><ul><li>Hello World! It's me, thread #4! </li></ul>
  34. 34. Example -4-
  35. 35. <ul><li>#include <pthread.h> #include <stdlib.h> #include <stdio.h> void *thread_func( void *vptr_args ); int main( void ){ int i, j; pthread_t thread; pthread_create( &thread, NULL, &thread_func, NULL ); for( j= 0; j < 6; ++j ){ fprintf( stdout, &quot;a &quot; ); for( i= 99999999; i; --i ); /* use some CPU time */ } pthread_join( thread, NULL ); exit( EXIT_SUCCESS ); } void *thread_func( void *vptr_args ){ int i, j; for( j= 0; j < 6; ++j ){ fprintf( stderr, &quot; b &quot; ); for( i= 99999999; i; --i ); /* use some CPU time */ } return NULL; </li></ul><ul><li>} </li></ul>
  36. 36. Output <ul><li>a </li></ul><ul><li>b </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>b </li></ul><ul><li>a </li></ul><ul><li>b </li></ul><ul><li>a </li></ul><ul><li>b </li></ul><ul><li>b </li></ul><ul><li>a </li></ul><ul><li>b </li></ul><ul><li>What happens if we change the position of pthread_join() in the code? </li></ul>
  37. 37. <ul><li>#include <pthread.h> #include <stdlib.h> #include <stdio.h> void *thread_func( void *vptr_args ); int main( void ){ int i, j; pthread_t thread; pthread_create( &thread, NULL, &thread_func, NULL ); pthread_join( thread, NULL ); for( j= 0; j < 6; ++j ){ fprintf( stdout, &quot;a &quot; ); for( i= 99999999; i; --i ); /* use some CPU time */ } /* Previous position in code </li></ul><ul><li>pthread_join( thread, NULL ); */ exit( EXIT_SUCCESS ); } void *thread_func( void *vptr_args ){ int i, j; for( j= 0; j < 6; ++j ){ fprintf( stderr, &quot; b &quot; ); for( i= 99999999; i; --i ); /* use some CPU time */ } return NULL; </li></ul><ul><li>} </li></ul>
  38. 38. Output <ul><li>b </li></ul><ul><li>b </li></ul><ul><li>b </li></ul><ul><li>b </li></ul><ul><li>b </li></ul><ul><li>b </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>a </li></ul><ul><li>What happened? </li></ul>

×