Wallace
Wallace

Reputation: 651

How a thread service two data sockets (not control sockets) equally?

Suppose that we have a single-thread application, and it needs to service two clients by writing 1G bytes data to two separate tcp sockets (one socket per client) respectively, in this situcation how can the thread work on the two tasks equally and continually?

I think this problem exists in server applications like Apache, take the Apache Web Server as an example, the Apache sets a max thread limit for itself, say it is MAX_THREADS, and if there are (MAX_THREADS + 1) outstanding requests and sockets there which means at least one thread must handle two sockets equally. Then how would apache handle this situation?

Steve

Upvotes: 0

Views: 84

Answers (1)

Pradheep
Pradheep

Reputation: 3819

Usually when we want to handle several sockets in a single threaded application then one of the following system calls are generally used

select (http://en.wikipedia.org/wiki/Select_%28Unix%29)

poll (http://linux.die.net/man/2/poll)

epoll (http://en.wikipedia.org/wiki/Epoll)

More on these calls can be found in the man pages.

the general idea is to make the single thread not get blocked waiting to get a resource and periodically check if data is available to send or receive

Upvotes: 1

Related Questions