Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at https://computing.llnl.gov/tutorials/pthreads.

Slides:



Advertisements
Similar presentations
Threads. Readings r Silberschatz et al : Chapter 4.
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.
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;
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
1 Confidential Enterprise Solutions Group Process and Threads.
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.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
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.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
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
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
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.
Threads A thread is an alternative model of program execution
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.
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.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
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.
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.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
Linux Processes & Threads
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Multithreading Tutorial
Realizing Concurrency using the thread model
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.
Presentation transcript:

Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at https://computing.llnl.gov/tutorials/pthreads

What is a Thread? OS view: A thread is an independent stream of instructions that can be scheduled to run by the OS. Software developer view: a thread can be considered as a “procedure” that runs independently from the main program. Sequential program: a single stream of instructions in a program. Multi-threaded program: a program with multiple streams Multiple threads are needed to use multiple cores/CPUs

Example multithread programs? Computer games each thread controls the movement of an object. Scientific simulations Hurricane movement simulation: each thread simulates the hurricane in a small domain. Molecular dynamic: each thread simulates a subset of particulars. …… Web server Each thread handles a connection.

Process and Thread Process context Process ID, process group ID, user ID, and group ID Environment Working directory. Program instructions Registers (including PC) Stack Heap File descriptors Signal actions Shared libraries Inter-process communication tools Two parts in the context: self-contained domain (protection) and execution of instructions.

Process and Thread What are absolutely needed to support a stream of instructions, given the process context? Process ID, process group ID, user ID, and group ID Environment Working directory. Program instructions Registers (including PC) Stack Heap File descriptors Signal actions Shared libraries Inter-process communication tools

Process and Thread

Threads… Exist within processes Die if the process dies Use process resources Duplicate only the essential resources for OS to schedule them independently Each thread maintains Stack Registers Scheduling properties (e.g. priority) Set of pending and blocked signals (to allow different react differently to signals) Thread specific data

Pthreads… Hardware vendors used to implement proprietary versions of threads Thread programs are not portable Pthreads = POSIX threads, specified in IEEE POSIX 1003.1c (1995)

Advantages of Threads Light-weight Lower overhead for thread creation Lower Context Switching Overhead Fewer OS resources

Platform fork() pthread_create() real user sys AMD 2.4 GHz Opteron (8cpus/node) 41.1 60.1 9.0 0.7 0.2 0.4 IBM 1.9 GHz POWER5 p5-575 (8cpus/node) 64.2 30.8 27.7 1.8 1.1 IBM 1.5 GHz POWER4 (8cpus/node) 104.1 48.6 47.2 2.0 1.0 1.5 INTEL 2.4 GHz Xeon (2 cpus/node) 55.0 20.8 1.6 0.9 INTEL 1.4 GHz Itanium2 (4 cpus/node) 54.5 22.2 1.3

Advantages of Threads Shared State Don’t need IPC-like mechanism to communicate between threads of same process

Disadvantages of Threads Shared State! Global variables are shared between threads. Accidental changes can be fatal. Many library functions are not thread-safe Library Functions that return pointers to static internal memory. E.g. gethostbyname() Lack of robustness Crash in one thread will crash the entire process.

The Pthreads API Three types of routines: Thread management: create, terminate, join, and detach Mutexes: mutual exclusion, creating, destroying, locking, and unlocking mutexes Condition variables: event driven synchronizaiton. Mutexes and condition variables are concerned about synchronization. Why not anything related to inter-thread communication? The concept of opaque objects pervades the design of the API.

The Pthreads API naming convention Routine Prefix Function Pthread_ General pthread Pthread_attr_ Thread attributes Pthread_mutex_ mutex Pthread_mutexattr Mutex attributes Pthread_cond_ Condition variables Pthread_condaddr Conditional variable attributes Pthread_key_ Thread specific data keys

Thread management routines Creation: pthread_create Termination: Return Pthread_exit Can we still use exit? Wait (parent/child synchronization): pthread_join Pthread header file <pthread.h> Compiling pthread programs: gcc –lpthread aaa.c

Creation Thread equivalent of fork() int pthread_create( pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg ); Returns 0 if OK, and non-zero (> 0) if error. Parameters for the routines are passed through void * arg. What if we want to pass a structure?

Termination Thread Termination Process Termination Return from initial function. void pthread_exit(void * status) Process Termination exit() called by any thread main() returns

Waiting for child thread int pthread_join( pthread_t tid, void **status) Equivalent of waitpid()for processes

pthread_detach(pthread_self()) Detaching a thread The detached thread can act as daemon thread The parent thread doesn’t need to wait int pthread_detach(pthread_t tid) Detaching self : pthread_detach(pthread_self())

Some multi-thread program examples A multi-thread program example: Example1.c Making multiple producers, give each an ID: example2.c What is going on in this program?

Matrix multiply and threaded matrix multiply Matrix multiply: C = A × B

Matrix multiply and threaded matrix multiply Sequential code: For (i=0; i<N; i++) for (j=0; j<N; j++) for (k=0; k<N; k++) C[I, j] = C[I, j] + A[I, k] * A[k, j] Threaded code program Do the same sequential operation, different threads work on different part of the C array. How to decide who does what? Need three parameters: N, nthreads, myid

Matrix multiply and threaded matrix multiply Threaded code program From N, nthreads, myid I am responsiable for sub-array C[0..N-1][N/Nthreads*myrank ..N/Nthreads*(myrank+1)-1) The calculation of c[I,j] does not depend on other C term. Mm_pthread.c.

PI calculation Sequential code: pi.c Multi-threaded version: pi_pthread.c Again domain partition based on N, nthreads, and myid.

Review What is a thread? What are in the context of a thread? Why switching between threads is faster than process context switching? Name something to be careful about (or weaknesses) when using thread How to create a pthread? how to pass multiple parameters to a thread starting routine? Why we almost always need nthreads, and myid in a thread program?