1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
Advertisements

Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
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.
THREAD IMPLEMENTATION For parallel processing. Steps involved Creation Creates a thread with a thread id. Detach and Join All threads must be detached.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
The University of Adelaide, School of Computer Science
POSIX Threads Programming The following is extracted from a tutorial by Blaise Barney at Livermore Computing Blaise Barney (Lawrence Livermore National.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
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.
Programming with POSIX* Threads Intel Software College.
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
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
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.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
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)
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.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
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.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Tutorial 4. In this tutorial session we’ll see Threads.
Realizing Concurrency using the thread model
Today’s topics Project 2 - Work on CEREAL - Due: Oct 10, :00 pm
Principles of Operating Systems Lecture 11
Threads in C Caryl Rahn.
Shared-Memory Programming with Threads
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Multithreading Tutorial
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
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo

2 What are Pthreads? Historically, hardware vendors have implemented their own proprietary versions of threads. Not portable. Pthread is a standardized thread programming interface specified by the IEEE POSIX (portable operating systems interface) in 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.

3 Why Pthread ? To realize potential program performance gains. When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes. All threads within a process share the same address space. Inter-thread communication is more efficient and in many cases, easier to use than inter-process communication.

4 Why Pthread ? Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways: –Overlapping CPU work with I/O –Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower priority tasks. –Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of new requests.

5 Include files and libraries.h file #include #include //for semaphore only Compile and Linking $gcc foo.c -o foo -lpthread –lrt (for semaphore)

6 The Pthreads API Thread management: The first class of functions work directly on threads - creating, terminating, joining, etc. Semaphores: provide for create, destroy, wait, and post on semaphores. Mutexes: provide for creating, destroying, locking and unlocking mutexes. Condition variables: include functions to create, destroy, wait and signal based upon specified variable values.

7 Thread Creation pthread_create (tid, attr, start_routine, arg) It returns the new thread ID via the tid argument. The attr parameter is used to set thread attributes, NULL for the default values. The start_routine is the C routine that the thread will execute once it is created. A single argument may be passed to start_routine via arg. It must be passed by reference as a pointer cast of type void.

8 Thread Termination and Join pthread_exit (value) ; This Function is used by a thread to terminate. The return value is passed as a pointer. pthread_join (tid, value_ptr); The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. Return 0 on success, and negative on failure. The returned value is a pointer returned by reference. If you do not care about the return value, you can pass NULL for the second argument.

9 Example Code - Pthread Creation and Termination #include void *PrintHello(void * id) { printf(“Thread%d: Hello World!\n", id); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); pthread_exit(NULL); }

10 Thread Identifiers pthread_self (); pthread_equal (tid1,tid2); The pthread_self() routine returns the unique, system assigned thread ID of the calling thread. The pthread_equal() routine compares two thread IDs. If the two IDs are different 0 is returned, otherwise a non-zero value is returned.

11 Mutex Variables For thread synchronization and protecting shared data when multiple writes occur. A mutex variable acts like a "lock" protecting access to a shared data resource. Only one thread can lock (or own) a mutex variable at any given time. Thus, even if several threads try to lock a mutex only one thread will be successful. No other thread can own that mutex until the owning thread unlocks that mutex. Threads must "take turns" accessing protected data.

12 Creating / Destroying Mutexes pthread_mutex_init (mutex, attr) pthread_mutex_destroy (mutex) Mutex variables must be declared with type pthread_mutex_t, and must be initialized before they can be used. attr, mutex object attributes, specified as NULL to accept defaults

13 Locking / Unlocking Mutexes pthread_mutex_lock (mutex); pthread_mutex_trylock (mutex); pthread_mutex_unlock (mutex); The pthread_mutex_lock() routine is used by a thread to acquire a lock on the specified mutex variable. The thread blocks if the mutex is already locked by another thread. pthread_mutex_trylock() will attempt to lock a mutex. However, if the mutex is already locked, the routine will return immediately with a "busy" error code. pthread_mutex_unlock() will unlock a mutex if called by the owning thread.

14 Semaphores Semaphore are not defined in the POSIX.4a (pthread) specifications, but they are included in the POSIX.4 (realtime extension) specifications..h #include Semaphore descriptors are declared global ex: sem_t mutex, full, empty;

15 Routines of Semaphore sem_t sp; sem_init(&sp, pshared, init_value) –If pshared is nonzero, the semaphore can be shared between processes. sem_destroy(&sp) sem_wait (&sp); //P operation sem_trywait(&sp); sem_post (&sp); // V operation

16 Condition Variables While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling. A condition variable is always used in conjunction with a mutex lock.

17 Conditional Variable Routines pthread_cond_init (condition, attr) pthread_cond_destroy (condition) pthread_cond_wait (condition, mutex) pthread_cond_signal (condition) pthread_cond_broadcast (condition)

18 A representative sequence for using condition variables Main Thread Declare and initialize global data/variables Declare and initialize a condition variable object Declare and initialize an associated mutex Create threads A and B to do work Thread A Do work up to the point where a certain condition must occur (such as "count" must reach a specified value) Lock associated mutex and check value of a global variable Call pthread_cond_wait() to perform a blocking wait for signal from Thread-B. It will automatically and atomically unlocks the associated mutex variable so that it can be used by Thread-B. When signalled, wake up. Mutex is automatically and atomically locked. Explicitly unlock mutex. Continue Thread B Do work Lock associated mutex Change the value of the global variable that Thread-A is waiting upon. Check value of the global Thread-A wait variable. If it fulfills the desired condition, signal Thread-A. Unlock mutex. Continue Main Thread Join / Continue

19 References POSIX thread programming "Multithreaded, Parallel, and Distributed Programming" by Gregory R. Andrews. Introduction to PThreads Getting Started With POSIX Threads