Download presentation
Presentation is loading. Please wait.
Published byLenard Derek Peters Modified over 5 years ago
1
Chapter 4: Threads CSS503 Systems Programming
Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell CSS503 Chapter 4: Thraeds
2
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
3
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
4
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
5
Benefit 1: Responsiveness Multithreaded Server
Web Server Clients DB1 DB2 CGI http pages CSS503 Chapter 4: Thraeds
6
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
7
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
8
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
9
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
10
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
11
Many-to-Many Model Covering the shortage of the previous two models
Examples: Windows XP’s fiber library, AIX Pthreads CSS503 Chapter 4: Thraeds
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.