Presentation is loading. Please wait.

Presentation is loading. Please wait.

June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.

Similar presentations


Presentation on theme: "June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads."— Presentation transcript:

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

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

3 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 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 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 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 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 8 str_cli Function Using Threads (1) str_cli thread version Figure 26.1 Recording str_cli to use threads.

9 9 str_cli Function Using Threads (2) unpthread.h header file : 일반 unp.h 와 Posix.1 그리고 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.

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

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

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

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

14 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 15 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… Figure 26.7 Possible implementation of thread-specific data.

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

17 17 Thread-specific data (4) Scenario 1.A process is started and multiple threads are started 2.Call readline  pthread_key_create  The system finds the first unused key structure and returns its index(0-127) 3.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]) 4.Another thread, say thread n, calls readline, perhaps while threads 0 is still executing within readline. 5.readline calls pthreads_getspecific to fetch the pkey[1] pointer for this thread, and a null pointer is returned. 6.Thread n continues executing in readline, using and modifying its own thread-specific data.

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

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

20 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 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 22 Example: readline Function Using Thread-Specific Data (1) First part of thread-safe readline function Figure 26.11 First part of thread-safe readline function. Destructor One-time function Rline strcture

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

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

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

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

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

28 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 29 Mutexes : Mutual Exclusion (2) Example program Figure 26.17 Two threads that increment a global variable incorrectly.

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

31 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 32 Mutexes : Mutual Exclusion (5) Corrected version of Figure 26.17 using mutex Figure 26.18 Corrected version of Figure 26.17 using a mutex to protect the share variable.

33 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 34 Condition variables (2) 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

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

36 36 Condition variables (4)

37 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 38 Web Client and Simultaneous Connections (Continued) (4) Figure 26.19 Main processing loop of main function. If possible, create another thread Wait for thread to terminate Handle terminated thread

39 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.


Download ppt "June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads."

Similar presentations


Ads by Google