1 / 39

Chapter 26 - Threads

Chapter 26 - Threads. June-Hyun, Moon Computer Communications LAB., Kwangwoon University imp@kw.ac.kr. Introduction. Problem Fork is expensive Memory copy Duplication of all file descriptor Copy-on write IPC is required to pass information. Thread (1).

briana
Download Presentation

Chapter 26 - Threads

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 26 - Threads June-Hyun, Moon Computer Communications LAB., Kwangwoon University imp@kw.ac.kr

  2. Introduction • Problem • Fork is expensive • Memory copy • Duplication of all file descriptor • Copy-on write • IPC is required to pass information

  3. Thread (1) • Threads are called lightweight processes • Thread creation can be 10-100 times faster than process creation • All threads within a process share the same global memory.

  4. Thread (2) • Posix thread • called Pthreads • were standardized in 1995(Posix. 1c standard) • covers posix thread in this chapter • All Pthread functions begin with pthread_.

  5. Basic Thread Functions (1) • pthread_create Function • When a program is started by exec, a single thread is created, called the initial thread or main thread. • Additional thread are created by pthread_create. • The address of the function is specified as the func argument, and this function is called with a single pointer argument, arg.

  6. Basic Thread Functions (2) • pthread_join Function • wait for a given thread to terminate by calling pthread_join. • pthread_join is similar to waitpid. • pthread_self Function • Each thread has an ID that identifies it within a given process.

  7. Basic Thread Functions (3) • pthread_detach Function • A thread is either joinable or detached. A detached thread is like a daemon process. when it terminates, all its resource are released and we cannot wait for it to terminate. • pthread_exit Function • one way for a thread to terminate is to call pthread_exit.

  8. Figure 26.1 Recording str_cli to use threads. str_cli Function Using Threads (1) • str_cli thread version

  9. unpthread.h header file : 일반 unp.h와 Posix.1 <pthread.h> 그리고 pthread_로 시작하는 모든 Pthread_xxx 함수를 정의하는 원형이 포함됨 Save arguments in externals Create new thread Main thread loop : copy socket to standard output Terminate Copyto thread Figure 26.2 str_cli function using threads. str_cli Function Using Threads (2)

  10. Create thread Thread function Figure 26.3 TCP echo server using threads (see also Exercise 26.5) TCP Echo Server Using Threads

  11. Passing Arguments to New Threads (1) • Problem : shared variable

  12. Figure 26.4 TCP echo server using threads with more portable argument passing. Passing Arguments to New Threads (2) • More efficient argument passing

  13. Figure 26.5 Threads-safe functions. Thread-safe function

  14. Thread-specific data (1) • Problem of Multi-thread programming • All threads within a process share the same address space. • static or extern (figure 3.16) • it may be read and written by all threads within process. • Solution : converting existing functions to run in a threads environment • Use thread-specific data • Change the calling sequence • Ignore the speedup and go back to the older version

  15. Figure 26.7 Possible implementation of thread-specific data. Thread-specific data (2) • Thread-specific data • Common technique for making an existing function thread-safe. • A limited number of thread-specific data items. (posix.1 : no less than 128 per process) • The system maintains one array of structures per process, which we call key structure, as following this…

  16. Figure 26.8 Information maintained by the system about each thread. Thread-specific data (3) • Pthread structure • Information maintained by the system about each thread.

  17. Thread-specific data (4) • Scenario • A process is started and multiple threads are started • Call readline • pthread_key_create • The system finds the first unused key structure and returns its index(0-127) • readline • calls pthread_getspecific : the return value is a null pointer and then, readline calls pthread_setspecific to set the thread-specific data pointer(pkey[1]) • Another thread, say thread n, calls readline, perhaps while threads 0 is still executing within readline. • readline calls pthreads_getspecific to fetch the pkey[1] pointer for this thread, and a null pointer is returned. • Thread n continues executing in readline, using and modifying its own thread-specific data.

  18. Figure 26.9 Associating malloced region with thread-specific data pointer. Thread-specific data (5)

  19. Figure 26.10 Data structures after thread n initializes ins thread-specific data. Thread-specific data (6)

  20. Thread-specific data (7) • pthread_once is normally called every time is called that uses thread-specific data, but pthread_once uses the value in the variable pointed to by onceptr to guarantee that the init function is called only one time per process. • pthread_key_create must be called only one time for a given key within a process.

  21. Thread-specific data (8) • The pthread_getspecific and pthread_setspecific functions are used to fetch and store the value associated with a key.

  22. Destructor One-time function Rline strcture Figure 26.11 First part of thread-safe readline function. Example: readline Function Using Thread-Specific Data (1) • First part of thread-safe readline function

  23. my_read function Example: readline Function Using Thread-Specific Data (2) • Second part of thread-safe readline function

  24. Allocate thread-specific data Fetch thread-specific data pointer Figure 26.12 Second part of thread-safe readline function. Example: readline Function Using Thread-Specific Data (3) • Second part of thread-safe readline function

  25. Global Variable Thread ID home_page function Figure 26.13 Globals and start of mian funtion. Web Client and Simultaneous Connections (Continued) (1)

  26. If possible, create another thread Wait for any thread to terminate Figure 26.14 Main processing loop of main function. Web Client and Simultaneous Connections (Continued) (2)

  27. Create TCP socket, establish connect Write request to server Read sever’s reply Figure 26.15 do_get_read function. Web Client and Simultaneous Connections (Continued) (3)

  28. Mutexes : Mutual Exclusion (1) • Consider the following scenario: • Thread A is running and it loads the value of nconn(3) into a register. • The system switches threads from A to B. A’s registers are saved, and B’s register are restored. • Thread B executes the three instructions corresponding to the C expression nconn--, storing the new value of 2. • Sometime later the system switches thread from A to A. A’s register are restored and A continues where it left off, at the second machine instruction in the three-instructions sequence

  29. Figure 26.17 Two threads that increment a global variable incorrectly. Mutexes : Mutual Exclusion (2) • Example program

  30. this continues as thread 4 executes thread 5 now executes this continues as thread 5 executes thread 4 now executes, stored value is wrong Figure 26.16 Output from program in Figure 26.17. Mutexes : Mutual Exclusion (3) • Program result

  31. Mutexes : Mutual Exclusion (4) • Solution • To protect the shared variable with a mutex and access the variable only when we hold the mutex. • A mutex is a variable of type pthread_mutex_t. • Two function of mutex – lock and unlock

  32. Figure 26.18 Corrected version of Figure 26.17 using a mutex to protect the share variable. Mutexes : Mutual Exclusion (5) • Corrected version of Figure 26.17 using mutex

  33. Condition variables (1) • A mutex is fine to prevent simultaneous access to a shared variable, but we need something else to let us go to sleep waiting for some condition to occur. • We cannot call the Pthread function until we know that a thread has terminated • declare a global that counts the number of terminated threads and protect it with a mutex.

  34. The main loop never goes to sleep, and it checks ndone every time. This is called polling and is considered a waste of CPU time Condition variables (2)

  35. Condition variables (3) • Mutex and condition variables • Mutex : provides mutual exclusion • Condition variable : provides a signaling mechanism

  36. Condition variables (4)

  37. Other functions • pthread_cond_broadcast will wake up all threads that are blocked on the condition variable. • pthread_cond_timewait lets a thread place a limit on how long it will block.

  38. Web Client and Simultaneous Connections (Continued) (4) If possible, create another thread Wait for thread to terminate Handle terminated thread Figure 26.19 Main processing loop of main function.

  39. Summary • The creation of a new thread is normally faster of a new process with fork. • This alone can be an advantage in heavily used network servers. • All threads in a process share global variable and descriptor, allowing this information to be shared between different threads. • So we must use are Mutex and condition variable. • When writing functions that can be called by threads applications, these functions must be thread-safe. • Thread-specific data is one technique that helps with this, and we the showed an example with our readline function.

More Related