Mobile Computing & Software Engineering Lab Concurrency in Clients Prof. Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Taipei, TAIWAN NTUT, TAIWAN 1
Mobile Computing & Software Engineering Lab Introduction This chapter considers the issue of concurrency in client software It shows an example client that illustrates concurrent operation NTUT, TAIWAN 2
Mobile Computing & Software Engineering Lab The Advantage of Concurrency Servers use concurrency for two main reasons: Concurrency can improve the observed response time (and therefore the overall throughput to all clients) Concurrency can eliminate potential deadlocks NTUT, TAIWAN 3
Mobile Computing & Software Engineering Lab The Advantage of Concurrency (cont.) Concurrency does have advantages in clients: Concurrent implementations can be easier to program because they separate functionality into conceptually separate components Concurrent implementations can be easier to maintain and extend because they make the code modular NTUT, TAIWAN 4
Mobile Computing & Software Engineering Lab The Advantage of Concurrency (cont.) Concurrent clients can contact several servers at the same time, either to compare response time or merge the results the servers return Concurrency can allow the user to change parameters, inquire about the client status, or control processing dynamically NTUT, TAIWAN 5
Mobile Computing & Software Engineering Lab The Advantage of Concurrency (cont.) This chapter will focus on the idea of interacting with multiple servers at the same time The key advantage of using concurrency in clients lies in asynchrony – allows a client to handle multiple tasks simultaneously without imposing a strict execution order on them NTUT, TAIWAN 6
Mobile Computing & Software Engineering Lab The Motivation for Exercising Control One possible use of asynchrony arises from the need to separate control functions from normal processing A user who invokes a client may have little or no idea how long it will take to receive a response or how large that response will be NTUT, TAIWAN 7
Mobile Computing & Software Engineering Lab The Motivation for Exercising Control (cont.) An appropriately designed concurrent client can permit the user to continue to interact with the client while the client waits for a response The user can find out whether any data has been received, choose to send a different request, or to terminate the communication gracefully NTUT, TAIWAN 8
Mobile Computing & Software Engineering Lab The Motivation for Exercising Control (cont.) For example, an concurrent implementation can read and process input from the user’s keyboard or mouse concurrently with database search NTUT, TAIWAN 9
Mobile Computing & Software Engineering Lab Concurrent Contact with Multiple Servers Concurrency can allow a single client to contact several servers at the same time and to report to the user as soon as it receives a response from any of them Consider how concurrency can enhance a client that uses ECHO to measure throughput Because it makes all measurements at the same time, they are faster than a non-concurrent client, and are all affected equally by the loads on the CPU and the local network NTUT, TAIWAN 10
Mobile Computing & Software Engineering Lab Implementing Concurrent Clients Like concurrent servers, most concurrent client implementations follow one of the two basic approaches: A multi-thread implementation that each thread handles one function A single thread implementation that uses select to handle multiple I/O events asynchronously NTUT, TAIWAN 11
Mobile Computing & Software Engineering Lab Implementing Concurrent Clients (cont.) As Fig. 17.1 illustrates, multiple threads allow a connection-oriented client to separate I/O processing An input thread reads from standard input, formulates requests, and sends them to the server over the TCP connection A separate output thread receives responses from the server and writes them to the standard output A control thread accepts commands from the user that control processing NTUT, TAIWAN 12
Mobile Computing & Software Engineering Lab Figure 17.1 NTUT, TAIWAN 13
Mobile Computing & Software Engineering Lab Single-Thread Implementation Fig. 17.2 shows the process structure used to provide apparent concurrency in a single thread, connection-oriented client (Algorithm 8.5, the examples in Ch 13 through 15) If the input descriptor becomes ready, the client reads the input and either stores it for later use or acts on it immediately If a TCP socket becomes ready for output, the client prepares and sends a request across a TCP connection NTUT, TAIWAN 14
Mobile Computing & Software Engineering Lab Figure 17.2 NTUT, TAIWAN 15
Mobile Computing & Software Engineering Lab Algorithm 8.5 NTUT, TAIWAN 16
Mobile Computing & Software Engineering Lab Single-Thread Implementation (cont.) If a TCP socket becomes ready for input, the client reads the response that the server has sent and handles it The client reads input or responses from the server at whatever rate they are generated The client will continue to read and honor control commands even if the server fails to respond NTUT, TAIWAN 17
Mobile Computing & Software Engineering Lab Single-Thread Implementation (cont.) A single thread client can become deadlocked if it invokes a system function that blocks The programmer must be careful to ensure that the client does not block indefinitely waiting for an event that will not occur NTUT, TAIWAN 18
Mobile Computing & Software Engineering Lab An Example Concurrent Client That Uses ECHO Fig. 17.2a shows TCPtecho.c that uses the ECHO service described in Ch 7 to measure network throughput to a set of machines NTUT, TAIWAN 19
Mobile Computing & Software Engineering Lab Execution of the Concurrent Client Fig. 17.3 shows sample output from three separate executions of TCPtecho NTUT, TAIWAN 20
Mobile Computing & Software Engineering Lab Figure 17.3 NTUT, TAIWAN 21
Mobile Computing & Software Engineering Lab Concurrency in the Example Code A concurrent implementation of TCPtecho improves the program in two ways Obtain a more accurate measure of the time required for each connection because congestion affects all connection equally A concurrent version makes TCPtecho more appealing to users (it offers faster response time than a sequential version NTUT, TAIWAN 22
Mobile Computing & Software Engineering Lab Summary Concurrent client implementation can offer faster response time and can avoid deadlock problems Concurrency can help designers separate control and status processing from normal input and output NTUT, TAIWAN 23
Mobile Computing & Software Engineering Lab Summary (cont.) We studied a TCP client that measures the time required to access the ECHO server one one or more machines Avoid the differences in throughput caused by network congestion by making all measurements during the same time interval Appeals to users because it overlaps the measurements NTUT, TAIWAN 24
Recommend
More recommend