Concurrent Server Design Multiple- Threads vs Single-thread Chuan-Ming LIu Computer Science and Information Engineering Spring 2004, NTUT TAIWAN CSIE NTUT Taiwan
1 CSIE, NTUT, Taiwan Concurrent Server Design Multiple-Threads v.s. Single-thread Chuan-Ming Liu Computer Science and Information Engineering Spring 2004, NTUT TAIWAN
●● Examples o Using Threads for Concurrency(TCP) o Single-Thread, Currency Servers(TCP) SIE NTUT. Taiwan
2 CSIE, NTUT, Taiwan Examples Using Threads for Concurrency (TCP) Single-Thread, Currency Servers (TCP)
Using Threads for Concurrency (TCp
Using Threads for Concurrency (TCP)
。 Multiple Threads o Master thread for connection requests o Slave threads for handling communication th clients o Thread of execution an abstraction of independent computation Single process can contain one or more threads 4 SIE NTUT. Taiwan
4 CSIE, NTUT, Taiwan Multiple Threads Master thread for connection requests Slave threads for handling communication with clients Thread of execution: an abstraction of independent computation Single process can contain one or more threads
●°。 Threads vs. Processes o Creation of a new process using forko is expensive( time memory) o a thread (sometimes called a lightweight process )does not require lots of memory or startup time 5 SIE NTUT. Taiwan
5 CSIE, NTUT, Taiwan Threads vs. Processes Creation of a new process using fork( ) is expensive (time & memory). A thread (sometimes called a lightweight process) does not require lots of memory or startup time
●● Multiple Threads o each process can include many threads o All threads of a process share o memory (program code and global data o open file/socket descriptors o signal handlers and signal dispositions o working environment(current directory,user ID, etc.) 6 SIE, NTUT, Taiwan
6 CSIE, NTUT, Taiwan Multiple Threads Each process can include many threads. All threads of a process share: ⚫ memory (program code and global data) ⚫ open file/socket descriptors ⚫ signal handlers and signal dispositions ⚫ working environment (current directory, user ID, etc.)
°°| Thread- Specific Resources o Each thread has it's own ● Thread id( Integer) ● Stack ● Registers ● Program Counter o Threads within the same process can communicate using shared memor Must be done carefully! SIE NTUT. Taiwan
7 CSIE, NTUT, Taiwan Thread-Specific Resources Each thread has it’s own: ⚫ Thread ID (integer) ⚫ Stack ⚫ Registers ⚫ Program ⚫ Counter Threads within the same process can communicate using shared memory. Must be done carefully!
●。 Posix Threads We will focus on posix threads -most widely supported threads programming APl SIE, NTUT, Taiwa
8 CSIE, NTUT, Taiwan Posix Threads We will focus on Posix Threads - most widely supported threads programming API
●。f。rk() Process A Global fork( Variables Process B Code Global Variables Stack Code Stack SIE NTUT. Taiwan
9 CSIE, NTUT, Taiwan fork() Process A Global Variables Code Stack Process B Global Variables Code Stack fork()
●● Thread creation pthread create( pthread t *tid, const pthread attr t xattr V。id*(*func)(Void*) v。id*arg); Func is the function to be called When func( returns the thread is terminated 10 SIE NTUT. Taiwan
10 CSIE, NTUT, Taiwan Thread Creation pthread_create( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void *), void *arg); Func() is the function to be called. When func() returns the thread is terminated