Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shared Memory Programming with Pthreads

Similar presentations


Presentation on theme: "Shared Memory Programming with Pthreads"— Presentation transcript:

1 Shared Memory Programming with Pthreads

2 The University of Adelaide, School of Computer Science
27 August 2019 Outline # Chapter Subtitle Shared memory programming: Overview POSIX pthreads pthread_create() pthread_exit() pthread_join() pthread_cancel() Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 2 — Instructions: Language of the Computer

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

4 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

5 Logical View of Threads
Threads are created within a process A process Process hierarchy T2 T4 T1 P1 shared code, data and kernel context sh sh sh T3 T5 foo

6 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: Concurrent: A & B, A&C Sequential: B & C Thread A Thread B Thread C Time

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

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

9 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 Example: think of a thread as a subroutine that gets called on on processor, executed on another Like concurrent programming on a uniprocessor (as studied in CS162). Pn P1 P0 s s = ... Shared memory i: 2 i: 5 Private memory i: 8 CS267 Lecture 2

10 Shared Memory Programming
Several Thread Libraries/Systems Pthreads is the POSIX Standard Relatively low level Portable but possibly slow; relatively heavyweight OpenMP standard for application level programming Support for scientific programming on shared memory Java Threads TBB: Thread Building Blocks Intel CILK: Language of the C “ilk” Lightweight threads embedded into C POSIX = Portable Operating System Interface Slow if needs to go to OS for every thread operation Java threads built on POSIX threads: Are objects (like other objects in Java) (1) create Java thread object (2) send it a message saying “run” etc CS267 Lecture 2

11 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 thread CS267 Lecture 2

12 What are Pthreads? POSIX Threads, or Pthreads, is a POSIX standard for threads. The latest version is known as IEEE Std , 2004 Edition. Implementations of the API are available on many Unix-like POSIX systems such as FreeBSD, NetBSD, GNU/Linux, Mac OS X and Solaris, Microsoft Windows implementations also exist. For example, the pthreads-w32 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

13 Creation of Unix processes vs. Pthreads

14 The Pthread API Pthreads API can be grouped into four:
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. 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. 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.  Synchronization: Routines that manage read/write locks and barriers.

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

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

17 pthread_t* thread_p /* out */ ,
A closer look (2) int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* 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

18 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

19 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

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

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

22 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

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

24 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

25 Example: Creating one Simple Thread
$ gcc -o thread0 thread0.c -lpthread $ ./thread0 In main: creating thread This is worker_thread() // thread0.c #include <pthread.h> #include <stdio.h> #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 thread\n"); ret = pthread_create(&my_thread, NULL, &worker_thread, NULL); if(ret != 0) { printf("Error: pthread_create() failed\n"); exit(EXIT_FAILURE);

26 Example: Creating Multiple Threads
// thread01.c #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define N 5 void *worker_thread(void *arg) { printf("This is worker_thread #%ld\n", (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() failed\n"); exit(EXIT_FAILURE);

27 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.

28 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.

29 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("%d\n",i); // will print out 911

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

31

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


Download ppt "Shared Memory Programming with Pthreads"

Similar presentations


Ads by Google