Page 1 threads CS 360, WSU Vancouver POSIX Threads 1. Background 2. Threads vs. Processes 3. Thread Synchronization 4. Mutex Variables 5. Condition Variables.

Slides:



Advertisements
Similar presentations
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
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.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
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.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
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,
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.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
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)
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  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.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
Multithreading Tutorial
POSIX Threads 1. Background 2. Threads vs. Processes
Multithreading Tutorial
Thread Programming.
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Multithreading Tutorial
Programming with Shared Memory
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
POSIX Threads(pthreads)
Presentation transcript:

Page 1 threads CS 360, WSU Vancouver POSIX Threads 1. Background 2. Threads vs. Processes 3. Thread Synchronization 4. Mutex Variables 5. Condition Variables 6. Threads and UNIX

Page 2 threads CS 360, WSU Vancouver 1. Background l Threads are light-weight processes –only local variables in a function are specific to a thread (e.g. each thread has its own stack) –most other data is shared between threads (e.g. global variables & the heap) l pthreads is the POSIX threads standard. –The associated library interface is obtained by: –#include

Page 3 threads CS 360, WSU Vancouver 1.1. Reasons for Using Threads l Efficiency l Ease of data sharing l Common uses of parallelism: –overlapping I/O l do long I/O and CPU tasks at the same time –asynchronous events l wait for a response and do something else –real-time scheduling l quickly respond to important tasks –to utilize multiple processors

Page 4 threads CS 360, WSU Vancouver 2. Threads vs Processes l A thread uses less system resources than a similar process (for the same task). l A thread may require more user space resources than a similar process –depends on the implementation l Threads (within the same process) share everything except their stack and processor state continued

Page 5 threads CS 360, WSU Vancouver l The threads mechanism can be used instead of many different process-based mechanisms: –non-blocking I/O, shared memory (IPC), jmp() functions, signals,... l Threads use an inherently simpler shared memory mechanism than processes: –inter-thread communication is far easier –but it is also much easier to code parallelism errors (e.g. race conditions)

Page 6 threads CS 360, WSU Vancouver Problems with Threads l Unpredictable functioning (non-determinism) l Difficult to debug l Impossible to prove correct l Almost impossible to test thoroughly l Specific behavior not easily reproducible l Simple conceptually, but added complexities for dealing with many pitfalls

Page 7 threads CS 360, WSU Vancouver 2.1. Processes Example shared memory child 1: do_one_thing() child 2: do_another_thing() r1p r2p Global variables, etc shmid, shm_ptr, r1p, r2p Global variables, etc shmid, shm_ptr, r1p, r2p

Page 8 threads CS 360, WSU Vancouver #include #include #include #include #include #include #include void do_one_thing(int *pnum); void do_another_thing(int *pnum); int shmid, *shm_ptr;/* global, but not shared */ int *r1p, *r2p; : : 2.1. ints_processes.c continued

Page 9 threads CS 360, WSU Vancouver int main() { pid_t child1, child2; /* initialize shared memory */ shmid = shmget(IPC_PRIVATE, 2*sizeof(int), (IPC_CREAT | 0666)); if ((shm_ptr = (int *)shmat(shmid, (char *)0, 0)) < 0 ) perror("shmat failed"); : continued

Page 10 threads CS 360, WSU Vancouver /* initialise shared memory ints */ r1p = shm_ptr; r2p = (shm_ptr + 1); *r1p = 0; *r2p = 0; /* start forking children */ if ((child1 = fork()) == 0) { /* first child */ do_one_thing(r1p); exit(0); } : continued

Page 11 threads CS 360, WSU Vancouver if ((child2 = fork()) == 0) { /* second child */ do_another_thing(r2p); exit(0); } /* parent waits for children */ wait(NULL); wait(NULL); printf(“Final Values: %d, %d\n”, *r1p, *r2p); return 0; } continued

Page 12 threads CS 360, WSU Vancouver void do_one_thing(int *pnum) /* Waste time, and increment pnum */ { int i, j, x=0; for (i = 0; i < 4; i++) { printf("doing one thing\n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum)++; } } continued

Page 13 threads CS 360, WSU Vancouver void do_another_thing(int *pnum) /* Waste time, and increment pnum. The code is almost the same as do_one_thing() */ { int i, j, x=0; for (i = 0; i < 4; i++) { printf("doing another \n"); for (j = 0; j < 10000; j++) x = x + i; (*pnum)++; } } continued

Page 14 threads CS 360, WSU VancouverUsage $ ints_processes doing one thing doing one thing doing another doing another doing another doing another doing one thing doing one thing Final Values: 4, 4 $ Order may vary each time ints_processes is executed.

Page 15 threads CS 360, WSU Vancouver 2.2. Threads Version thread 1: do_one_thing() thread 2: do_another_thing() Global variables, etc r1 r2 shared by default

Page 16 threads CS 360, WSU Vancouver 2.2. ints_threads.c #include /* Same functions as in simple_processes.c */ void do_one_thing(int *pnum); void do_another_thing(int *pnum); /* Global (and shared) integers */ int r1 = 0, r2 = 0; : continued

Page 17 threads CS 360, WSU Vancouver int main() { pthread_t thread1, thread2; pthread_create(&thread1, NULL, (void *) do_one_thing, (void *) &r1); pthread_create(&thread2, NULL, (void *) do_another_thing, (void *) &r2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); printf(“Final Values: %d, %d\n”,r1, r2); return 0; }

Page 18 threads CS 360, WSU VancouverNotes l No need for shared memory library functions –threads approach has better performance –And simpler code l Difficult to program –synchronization problems (with processes, too) l Global variables are accessible to all threads –easy to make coding mistakes

Page 19 threads CS 360, WSU Vancouver 2.3. Thread Functions int pthread_create(pthread_t *thread, const pthread_attr_t attr, void *(*func)(void *), void *arg); Create a new thread with attributes specified in attr (usually NULL ). Start executing func() and pass it arg. l Consider carefully what to pass as the argument l Return 0 if ok, non-zero if error. continued

Page 20 threads CS 360, WSU Vancouver int pthread_join(pthread_t thread, void **value_ptr); Make the calling thread wait for the specified thread to terminate. value_ptr is assigned its return value (or PTHREAD_CANCELLED ).

Page 21 threads CS 360, WSU Vancouver 2.4. Matrix Multiplication Example l Parallelize matrix multiplication: * =

Page 22 threads CS 360, WSU Vancouver Coding Approach Create MATSIZE (e.g. 4) threads: one for each column of the results[] array –each column will be calculated in parallel The parallelism could be increased by creating MATSIZE*MATSIZE threads: one for each element of the results[] array.

Page 23 threads CS 360, WSU Vancouvermatmult.c #include #include #define MATSIZE 4 void *matMult(void *); /* note the template */ void printMult(void); /* global and shared data */ int mat1[MATSIZE][MATSIZE] = {{9,8,7,6},{6,5,4,3},{3,2,1,0},{0,-1,-2,-3}}; int mat2[MATSIZE][MATSIZE] = {{1,2,3,4},{4,5,6,7},{7,8,9,10},{10,11,12,13}}; int result[MATSIZE][MATSIZE]; :

Page 24 threads CS 360, WSU Vancouver int main() { pthread_t thr[MATSIZE]; int i, *iPtr; for(i=0; i<MATSIZE; i++) iPtr = (int*) malloc(sizeof(int)); *iPtr = i; pthread_create(&thr[i], NULL, matMult, (void *)iPtr); for(i=0; i < MATSIZE; i++) pthread_join(thr[i], NULL); printMult(); return 0; }

Page 25 threads CS 360, WSU Vancouver void *matMult(void *colvPtr) { int i, j; int col = *colvPtr; free(colPtr); for(i=0; i < MATSIZE; i++) { result[i][col] = 0; for(j=0; j < MATSIZE; j++) result[i][col] += mat1[i][j] * mat2[j][col]; } return NULL; }

Page 26 threads CS 360, WSU Vancouver void printMult(void) { int i, j; for(i=0; i < MATSIZE; i++) { printf(“|”); for(j=0; j < MATSIZE; j++) printf(“%3d”, mat1[i][j]); printf(“|%c|”, (i==MATSIZE/2 ? ‘*’ : ‘‘)); for(j=0; j < MATSIZE; j++) printf(“%3d”, mat2[i][j]); printf(“|%c|”, (i==MATSIZE/2 ? ‘=’ : ‘‘)); for(j=0; j < MATSIZE; j++) printf(“%4d”, result[i][j]); printf(“|\n”); } }

Page 27 threads CS 360, WSU Vancouver 3. Thread Synchronization pthread_join() –like wait() for processes l mutex variables –like binary semaphores for processes l condition variables –wait for an ‘event’ (e.g. a variable is assigned a certain value), which is ‘signaled’ by another thread.

Page 28 threads CS 360, WSU Vancouver 4. Mutex Variables l A mutex variable is a mutual exclusion lock, allowing threads to control access to shared data. l Only one thread can hold a mutex at a time. l The threads must agree to use the mutex to protect the shared data. l Mutex variables are not managed by OS and thus very efficient (no system calls).

Page 29 threads CS 360, WSU Vancouver Example Diagram thread 1: do_one_thing() thread 2: do_another_thing() Global variables, etc r1 r2 shared by default r3 r3_mutex lock

Page 30 threads CS 360, WSU Vancouverints_mutex.c #include #include #include void lock_one_thing(int *pnum); void lock_another_thing(int *pnum); /* global (and shared) variables */ int r1 = 0, r2 = 0, r3 = 0; pthread_mutex_t r3_mutex; : continued

Page 31 threads CS 360, WSU Vancouver void main(int argc, char *argv[]) { pthread_t thread1, thread2; pthread_mutex_init(&r3_mutex, NULL); if (argc < 2) { printf("usage %s number\n",argv[0]); exit(1); } r3 = atoi(argv[1]); : continued

Page 32 threads CS 360, WSU Vancouver : pthread_create(&thread1, NULL, (void *) lock_one_thing, (void *) &r1); pthread_create(&thread2, NULL, (void *) lock_another_thing, (void *) &r2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); printf(“Final Values: %d, %d\n”, r1, r2); } continued

Page 33 threads CS 360, WSU Vancouver void lock_one_thing(int *pnum) { int i, j, x=0; for (i = 0; i < 4; i++) { pthread_mutex_lock(&r3_mutex); r3 = r3 + (*pnum); printf(“one altered r3: %d\n”, r3); pthread_mutex_unlock(&r3_mutex); for (j = 0; j < 10000; j++) x = x + i; (*pnum)++; } } continued

Page 34 threads CS 360, WSU Vancouver void lock_another_thing(int *pnum) { int i, j, x=0; for (i = 0; i < 4; i++) { pthread_mutex_lock(&r3_mutex); r3 = r3 + (*pnum); printf(“another altered r3: %d\n”,r3); pthread_mutex_unlock(&r3_mutex); for (j = 0; j < 10000; j++) x = x + i; (*pnum)++; } } continued

Page 35 threads CS 360, WSU VancouverUsage $ ints_mutex 4 one altered r3: 4 one altered r3: 5 one altered r3: 7 one altered r3: 10 another altered r3: 10 another altered r3: 11 another altered r3: 13 another altered r3: 16 Final Values: 4, 4 $

Page 36 threads CS 360, WSU Vancouver 4.2. Mutex Functions int pthread_mutex_lock( pthread_mutex_t *mutex); l Lock an unlocked mutex; if already locked, the thread waits until the mutex becomes unlocked. l int pthread_mutex_trylock( pthread_mutex_t *mutex); l Lock an unlocked mutex, but if locked, do not block and return EBUSY continued

Page 37 threads CS 360, WSU Vancouver int pthread_mutex_unlock( pthread_mutex_t *mutex); l Unlock a mutex; if any threads are waiting to lock this mutex, one is woken up.

Page 38 threads CS 360, WSU Vancouver 5. Condition Variables l Synchronize threads by using events –e.g. a variable is assigned a certain value l A thread (or threads) wait for an event which is ‘signaled’ by another thread. –These events are not UNIX signals l The ‘signal’ causes the thread (or threads) to wake up. l We won’t cover the details of coding this! –This could be used effectively on the dining philosophers problem.

Page 39 threads CS 360, WSU Vancouver 6. Threads and UNIX l UNIX was originally designed to handle processes before shared memory multiprocessors were available. –how to add in threads to take advantage of multiple CPUs ? l A process is a ‘container’ for one or more threads –all the threads share the process’ memory address space

Page 40 threads CS 360, WSU Vancouver How do threads deal with... l signals? l library functions? l process management? –e.g. fork(), exec()

Page 41 threads CS 360, WSU Vancouver 6.1. Signals l Each thread can have its own signal mask and signal actions. l Signals can be sent to a specific thread or to the process that ‘holds’ the thread(s). –Synchronous signals get delivered to the thread that caused them (such as SEGV or SIGFPE)

Page 42 threads CS 360, WSU Vancouver 6.2. Library Functions l How do several threads share the same library function at the same time? –Any function that uses global variables (or statically declared variables) is suspect! l Answer: thread-safe libraries –Libraries can be made thread-safe by adding mutexes around the library function’s global variables –library functions may not be thread-safe! –Some libraries have thread-safe alternatives (ctime vs. ctime_r)

Page 43 threads CS 360, WSU Vancouver l What if a thread is terminated inside a library function? –e.g. in the middle of changing global data l Answer: the pthreads library includes cancellation-safe functions –they clean up upon cancellation

Page 44 threads CS 360, WSU Vancouver l What if a thread blocks inside a library function? l Answer: the pthreads library includes many functions which only block the thread, not the entire process –the programmer can also turn off blocking in other functions

Page 45 threads CS 360, WSU Vancouver 6.3. Process Management What does a fork() call from a thread do to the other threads in the containing process? Answer: the new child process contains a single copy of the thread that called fork() –What happens to mutexes? –big headaches are possible! l Guidelines: –Fork from a process with only one thread –Fork before creating additional threads –Fork only from the main (parent) thread –Hold no locks during the fork

Page 46 threads CS 360, WSU Vancouver What does an exec() call from a thread do to the threads in the containing process? Answer: all the threads terminate, and a new thread is created for the exec() program.