Chapter 4: Threads CSS503 Systems Programming

Slides:



Advertisements
Similar presentations
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Advertisements

Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
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.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
CSS434 OS Support1 CSS434 Operating System Support Textbook Ch7 Professor: Munehiro Fukuda.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CSS430 Threads Textbook Chapter 4
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Typical Program Processor utilization?
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
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.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
Threads Overview Benefits, User and Kernel Threads.
Chapter 4 – Thread Concepts
CSS 430: Operating Systems - Threads
OPERATING SYSTEM CONCEPT AND PRACTISE
CSS490 Process Management
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS Threads
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Jonathan Walpole Computer Science Portland State University
Chapter 4: Threads.
Chapter 4: Threads.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4 Threads “Most modern applications are multithreaded”
Chapter 4: Threads.
Chapter 1: Introduction CSS503 Systems Programming
Chapter 4: Threads.
Presentation transcript:

Chapter 4: Threads CSS503 Systems Programming Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell CSS503 Chapter 4: Thraeds

Thread Concepts Process-oriented computing Multithreaded computing code files data code files data thread code files data process code files data code files data process Operating System Operating System CSS503 Chapter 4: Thraeds

Single vs Multithreaded Processes Carries everything such as an address space, code, data, and files Heavyweight in terms of creation/termination/context switching Threads Shares an address space, code, data and files if they belong to the same process Lightweight in terms of creation/termination/context switching Then, what is uniquely carried by each thread? CSS503 Chapter 4: Thraeds

Benefits Responsiveness Deadlock avoidance Scalability, (i.e., Utilization of multiprocessor architecture) Resource sharing and economy Threads share an address space, files, code, and data Avoid resource consumption Perform much a faster context swtich CSS503 Chapter 4: Thraeds

Benefit 1: Responsiveness Multithreaded Server Web Server Clients DB1 DB2 CGI http pages CSS503 Chapter 4: Thraeds

Benefit 2: Deadlock avoidance Process 1 Process 2 Bounded buffer send send Bounded buffer receive receive A sequence of send and then receive does not work! Send and receive must be executed concurrently with threads. CSS503 Chapter 4: Thraeds

Benefit 3: Utilization of multicore multiprocessor architecture time = t time = t + x Place code, files and data in the main memory. Distribute threads to each of CPUs, and Let them execute in parallel. time = t + 2x code files data CSS503 Chapter 4: Thraeds

User and Kernel Threads User Threads Thread Management Done by User-Level Threads Library Examples - POSIX Pthreads - Win32 threads - Java threads Kernel does not care how many user threads exist. Kernel Threads Supported by the Kernel - Linux - Windows XP/2000 - Solaris lightweight processes CSS503 Chapter 4: Thraeds

Many-to-One Model Advantage: fast Many user-level threads mapped to a single kernel thread. Used on systems that do not support kernel threads. Examples: Solaris Green Threads, GNU Portable Threads Advantage: fast Disadvantage: 1. Non preemption or very complicated to handle preemption and I/O 2. No use of MP architecture CSS503 Chapter 4: Thraeds

One-to-One Model Advantage: everything is supported by OS. Each user-level thread maps to kernel thread. Examples - Windows NT/XP/2000 - Linux - Solaris 9 or later Advantage: everything is supported by OS. Using MP architecture. Disadvantage: slower than many-to-one model. too many threads do not help. CSS503 Chapter 4: Thraeds

Many-to-Many Model Covering the shortage of the previous two models Examples: Windows XP’s fiber library, AIX Pthreads CSS503 Chapter 4: Thraeds

Process Structure Kernel schedules each light-weight process separately. Traditional UNIX Process Multithreaded Process Process ID Process ID UID GID EUID EGID UID GID EUID EGID Directory Entry Directory Entry TTY TTY Signal Dispatch Table Signal Dispatch Table Memory Map Memory Map LWP1 LWP2 stdin priority stdin LWP ID LWP ID 1 stdout Intr. mask 1 stdout priority priority 2 stderr registers 2 stderr Intr. mask Intr. mask 3 3 registers registers CPU Status File Descriptors File Descriptors CSS503 Chapter 4: Thraeds 11

Thread Structures Thead library runs in a user space. Process JVM Java Threads Thead library runs in a user space. All threads shared the same address space. Each thread maintains its own information and stack space. Once a thread is mapped to a LWP, it runs on a CPU. thread info Thread3 stack red zone thread info Thread2 stack red zone Thread1 thread info stack red zone Thread ID Thread ID Thread ID priority priority priority PC PC PC SP SP SP Other regs Other regs Other regs heap Thread1 Thread2 Thread3 code + global data Pthreads Win32 threads Thread to LWP mapping User mode Signal Dispatch Table Memory Map File Descriptors Directory Entry UID GID EUID EGID Process ID TTY LWP1 priority Intr. mask registers LWP ID LWP2 priority Intr. mask registers LWP ID Kernel mode CSS503 Chapter 4: Thraeds 12

Discussion 1 Why can threads perform their context switch much faster than processes? What data structure must threads carry by their own resource? What data structure can they share? What if we use processes rather than threads in the following cases we discussed. Responsiveness Deadlock avoidance Utilization of multiprocessor architecture CSS503 Chapter 4: Thraeds

Threads v.s. Multiple Processes Maybe, we can implement a server of multiple processes? Server consisting of Threads Multiple Processes Creation Light overhead Heavy overhead Context switch Cheap Expensive Remote object sharing Easy Needs shared memory Priority Easy to change Not quite dynamically changeable Protection Vulnerable Safe CSS503 Chapter 4: Thraeds

Thread Implementation Library and Class Functions Java Pthread Solaris Thread Create a new thread new Thread( ) pthread_create( ) thr_create( ) Terminate myself destroy( ) pthread_exit( ) thr_exit( ) Wait for a given thread to be terminated join( ) pthread_join( ) thr_join( ) Terminate a given thread stop( ) pthread_kill( ) thr_kill( ) Get my thread object currentThread( ) pthread_self( ) thr_self( ) Relinquish CPU and put myself in a ready queue yield( ) thr_yield( ) Suspend a given thread suspend( ) thr_suspend( ) Resume a give thread resume( ) thr_continue( ) Get my current running priority getPriority( ) pthread_getschedparam( ) thr_getprio( ) Change my current running priority setPriority( ) pthread_setschedparam( ) thr_setprio( ) Wait for another thread to signal me wait( ) pthread_cond_wait( ) cond_wait( ) Signal another thread waiting for me signal( ) pthread_cond_signal( ) cond_signal( ) CSS503 Chapter 4: Thraeds

Thread Implementation PThread Example #include <iostream> #include <string> using namespace std; #include <pthread.h> #include <unistd.h> void* thread_func( void *param ) { for ( int i = 0; i < 5; i++ ) { sleep( 2 ); cout << "I'm a slave: " << *( (string *)param ) << endl; } return NULL; void main( int argc, char *argv[] ) { pthread_t child; string arg; cout << "enter message: "; cin >> arg; pthread_create( &child, NULL, thread_func, (void *)&arg ); for ( int i = 0; i < 10; i++ ) { sleep( 1 ); cout << "I'm a master: " << arg << endl; pthread_join( child, NULL ); cout << "Master synched with slave" << endl; Compilation and Execution Source Code $ g++ thread.cpp -lpthread $ a.out enter message: hello! I'm a master: hello! I'm a slave: hello! Master synched with slave $ CSS503 Chapter 4: Thraeds

Thread Implementation Java Thread Example MyThread.java public class MyThread { public static void main( String args[] ) { String arg = args[0]; ThreadFunc child = new ThreadFunc( arg ); child.start( ); for ( int i = 0; i < 10; i++ ) { try { Thread.sleep( 1000 ); } catch ( InterruptedException e ) { }; System.out.println( "I'm a master: " + arg ); } child.join( ); System.out.println( "Master synched with slave" ); public class ThreadFunc extends Thread { public ThreadFunc( String param ) { this.param = param; public void run( ) { for ( int i = 0; i < 5; i++ ) { Thread.sleep( 2000 ); System.out.println( "I'm a slave: " + param ); String param; Compilation and Execution $ ls MyThread.java ThreadFunc.java $ javac MyThread.java $ java MyThread hello! I'm a master: hello! I'm a slave: hello! Master synched with slave $ ThreadFunc.java CSS503 Chapter 4: Thraeds

Threading Issues Semantics of fork( ) and exec( ) Should all threads be duplicated? Thread cancellation Asynchronous cancellation Deferred cancellation Signal handling Synchronous: illegal memory access Asynchronous: a signal ^c from a shell Thread pools On-demand thread creation A pool of threads upon a process creation CSS503 Chapter 4: Thraeds

Thread Cancellation Asynchronous Deferred #include <iostream> using namespace std; void thread_func( void *arg ) { while ( true ) { // do something ; } cout << "terminated" << endl; int main( ) { pthread_t tid; pthread_create( &tid, NULL, thread_func, NULL ); sleep( 10 ); pthread_kill( tid, SIGKILL ); pthread_join( tid, NULL ); cout << "joined" << endl; $ ./a.out Killed What if a child thread acquired a lock that the parent needs later? Deferred #include <iostream> using namespace std; void *thread_func( void *arg ) { bool &alive = *(bool *)arg; while ( alive ) { // do something ; } cout << "terminated" << endl; int main( ) { pthread_t tid; bool alive = true; pthread_create( &tid, NULL, thread_func, (void *)&alive ); sleep( 10 ); alive = false; pthread_join( tid, NULL ); cout << "joined" << endl; $ ./a.out terminated joined CSS503 Chapter 4: Thraeds

Signal Handling Synchronous: illegal memory access void sigint_func( int sig ) { // print out my id pthread_t tid = pthread_self( ); printf( "%u caught SIGINT.\n", tid ); exit( 0 ); } void sigfpe_func( int sig ) { printf( "%u caught SIGFPE.\n", tid ); pthread_exit( NULL ); void *thread_func( void *arg ) { int logical_id = *(int *)arg; *(int *)arg = -1; pthread_t child_tid = pthread_self( ); printf( "child[%d]'s tid = %u\n", logical_id, child_tid ); // have sigint_func catch ^C signal( SIGINT, sigint_func ); // have sigfpe_func catch SIGFPE signal( SIGFPE, sigfpe_func ); while ( true ) { int i = 1, div = 0; // have child[MAX - 1] cause a zero division error. if ( logical_id == MAX - 1 ) { div = i / div; int main( ) { // print out my id pthread_t main_tid = pthread_self( ); printf( "main's pthread_t = %u\n", main_tid ); // create children pthread_t tid[MAX]; for ( int i = 0; i < MAX; i++ ) { int id = i; pthread_create( &tid[i], NULL, thread_func, (void *)&id ); while ( id == i ); } while ( true ); Synchronous: illegal memory access received by the same thread Asynchronous: a signal ^c from a shell received by the main thread (Textbook: the first thread found) CSS503 Chapter 4: Thraeds

Thread Pools The worker pool RMI Per-request threads CSS503 Client 1 Pick up a request RMI server stub Client 2 accept Client 1 queue RMImethod1( ) { : pthread_create( ); pthread_kill( ); return; } Client n Client 2 Per-request threads Client 1 Spawn for each request accept Client 2 Can it kill them at once? Client n CSS503 Chapter 4: Thraeds

Discussions 2 Why does the latest Java compiler deprecate the use of resume, suspend, and stop? Consider an undesired situation incurred when those three functions are used. If we omit a join( ) statement in both the Pthread and Java versions (in p16 and p17), what happened to them. Do we observe any difference between them? Why does Linux deliver asynchronous signals to the main thread? CSS503 Chapter 4: Thraeds