Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.

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

1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
PTHREADS These notes are from LLNL Pthreads Tutorial
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 Threads are lightweight processes
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Outline Unix Architecture Vi Fork Pthread. UNIX Architecture.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
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.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
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.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
1 Threads. 2 Processes versus Threads 3 Why Threads? Processes do not share resources very well Why? Process context switching cost is very high Why?
CS333 Intro to Operating Systems Jonathan Walpole.
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© 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.
Threads Dr. Yingwu Zhu. Threaded Applications Web browsers: display and data retrieval Web servers Many others.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
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:
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.
Realizing Concurrency using the thread model
Lecture 5 : Pthread Programming
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Principles of Operating Systems Lecture 8
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Lecture 8: POSIX Threads
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
Lecture 8: POSIX Threads
Tutorial 4.
Concurrency and Threading: Introduction
POSIX Threads(pthreads)
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 Thread Packages Kernel thread packages Implemented and supported at kernel level User-level thread packages Implemented at user level

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Implementing Threads in User Space (Old Linux) A user-level threads package

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 User-level Threads

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 User-level Threads Advantages Fast Context Switching: User level threads are implemented using user level thread libraries, rather than system calls, hence no call to OS and no interrupts to kernel When a thread is finished running for the moment, it can call thread_yield. This instruction (a) saves the thread information in the thread table, and (b) calls the thread scheduler to pick another thread to run. The procedure that saves the local thread state and the scheduler are local procedures, hence no trap to kernel, no context switch, no memory switch, and this makes the thread scheduling very fast. Customized Scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Kernel-level Threads Kernel can schedule threads in addition to processes. Multiple threads of a process can run simultaneously on multiple CPUs. Synchronization more efficient than for processes (but less than for user-level threads). Kernel-level threads can make blocking I/O calls without blocking other threads of same process

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Trade-offs? Kernel thread packages Each thread can make blocking I/O calls Can run concurrently on multiple processors Threads in User-level Fast context switch Customized scheduling

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Hybrid Implementations (Solaris) Multiplexing user-level threads onto kernel-level threads

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Pthread Operations (review) POSIX functiondescription pthread_cancel terminate another thread pthread_create create a thread pthread_detach set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_kill send a signal to a thread pthread_join wait for a thread pthread_self find out own thread ID

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Creating a Thread When a new thread is created it runs concurrently with the creating process. When creating a thread you indicate which function the thread should execute.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Creating a thread with pthread A thread is created with int pthread_create( pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); The creating process (or thread) must provide a location for storage of the thread id. The third parameter is just the name of the function for the thread to run. The last parameter is a pointer to the arguments.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 The Restrict Keyword One of the new features in the recently approved C standard C99 This qualifier can be applied to a data pointer to indicate that During the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. It enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer Can avoid pointer aliasing problem

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

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Example 2: Passing Parameters to a Thread #include #define NUM_THREADS 5 void *PrintHello(void *ptr) { char *filename; int j; filename = (char *) ptr; while (1) { printf("Hello World! It's me, thread %s!\n", filename); for (j=1; j++; j<500); } pthread_exit(NULL); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Example 2: Passing Parameters to a Thread int main (int argc, char *argv[]) { pthread_t thread[100]; int err_code, i=0; char *filename; printf ("Enter thread name at any time to create thread\n"); while (1) { filename = (char *) malloc (80*sizeof(char)); scanf ("%s", filename); printf("In main: creating thread %d\n", i); err_code = pthread_create(&thread[i], NULL, PrintHello, (void *)filename); if (err_code){ printf("ERROR code is %d\n", err_code); exit(-1); } else i++; } pthread_exit(NULL); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Example 3: Files void *PrintHello(void *ptr) { FILE *file; char *filename; char textline[100]; int j; filename = (char *) ptr; printf("Hello World! Opening %s!\n", filename); file = fopen(filename, "r"); if (file != NULL) { while(fscanf(file, "%s\n", textline) != EOF) printf ("%s\n", textline); } fclose(file); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Exiting and Cancellation Question: If a thread calls exit(), what about other threads in the same process? When does a process terminate?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Exiting and Cancellation Question: If a thread calls exit(), what about other threads in the same process? A process can terminate when: it calls exit directly one of its threads calls exit it returns from main() it receives a termination signal In any of these cases, all threads of the process terminate.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Thread Exit When a thread is done, it can return from its first function (the one used by pthread_create) or it can call pthread_exit void pthread_exit(void *value_ptr); Common uses: Return value is often a pointer to the original struct or a malloc’d struct (memory must be free’d by joining thread) Use the heap not the stack!!!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Cancel that thread! One thread can request that another exit with pthread_cancel int pthread_cancel(pthread_t thread); The pthread_cancel returns after making the request.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Thread Attributes Create an attribute object (initialize it with default properties) Modify the properties of the attribute object Create a thread using the attribute object The object can be changed or reused without affecting the thread The attribute object affects the thread only at the time of thread creation

Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 Settable Properties of Thread Attributes propertyfunction attribute objects pthread_attr_destroy pthread_attr_init detach state pthread_attr_getdetachstate pthread_attr_setdetachstate stack pthread_attr_getguardsize pthread_attr_setguardsize pthread_attr_getstack pthread_attr_setstack scheduling pthread_attr_getinheritsched pthread_attr_setinheritsched pthread_attr_getschedparam pthread_attr_setschedparam pthread_attr_getschedpolicy pthread_attr_setschedpolicy pthread_attr_getscope pthread_attr_setscope

Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 Example: Create a detached thread int e, fd; pthread_attr_t tattr; pthread_t tid; if(e = pthread_attr_init(&tattr)) fprintf(stderr, "Failed to create attribute object: %s\n", strerror(e)); else if(e = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED)) fprintf(stderr, "Failed to set attribute state to detached: %s\n", strerror(e)); else if(e = pthread_create(&tid, &tattr, data, &fd)) fprintf(stderr, "Failed to create thread: %s\n", strerror(e));

Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 Zombies, Thread Detach & Join Call pthread_join() or pthread_detach() for every thread that is created joinable so that the system can reclaim all resources associated with the thread Failure to join or to detach threads  memory and other resource leaks until the process ends

Copyright ©: Nahrstedt, Angrave, Abdelzaher 26 Detaching a Thread int pthread_detach(pthread_t threadid); Indicate that system resources for the specified thread should be reclaimed when the thread ends If the thread is already ended, resources are reclaimed immediately This routine does not cause the thread to end A detached thread’s thread ID is undetermined Threads are detached after a pthread_detach() call after a pthread_join() call if a thread terminates and the PTHREAD_CREATE_DETACHED attribute was set on creation

Copyright ©: Nahrstedt, Angrave, Abdelzaher 27 How to make a Thread Detached void *processfd(void *arg); int error; int fd pthread_t tid; if (error = pthread_create(&tid, NULL, processfd, &fd)) { fprintf(stderr, "Failed to create thread: %s\n", strerror(error)); } else if (error = pthread_detach(tid)){ fprintf(stderr, "Failed to detach thread: %s\n", strerror(error)); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 28 How a thread can detach itself void *runnger(void *arg) { … if (!pthread_detach( pthread_self()) ) return NULL; … }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 29 “Waiting” on a Thread: pthread_join() int pthread_join(pthread_t thread, void** retval); pthread_join() is a blocking call on non- detached threads It indicates that the caller wishes to block until the thread being joined exits You cannot join on a detached thread, only non-detached threads (detaching means you are NOT interested in knowing about the threads exit)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 30 Pthread_join int error; int *exitcodep; pthread_t tid; if (error = pthread_join(tid, &exitcodep)){ fprintf(stderr, "Failed to join thread: %s\n", strerror(error)); } else { fprintf(stderr, "The exit code was %d\n", *exitcodep); }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 31 Thread scheduling int pthread_attr_getscope(const pthread_attr_t *restrict attr, int *restrict contentionscope); int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope); The contentionscope can be PTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM. The scope determines whether the thread competes with other threads of the process or with other processes in the system.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 32 Create a thread that contends with other processes pthread_attr_init(&tattr)) pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) thread_create(&tid, &tattr, myfunction, &myptr))