+ All Categories
Home > Documents > June-Hyun, Moon Computer Communications LAB., Kwangwoon University [email protected] Chapter 26 -...

June-Hyun, Moon Computer Communications LAB., Kwangwoon University [email protected] Chapter 26 -...

Date post: 03-Jan-2016
Category:
Upload: camilla-gallagher
View: 229 times
Download: 0 times
Share this document with a friend
Popular Tags:
39
June-Hyun, Moon Computer Communications LAB., Kwangwoon University [email protected] Chapter 26 - Threads
Transcript
Page 1: June-Hyun, Moon Computer Communications LAB., Kwangwoon University imp@kw.ac.kr Chapter 26 - Threads.

June-Hyun, Moon

Computer Communications LAB.,

Kwangwoon University

[email protected]

Chapter 26 - Threads

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

2

Introduction

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

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

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.

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

4

Thread (2)

Posix threadcalled Pthreadswere standardized in 1995(Posix. 1c standard)covers posix thread in this chapterAll Pthread functions begin with pthread_.

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

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.

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

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.

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

7

Basic Thread Functions (3)

pthread_detach Function A thread is either joinable or detached. A detached thread is li

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

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

8

str_cli Function Using Threads (1)

str_cli thread version

Figure 26.1 Recording str_cli to use threads.

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

9

str_cli Function Using Threads (2)

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.

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

10

TCP Echo Server Using Threads

Create thread

Thread function

Figure 26.3 TCP echo server using threads (see also Exercise 26.5)

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

11

Passing Arguments to New Threads (1)

Problem : shared variable

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

12

Passing Arguments to New Threads (2)

More efficient argument passing

Figure 26.4 TCP echo server using threads with more portable argument passing.

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

13

Thread-safe function

Figure 26.5 Threads-safe functions.

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

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

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

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.

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

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.

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

17

Thread-specific data (4) Scenario

1. A process is started and multiple threads are started2. Call readline

pthread_key_create The system finds the first unused key structure and returns its in

dex(0-127)

3. readline calls pthread_getspecific : the return value is a null pointer and t

hen, 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.

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

18

Thread-specific data (5)

Figure 26.9 Associating malloced region with thread-specific data pointer.

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

19

Thread-specific data (6)

Figure 26.10 Data structures after thread n initializes ins thread-specific data.

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

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.

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

21

Thread-specific data (8)

The pthread_getspecific and pthread_setspecific functions are used to fetch and store the value associated with a key.

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

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

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

23

Example: readline Function Using Thread-Specific Data (2)

Second part of thread-safe readline function

my_read function

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

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

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

25

Web Client and Simultaneous Connections (Continued) (1)

Thread ID

Global Variable

home_page function

Figure 26.13 Globals and start of mian funtion.

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

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 threadto terminate

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

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

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

28

Mutexes : Mutual Exclusion (1)

Consider the following scenario: Thread A is running and it loads the value of nconn(3) into a r

egister. The system switches threads from A to B. A’s registers are sa

ved, 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 re

gister are restored and A continues where it left off, at the second machine instruction in the three-instructions sequence

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

29

Mutexes : Mutual Exclusion (2)

Example program

Figure 26.17 Two threads that increment a global variable incorrectly.

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

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

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

31

Mutexes : Mutual Exclusion (4)

Solution To protect the shared variable with a mutex and access the v

ariable only when we hold the mutex. A mutex is a variable of type pthread_mutex_t. Two function of mutex – lock and unlock

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

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.

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

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.

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

34

Condition variables (2)

The main loop never goes to sleep,

and it checks ndone every time.

This is called polling and is conside

red a waste of CPU time

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

35

Condition variables (3)

Mutex and condition variables Mutex : provides mutual exclusion Condition variable : provides a signaling mechanism

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

36

Condition variables (4)

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

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.

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

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

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

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 serv

ers.

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.


Recommended