Understanding the Multiple-Client Problem in socket
Figure 11.1 shows several clients, which have contacted one server. The client connections conceptually form spokes around the central server.
Several clients attached to one server can be graphically represented as spokes attached to a hub.
The server, acting as the central hub in Figure 11.1, must balance its resources among several connected clients. The server is normally designed to behave such that each client thinks that it has dedicated server access. In reality, however, the server services all clients in a concurrent manner.
There are a few of ways of achieving this. They are
- Forked server processes (multi-process method)
- Threaded server processes (multi-thread method)
- One process and a select(2) call
- One process and a poll(2) call
The first method of using the fork(2) system call is perhaps the simplest way to service multipleclient processes. However, it suffers from the disadvantage that sharing information becomes more complex. This usually requires the use of message queues, shared memory, and semaphores. It also suffers from the disadvantage that it requires more CPU to start and manage a new process for each request.
The threaded server method is relatively new to UNIX, and is now a viable option for Linux. Kernel versions 2.0.0 and later support threads, provided that the appropriate thread-safe libraries are used. Threads offer the lightweight advantages of the multi-process method, without hampering centralized communication. Threaded processes can be very difficult to debug, however, especially for beginning programmers. For this reason, threads will not be explored in this text.
The last two methods listed involve the use of the select(2) or poll(2) function calls. Each of these functions offer a different way to block execution of the server until an event occurs. The
select(2) function will be examined in detail within this chapter. The interested reader is encouraged to read the man pages for poll(2) after completing this chapter.