●● 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