Shared Memory Programming with Pthreads Shared memory programming

  • Slides: 32
Download presentation
Shared Memory Programming with Pthreads

Shared Memory Programming with Pthreads

 • Shared memory programming: Overview • POSIX pthreads § pthread_create() § pthread_exit() §

• Shared memory programming: Overview • POSIX pthreads § pthread_create() § pthread_exit() § pthread_join() § pthread_cancel() Copyright © 2010, Elsevier Inc. All rights Reserved # Chapter Subtitle Outline

Shared Memory Architecture Copyright © 2010, Elsevier Inc. All rights Reserved

Shared Memory Architecture Copyright © 2010, Elsevier Inc. All rights Reserved

Processes and Threads • A process is an instance of a running (or suspended)

Processes and Threads • A process is an instance of a running (or suspended) program. • Threads are analogous to a “light-weight” process. • In a shared memory program a single process may have multiple threads of control. Copyright © 2010, Elsevier Inc. All rights Reserved

Logical View of Threads • Threads are created within a process A process Process

Logical View of Threads • Threads are created within a process A process Process hierarchy T 2 T 4 T 1 shared code, data and kernel context P 1 sh T 5 T 3 foo sh sh

Concurrent Thread Execution • Two threads run concurrently if their logical flows overlap in

Concurrent Thread Execution • Two threads run concurrently if their logical flows overlap in time • Otherwise, they are sequential (we’ll see that processes have a similar rule) • Examples: Thread A Thread B Thread C § Concurrent: A & B, A&C § Sequential: Time B & C

Execution Flow on one-core or multi-core systems Concurrent execution on a single core system

Execution Flow on one-core or multi-core systems Concurrent execution on a single core system Parallel execution on a multi-core system

Benefits of multi-threading • Responsiveness • Resource Sharing § Shared memory • Economy •

Benefits of multi-threading • Responsiveness • Resource Sharing § Shared memory • Economy • Scalability § Explore multi-core CPUs

Thread Programming with Shared Memory • Program is a collection of threads of control.

Thread Programming with Shared Memory • Program is a collection of threads of control. § Can be created dynamically • Each thread has a set of private variables, e. g. , local stack variables • Also a set of shared variables, e. g. , static variables, shared common blocks, or global heap. § Threads communicate implicitly by writing and reading shared variables. § Threads coordinate by synchronizing on shared variables s i: 2 P 0 i: 5 P 1 Shared memory s =. . . Private memory i: 8 9 Pn

Shared Memory Programming Several Thread Libraries/Systems • Pthreads is the POSIX Standard § Relatively

Shared Memory Programming Several Thread Libraries/Systems • Pthreads is the POSIX Standard § Relatively low level § Portable but possibly slow; relatively heavyweight • Open. MP standard for application level programming § Support for scientific programming on shared memory § http: //www. open. MP. org • Java Threads • TBB: Thread Building Blocks § Intel § https: //www. threadingbuildingblocks. org/ • CILK: Language of the C “ilk” § Lightweight threads embedded into C § https: //en. wikipedia. org/wiki/Cilk 10

Overview of POSIX Threads • POSIX: Portable Operating System Interface for UNIX § Interface

Overview of POSIX Threads • POSIX: Portable Operating System Interface for UNIX § Interface to Operating System utilities • PThreads: The POSIX threading interface § System calls to create and synchronize threads § compile a c program with – $gcc sample. c –o sample -lpthread • PThreads contain support for § Creating parallelism and synchronization § No explicit support for communication, because: – shared memory is implicit; a pointer to shared data is passed to a 11 thread

What are Pthreads? • POSIX Threads, or Pthreads, is a POSIX standard for threads.

What are Pthreads? • POSIX Threads, or Pthreads, is a POSIX standard for threads. § The latest version is known as IEEE Std 1003. 1, 2004 Edition. • Implementations of the API are available on many Unix-like POSIX systems such as Free. BSD, Net. BSD, GNU/Linux, Mac OS X and Solaris, • Microsoft Windows implementations also exist. § For example, the pthreads-w 32 is available and supports a subset of the Pthread API for the Windows 32 -bit platform. • Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread. h header file. In GNU/Linux, the pthread functions are not included in the standard C library. § They are in libpthread. so, therefore, we should add -lpthread to link our program. § Tryout : $ locate libpthread. so

Creation of Unix processes vs. Pthreads

Creation of Unix processes vs. Pthreads

The Pthread API • Pthreads API can be grouped into four: 1. Thread management

The Pthread API • Pthreads API can be grouped into four: 1. Thread management – Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes such as joinable, scheduling etc. 2. Mutexes – Routines that deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes. 3. Condition variables – Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. 4. Synchronization: – Routines that manage read/write locks and barriers.

C function for starting a thread pthread. h pthread_t One object for each thread.

C function for starting a thread pthread. h pthread_t One object for each thread. int pthread_create ( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine ) ( void ) void* arg_p ) ; /* out */ , /* in */ Copyright © 2010, Elsevier Inc. All rights Reserved

A closer look (1) int pthread_create ( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine

A closer look (1) int pthread_create ( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine ) ( void ) void* arg_p ) ; /* out */ , /* in */ We won’t be using, so we just pass NULL. Allocate before calling. Copyright © 2010, Elsevier Inc. All rights Reserved

A closer look (2) int pthread_create ( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine

A closer look (2) int pthread_create ( pthread_t* thread_p const pthread_attr_t* attr_p void* (*start_routine ) ( void ) void* arg_p ) ; /* out */ , /* in */ Pointer to the argument that can be passed to the function start_routine. The function that the thread is to run. Copyright © 2010, Elsevier Inc. All rights Reserved

Function started by pthread_create • Prototype: void* thread_function ( void* args_p ) ; •

Function started by pthread_create • Prototype: void* thread_function ( void* args_p ) ; • void* can be cast to any pointer type in C. • So args_p can point to a list containing one or more values needed by thread_function. • Similarly, the return value of thread_function can point to a list of one or more values. Copyright © 2010, Elsevier Inc. All rights Reserved

Wait for Completion of Threads pthread_join(pthread_t *thread, void **result); § Wait for specified thread

Wait for Completion of Threads pthread_join(pthread_t *thread, void **result); § Wait for specified thread to finish. – Place exit value into *result. • We call the function pthread_join once for each thread. • A single call to pthread_join will wait for the thread associated with the pthread_t object to complete. Copyright © 2010, Elsevier Inc. All rights Reserved

Example of Pthreads #include <pthread. h> #include <stdio. h> void *Print. Hello(void * id){

Example of Pthreads #include <pthread. h> #include <stdio. h> void *Print. Hello(void * id){ printf(“Thread %d: Hello World!n", id); } void main (){ pthread_t thread 0, thread 1; pthread_create(&thread 0, NULL, Print. Hello, (void *) 0); pthread_create(&thread 1, NULL, Print. Hello, (void *) 1); }

Example of Pthreads with join #include <pthread. h> #include <stdio. h> void *Print. Hello(void

Example of Pthreads with join #include <pthread. h> #include <stdio. h> void *Print. Hello(void * id){ printf(“Hello from thread %dn", id); } void main (){ pthread_t thread 0, thread 1; pthread_create(&thread 0, NULL, Print. Hello, (void *) 0); pthread_create(&thread 1, NULL, Print. Hello, (void *) 1); pthread_join(thread 0, NULL); pthread_join(thread 1, NULL); }

Some More Pthread Functions • pthread_yield(); § Informs the scheduler that the thread is

Some More Pthread Functions • pthread_yield(); § Informs the scheduler that the thread is willing to yield • pthread_exit(void *value); § Exit thread and pass value to joining thread (if exists) Others: • pthread_t me; me = pthread_self(); § Allows a pthread to obtain its own identifier pthread_t thread; • Synchronizing access to shared variables § pthread_mutex_init, pthread_mutex_[un]lock § pthread_cond_init, pthread_cond_[timed]wait

Compiling a Pthread program gcc −g −Wall −o hello. c −lpthread Linking the Pthreads

Compiling a Pthread program gcc −g −Wall −o hello. c −lpthread Linking the Pthreads library Copyright © 2010, Elsevier Inc. All rights Reserved

Running a Pthreads program . /hello Hello from thread 1 Hello from thread 0

Running a Pthreads program . /hello Hello from thread 1 Hello from thread 0 . /hello Hello from thread 0 Hello from thread 1 Copyright © 2010, Elsevier Inc. All rights Reserved

Example: Creating one Simple Thread $ gcc -o thread 0. c -lpthread // thread

Example: Creating one Simple Thread $ gcc -o thread 0. c -lpthread // thread 0. c $. /thread 0 #include <pthread. h> In main: creating thread #include <stdio. h> This is worker_thread() #include <stdlib. h> void *worker_thread(void *arg) { printf("This is worker_thread()n"); pthread_exit(NULL); } int main() { pthread_t my_thread; int ret; printf("In main: creating threadn"); ret = pthread_create(&my_thread, NULL, &worker_thread, NULL); if(ret != 0) { printf("Error: pthread_create() failedn"); exit(EXIT_FAILURE); } pthread_exit(NULL); }

Example: Creating Multiple Threads // thread 01. c #include <pthread. h> #include <stdio. h>

Example: Creating Multiple Threads // thread 01. c #include <pthread. h> #include <stdio. h> #include <stdlib. h> #define N 5 void *worker_thread(void *arg) { printf("This is worker_thread #%ldn", (long)arg); pthread_exit(NULL); } int main() { pthread_t my_thread[N]; for(int i = 1; i <= N; i++) { int ret=pthread_create(&my_thread[i], NULL, &worker_thread, (void*)i); if(ret != 0) { printf("Error: pthread_create() failedn"); exit(EXIT_FAILURE); } } pthread_exit(NULL); }

Attributes of Threads • By default, a thread is created with certain attributes. •

Attributes of Threads • By default, a thread is created with certain attributes. • Some of these attributes can be changed by the programmer via the thread attribute object. • pthread_attr_init() and pthread_attr_destroy() are used to initialize/destroy the thread attribute object. • Other routines are then used to query/set specific attributes in the thread attribute object.

Terminating Threads • There are several ways in which a Pthread may be terminated:

Terminating Threads • There are several ways in which a Pthread may be terminated: § The thread returns from its starting routine – (the main routine for the initial thread). § The thread makes a call to the pthread_exit() subroutine. § The thread is canceled by another thread via the pthread_cancel() routine § The entire process is terminated due to a call to either the exec() or exit() subroutines.

Join • int pthread_join (pthread_t th, void **thread_return) § The pthread_join() subroutine blocks the

Join • int pthread_join (pthread_t th, void **thread_return) § The pthread_join() subroutine blocks the calling thread until the specified thread terminates. § The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit() as show here: § A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread. void *worker_thread(void *arg) { pthread_exit((void*)911); } int main() { int i; pthread_t my_thread; pthread_create(&my_thread; , NULL, worker_thread, NULL); pthread_join(my_thread, (void **)&i); printf("%dn", i); // will print out 911 }

Difference between Single and Multithreaded Processes Shared memory access for code/data Separate control flow

Difference between Single and Multithreaded Processes Shared memory access for code/data Separate control flow -> separate stack/registers

Synchronization & Critical Sections Next ? ? Copyright © 2010, Elsevier Inc. All rights

Synchronization & Critical Sections Next ? ? Copyright © 2010, Elsevier Inc. All rights Reserved