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? 

Slides:



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

Threads. Readings r Silberschatz et al : Chapter 4.
PTHREADS These notes are from LLNL Pthreads Tutorial
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
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.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition. This material does not exist in first edition)
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.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Jonathan Walpole Computer Science Portland State University
POSIX Threads HUJI Spring 2007.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Lab. 2 (April 27th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
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.
CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition chapter This material does not exist.
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
CS333 Intro to Operating Systems Jonathan Walpole.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
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.
C++ is Fun – Part 15 at Turbine/Warner Bros.! Russell Hanson.
Threads Dr. Yingwu Zhu. Threaded Applications Web browsers: display and data retrieval Web servers Many others.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
Concurrency I: Threads Nov 9, 2000 Topics Thread concept Posix threads (Pthreads) interface Linux Pthreads implementation Concurrent execution Sharing.
Carnegie Mellon Recitation 11: ProxyLab Fall 2009 November 16, 2009 Including Material from Previous Semesters' Recitations.
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.
Today’s topics Project 2 - Work on CEREAL - Due: Oct 10, :00 pm
Threads in C Caryl Rahn.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Pthreads void *sleeping(void *); /* thread routine */
CS399 New Beginnings Jonathan Walpole.
PTHREADS These notes are from LLNL Pthreads Tutorial
Concurrent Programming November 13, 2008
Concurrency I: Threads April 10, 2001
Lab. 2 Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment. Use cygwin.
Operating Systems Lecture 13.
Lab. 2 (May 12th) Modify the multithreaded JAVA code we practiced in Lab. 1 to C code with pthread library. Pthread library works in UNIX environment.
Jonathan Walpole Computer Science Portland State University
Operating Systems Lecture 14.
PTHREADS AND SEMAPHORES
CS510 Operating System Foundations
BOOM! count is [ ], should be
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Programming with Shared Memory
Concurrent Programming November 13, 2008
Computer Architecture Multi-threaded Matrix Multiply
Lecture 19: Threads CS April 3, 2019.
Shared Memory Programming with Pthreads
Presentation transcript:

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?  What are some common mistakes? Copyright ©: University of Illinois CS 241 Staff pthread Examples

Passing Arguments to Threads pthread_create()  All arguments must be passed by reference and cast to (void *)  Only one argument to the thread start routine  For multiple arguments Creating a structure that contains all of the arguments Pass a pointer to that structure in pthread_create() Copyright ©: University of Illinois CS 241 Staff

Passing Arguments to Threads Passing an int:  int i = 42; pthread_create(..., my_func, (void *)&i); Passing a C-string:  char *str = "UIUC"; pthread_create(..., my_func, (void *)str); Passing an array:  int arr[100]; pthread_create(..., my_func, (void *)arr); Where should these be declared?

Passing Arguments to Threads Retrieving an int:  void *myfunc(void *vptr_value) { int value = *((int *)vptr_value); Retrieving a C-string:  void *myfunc(void *vptr_value) { char *str = (char *)vptr_value; Retrieving an array:  void *myfunc(void *vptr_value) { int *arr = (int *)vptr_value; Copyright ©: University of Illinois CS 241 Staff

Passing Arguments to Threads Putting it all together:  void *myfunc(void *vptr_value) { int value = *((int *)vptr_value); printf("Thread value: %d", value); return NULL; } int main() { pthread_t tid; int i = 6; pthread_create(&tid, NULL, myfunc, &i); pthread_join(tid, NULL); return 0; } Notifies the pthread library to use default attributes Notifies the pthread library to ignore return value of myfunc Will this be a problem?

Thread Argument Passing How can you safely pass data to newly created threads, given their non-deterministic start-up and scheduling?  Make sure that all passed data is thread safe i.e., it cannot be changed by other threads The following code fragment:  Demonstrates how to pass a simple integer to each thread  The calling thread uses a unique data structure for each thread  Each thread's argument remains intact throughout the program Copyright ©: University of Illinois CS 241 Staff

Thread Argument Passing #include #define NUM_THREADS8 char *messages[NUM_THREADS]; void *PrintHello(void *threadid) { int *id_ptr, taskid; sleep(1); id_ptr = (int *) threadid; taskid = *id_ptr; printf("Thread %d: %s\n", taskid, messages[taskid]); pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff

Thread Argument Passing int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int *taskids[NUM_THREADS]; int rc, t; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; Copyright ©: University of Illinois CS 241 Staff

Thread Argument Passing for(t=0;t<NUM_THREADS;t++) { taskids[t] = (int *) malloc(sizeof(int)); *taskids[t] = t; printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]); if (rc) { printf("ERR; pthread_create() ret = %d\n", rc); exit(-1); } pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff

Passing Complex Arguments #include #define NUM_THREADS8 char *messages[NUM_THREADS]; struct thread_data { intthread_id; int sum; char *message; }; struct thread_data thread_data_array[NUM_THREADS]; Copyright ©: University of Illinois CS 241 Staff

Passing Complex Arguments void *PrintHello(void *threadarg) { int taskid, sum; char *hello_msg; struct thread_data *my_data; sleep(1); my_data = (struct thread_data *) threadarg; taskid = my_data->thread_id; sum = my_data->sum; hello_msg = my_data->message; printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum); pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff

Passing Complex Arguments int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t, sum; sum=0; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; Copyright ©: University of Illinois CS 241 Staff

Passing Complex Arguments for(t=0;t<NUM_THREADS;t++) { sum = sum + t; thread_data_array[t].thread_id = t; thread_data_array[t].sum = sum; thread_data_array[t].message = messages[t]; printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]); if (rc) { printf("ERR; pthread_create() ret = %d\n", rc); exit(-1); } pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff

Passing Complex Arguments for(t=0;t<NUM_THREADS;t++) { sum = sum + t; thread_data_array[t].thread_id = t; thread_data_array[t].sum = sum; thread_data_array[t].message = messages[t]; printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &thread_data_array[t]); if (rc) { printf("ERR; pthread_create() ret = %d\n", rc); exit(-1); } pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);

Incorrect Argument Passing #include #define NUM_THREADS8 void *PrintHello(void *threadid) { int *id_ptr, taskid; sleep(1); id_ptr = (int *) threadid; taskid = *id_ptr; printf("Hello from thread %d\n", taskid); pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff

Incorrect Argument Passing int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0;t<NUM_THREADS;t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t); if (rc) { printf("ERR; pthread_create() ret = %d\n", rc); exit(-1); } pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff The loop that creates threads modifies the contents of the address passed as an argument, possibly before the created threads can access it. What is the possible output?

More Pthreads Examples #include #define NUM_THREADS 5 int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0;t < NUM_THREADS;t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; pthread_create() return code is %d\n", rc); exit(-1); } Copyright ©: University of Illinois CS 241 Staff void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } Will all threads get a chance to execute? pthread_exit(NULL);

More Pthreads Examples #include #define NUM_THREADS 5 int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for(t=0;t < NUM_THREADS;t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; pthread_create() return code is %d\n", rc); exit(-1); } pthread_exit(NULL); } Copyright ©: University of Illinois CS 241 Staff void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } Will all threads get a chance to execute before the parent exits? for(t=0;t < NUM_THREADS;t++) { pthread_join( thread[t], NULL); printf(“Joining thread %d\n", t); }

Returning data through pthread_join() void *thread(void *vargp) { pthread_exit((void *)42); } int main() { int i; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **)&i); printf("%d\n",i); } Copyright ©: University of Illinois CS 241 Staff This is legal, but is it good programming practice?

Returning data through pthread_join() void *thread(void *vargp) { int *value = (int *)malloc(sizeof(int)); *value = 84; pthread_exit(value); } int main() { int i; pthread_t tid; void *vptr_return; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, &vptr_return); i = *((int *)vptr_return); free(vptr_return); printf("%d\n",i); } Copyright ©: University of Illinois CS 241 Staff

Incorrectly returning data through pthread_join() void *thread(void *vargp) { exit(42); } int main() { int i; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void **)&i); printf("%d\n",i); } Copyright ©: University of Illinois CS 241 Staff What will happen here?

Incorrectly returning data through pthread_join() void *thread(void *vargp) { pthread_detach(pthread_self()); pthread_exit((void*)42); } int main() { int i = 0; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); pthread_join(tid, (void**)&i); printf("%d\n",i); } Copyright ©: University of Illinois CS 241 Staff What will happen here?

Incorrectly returning data through pthread_join() typedef struct _mystruct { double d; int i; } mystruct; void *myfunc(void *vptr) { mystruct my; my.d = ; my.i = 42; pthread_exit((void*)&my); } int main() { pthread_t pid; mystruct my; void *vptr; pthread_create(&pid, NULL, myfunc, NULL); pthread_join(pid, &vptr); my = *((mystruct *)vptr); free(vptr); printf("(%f, %d)", my.d, my.i); return 0; } Copyright ©: University of Illinois CS 241 Staff

Returning data through pthread_join() typedef struct _mystruct { double d; int i; } mystruct; void *myfunc(void *vptr) { mystruct *my = (mystruct *) malloc(sizeof(mystruct)); my->d = ; my->i = 42; pthread_exit((void*)my); } int main() { pthread_t pid; mystruct my; void *vptr; pthread_create(&pid, NULL, myfunc, NULL); pthread_join(pid, &vptr); my = *((mystruct *)vptr); free(vptr); printf("(%f, %d)", my.d, my.i); return 0; } Copyright ©: University of Illinois CS 241 Staff