Shared Memory Programming with Pthreads

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
CS427 Multicore Architecture and Parallel Computing
PTHREADS These notes are from LLNL Pthreads Tutorial
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Parallel Programming with Threads CS 240A Tao Yang, 2013.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
The University of Adelaide, School of Computer Science
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
CS333 Intro to Operating Systems Jonathan Walpole.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 4 Shared Memory Programming with Pthreads An Introduction to Parallel Programming Peter Pacheco.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Shared Memory Programming with Pthreads T. Yang. UCSB CS240A. Spring 2016.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Chapter 4: Threads.
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
The University of Adelaide, School of Computer Science
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
POSIX Threads(pthreads)
Presentation transcript:

Shared Memory Programming with Pthreads

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

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

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 hierarchy T2 T4 T1 P1 shared code, data and kernel context sh sh sh T3 T5 foo

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

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 Scalability Explore multi-core CPUs

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

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 http://www.openMP.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 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

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

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

Creation of Unix processes vs. Pthreads

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.

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

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

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

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 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 *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);

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);

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 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 ./hello Hello from thread 0 Hello from thread 1 Copyright © 2010, Elsevier Inc. All rights Reserved

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);

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);

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: 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 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

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 Reserved