[ Team LiB ] Previous Section Next Section

Exercises

26.1

Compare the descriptor usage in a server using fork versus a server using a thread, assuming 100 clients are being serviced at the same time.

26.2

What happens in Figure 26.3 if the thread does not close the connected socket when str_echo returns?

26.3

In Figures 5.5 and 6.13, we print "server terminated prematurely" when we expect an echoed line from the server but receive an EOF instead (recall Section 5.12). Modify Figure 26.2 to print this message too, when appropriate.

26.4

Modify Figures 26.11 and 26.12 so that they can compile on a system that does not support threads.

26.5

To see the error with the readline function that is used in Figure 26.3, build that program and start the server. Then, build the TCP echo client from Figure 6.13 that works in a batch mode correctly. Find a large text file on your system and start the client three times in a batch mode, reading from the large text file and writing its output to a temporary file. If possible, run the clients on a different host from the server. If the three clients terminate correctly (often they hang), look at their temporary output files and compare them to the input file.

Now build a version of the server using the correct readline function from Section 26.5. Rerun the test with three clients; all three clients should now work. You should also put a printf in the readline_destructor function, the readline_once function, and in the call to malloc in readline. This shows that the key is created only one time, but the memory is allocated for every thread, and that the destructor function is called for every thread.


    [ Team LiB ] Previous Section Next Section