Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.

Slides:



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

Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Pthread II. Outline Join Mutex Variables Condition Variables.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
POSIX Threads Programming The following is extracted from a tutorial by Blaise Barney at Livermore Computing Blaise Barney (Lawrence Livermore National.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
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.
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Programming with POSIX* Threads Intel Software College.
Linux Programming –Threads CS Threads Review Threads in the same address space –share everything in the address space –lighter than process –no.
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.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
CS450/550 Pthreads.1 Adapted from llnl UC. Colorado Springs CS450/550 Operating Systems Lecture 2A POSIX Threads Programming Palden Lama Department of.
POSIX Threads HUJI Spring 2011.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
Threads A thread is an alternative model of program execution
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
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.
PRINCIPLES OF OPERATING SYSTEMS Tutorial-4: Multi-process and Multi-threaded Programming CPSC 457, Spring 2015 May 28/29, 2015 Department of Computer Science,
POSIX THREADS. What is a thread? Multiple stands of execution in a single program are called threads. In other words, a thread is a sequence of control.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
Tutorial 4. In this tutorial session we’ll see Threads.
Lesson One – Creating a thread
PThreads.
Principles of Operating Systems Lecture 11
Threads Threads.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
CSE 333 – Section 9 Threads.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Multithreading Tutorial
Multithreading Tutorial
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Pthreads & Concurrency

Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney

 History  Advantages  Content

History  Different versions of threads each hardware vendor created own version  IEEE POSIX c Standard  C programming types and procedure calls

Advantages  Potential program performance gains Platform fork()pthread_create() realusersysrealusersys AMD 2.4 GHz Opteron IBM 1.9 GHz POWER5 p IBM 1.5 GHz POWER INTEL 2.4 GHz Xeon

Advantages  More efficient inter-thread communication  Threaded applications Overlap CPU work and I/O Priority/Real-time scheduling Asynchronous event handling

The Pthreads API  Thread management Create, detach, join Set/query thread attributes  Mutex Mutual Exclusion  Condition variables Address communications between threads  Non-mutex synchronization Read/write locks Barriers

The Pthreads API  Naming conventions Routine PrefixFunctional Group pthread_Thread themselves and miscellaneous subroutines pthread_attr_Thread attributes objects pthread_mutex_Mutexes pthread_mutexattr_Mutex attributes objects pthread_cond_Condition variables pthread_condattr_Condition attributes objects pthread_key_Thread-specific data keys

 Create & Terminate  Program: Hello World

Creation  Routine pthread_create( thread, attr, start_routine, arg)

Termination  Four ways for a Pthread to terminate Returns from start_routine Call pthread_exit routine Cancel by another thread through pthread_cancel routine exit call

Example: Hello World  Program Requirements Print “Hello World” through standard output Use two threads to finish the task, one should print “Hello”, the other should print “World” Terminate all related threads related after finishing the printing task

Example: Hello World #include ……

Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); }

Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

Example: Hello World main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; …… }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World main() { …… pthread_create(&thread1, pthread_attr_default, (void*)&print_message_function, (void*) message1); pthread_create(&thread2, pthread_attr_default, (void*)&print_message_function, (void*) message2); exit(0); }

Example: Hello World default threadThread 2Thread 1

Example: Hello World default threadThread 2Thread 1

Example: Hello World default threadThread 2Thread 1

Example: Hello World default threadThread 2Thread 1

Example: Hello World default threadThread 2Thread 1

Example: Hello World default threadThread 2Thread 1 Printf

Example: Hello World default threadThread 2Thread 1 PrintfExitPrintf

Example: Hello World Thread 1Thread 2 standard output Thread 1 default thread Thread 2 Return

Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); } main() { …… pthread_exit(); //exit(0); }

Example: Hello World void print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s ", message); pthread_exit(); } main() { …… pthread_exit(); //exit(0); }

Passing parameters to threads  The pthread_create() routine permits the programmer to pass one argument to the thread start routine.  For cases where multiple arguments must be passed, this limitation is overcome by creating a structure which contains all of the arguments, and then passing a pointer to that structure.  All arguments must be passed by reference and cast to (void *).

Joining and detaching threads  pthread_join (threadid,status) blocks the calling thread until the specified threadid thread terminates. target thread's termination return status placed in status if specified in the target thread's call to pthread_exit(). A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread.  pthread_detach (threadid, status) to explicitly detach a thread  pthread_attr_setdetachstate (attr,detachstate)  pthread_attr_getdetachstate (attr, detachstate)

 Create & Destroy  Lock & Unlock  Program: Bounded Buffer

Mutex  Mutual Exclusion  Act as a “lock” to prevent race conditions  Typical sequence of use create, initialize lock, unlock destroy  Losers in the competition for the mutex variable have to block

Creating & Destroying  Type: pthread_mutex_t  Initializing Statically ○ pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER Dynamically ○ pthread_mutex_init (mutex, attr) pthread_mutex_attr_t attr specifies protocol for priorities, priority ceiling, and process sharing  Destroying pthread_mutex_destroy – to delete a mutex object pthread_mutex_attr_destroy – to delete a mutex attr object

Lock & Unlock  Lock pthread_mutex_lock (mutex) ○ blocks calling thread if mutex already locked pthread_mutex_trylock(mutex) ○ returns with “busy” code if already locked  Unlock pthread_mutex_unlock (mutex) Error ○ mutex is released ○ mutex owned by others