POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.

Slides:



Advertisements
Similar presentations
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Advertisements

Threads. Readings r Silberschatz et al : Chapter 4.
1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
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.
1 CS 333 Introduction to Operating Systems Class 3 – Threads & Concurrency Jonathan Walpole Computer Science Portland State University.
Jonathan Walpole Computer Science Portland State University
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Processes, Threads, SMP, and Microkernels
OPERATIONS ON PROCESSES Process creation fork() exec() The argument vector Process deletion kill() signal()
Project 2 Data Communication Spring 2010, ICE Stephen Kim, Ph.D.
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.
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
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
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.
Multicore Programming (Parallel Computing) HP Training Session 5 Prof. Dan Connors.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
CS533 - Concepts of Operating Systems 1 Anyone NOT on this list see me after class! Arryadi, Rizal Carlson, Kristen Ellet, Burke Florey, David Greenwald,
POSIX Threads HUJI Spring 2011.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Now that you know the pthread API…  How do you create threads?  How do you pass different values to them?  How do you return values from threads? 
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS533 Concepts of Operating Systems Jonathan Walpole.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
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.
Tutorial 4. In this tutorial session we’ll see Threads.
Lecture 5 : Pthread Programming
Lab. 2 (April 15th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Threads and Cooperation
Operating Systems Lecture 13.
Jonathan Walpole Computer Science Portland State University
Operating Systems Lecture 14.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
CSCE 513 Computer Architecture
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Programming with Shared Memory
Tutorial 4.
Programming with Shared Memory - 2 Issues with sharing data
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4 Threads “Most modern applications are multithreaded”
Computer Architecture Multi-threaded Matrix Multiply
Shared Memory Programming with Pthreads
Presentation transcript:

POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems

Overview  Background Information on Threads  What are POSIX Threads?  Why use POSIX Threads?  Design of POSIX Threads  Thread Management  Pthread Example

Threads  A Thread is an independent stream of instructions that can be schedule to run as such by the OS.  Think of a thread as a “procedure” that runs independently from its main program.  Multi-threaded programs are where several procedures are able to be scheduled to run simultaneously and/or independently by the OS.  A Thread exists within a process and uses the process resources.

Threads (cont)  Threads only duplicate the essential resources it needs to be independently schedulable.  A thread will die if the parent process dies.  A thread is “lightweight” because most of the overhead has already been accomplished through the creation of the process.

POSIX Threads (PThreads)  For UNIX systems, implementations of threads that adhere to the IEEE POSIX c standard are Pthreads.  Pthreads are C language programming types defined in the pthread.h header/include file.

Why Use Pthreads  The primary motivation behind Pthreads is improving program performance.  Can be created with much less OS overhead.  Needs fewer system resources to run.  View comparison of forking processes to using a pthreads_create subroutine. Timings reflect 50,000 processes/thread creations.

PLATFORMfork()pthread_create() REALUSERSYSTEMREALUSERSYSTEM AMD 2.4 GHz Opteron (8cpus/node) IBM 1.9 GHz POWER5 p5-575 (8cpus/node) IBM 1.5 GHz POWER4 (8cpus/node) INTEL 2.4 GHz Xeon (2 cpus/node) INTEL 1.4 GHz Itanium2 (4 cpus/node) Threads vs Forks

Designing Pthreads Programs  Pthreads are best used with programs that can be organized into discrete, independent tasks which can execute concurrently.  Example: routine 1 and routine 2 can be interchanged, interleaved and/or overlapped in real time.

Candidates for Pthreads

Designing Pthreads (cont)  Common models for threaded programs: –Manager/Worker: manager assigns work to other threads, the workers. Manager handles input and hands out the work to the other tasks. –Pipeline: task is broken into a series of suboperations, each handled in series but concurrently, by a different thread.

Pthread Management – Creating Threads  The main() method comprises a single, default thread.  pthread_create() creates a new thread and makes it executable.  The maximum number of threads that may be created by a process in implementation dependent.  Once created, threads are peers, and may create other threads.

Pthread Management – Terminating Threads  Several ways to terminate a thread: –The thread is complete and returns –The pthread_exit() method is called –The pthread_cancel() method is invoked –The exit() method is called  The pthread_exit() routine is called after a thread has completed its work and it no longer is required to exist.

Terminating Threads (cont)  If the main program finishes before the thread(s) do, the other threads will continue to execute if a pthread_exit() method exists.  The pthread_exit() method does not close files; any files opened inside the thread will remain open, so cleanup must be kept in mind.

Pthread Example #include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { int tid; tid = (int)threadid; int tid; tid = (int)threadid; printf("Hello World! It's me, thread #%d!\n", tid); printf("Hello World! It's me, thread #%d!\n", tid); pthread_exit(NULL); pthread_exit(NULL);} int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; pthread_t threads[NUM_THREADS]; int rc, t; int rc, t; for(t=0; t<NUM_THREADS; t++) for(t=0; t<NUM_THREADS; t++) { printf("In main: creating thread %d\n", t); printf("In main: creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); exit(-1); } } pthread_exit(NULL); pthread_exit(NULL);}

Pthread Example - Output In main: creating thread 0 In main: creating thread 1 Hello World! It's me, thread #0! In main: creating thread 2 Hello World! It's me, thread #1! Hello World! It's me, thread #2! In main: creating thread 3 In main: creating thread 4 Hello World! It's me, thread #3! Hello World! It's me, thread #4!