[olug] questions about socket programming
Lou Duchez
lou at paprikash.com
Sun Aug 2 21:02:47 CDT 2020
... that makes a lot of sense. Thanks!
On 8/2/2020 9:58 PM, Eric W. Biederman wrote:
> Lou Duchez <lou at paprikash.com> writes:
>
>> I believe you're right about socket_accept. What I'm doing is I'm
>> first checking whether there are any incoming connections via
>> socket_select, which has a timeout interval that I set to 10
>> microseconds -- that's enough time to tell me if there are any waiting
>> connections, without idling for an extended period. If there is a
>> waiting connection, that's when I invoke socket_accept, so the
>> blocking behavior of socket_accept isn't a problem.
>>
>> The grand loop ("while (true) { ... }") that contains all the
>> connection handling, has three parts: accepting new connections,
>> reading data from existing connections, and sending data as
>> appropriate to existing connections. The "sleep" part that I feel is
>> a good idea is at the end of the grand loop.
>>>>> On Sat, Aug 1, 2020 at 8:26 PM Lou Duchez <lou at paprikash.com> wrote:
>>>>>> 2) The code examples I've seen do not put any sort of sleep(), usleep(),
>>>>>> or time_nanosleep() in the grand loop. That seems ill-advised to me;
>>>>>> based on my testing, the grand loop consumes a ton of CPU unless I put a
>>>>>> brief sleep (maybe one-tenth second) in each iteration. Am I wrong to
>>>>>> put the sleep in there? Am I handling it wrong?
>
> Are you handling it wrong? I believe so.
>
> You should use your socket_select or possibly even your socket_accept to
> sleep. If something comes in while they are waiting the sleep will be
> cut short, so responsiveness will be maintained. If they are allowed
> to sleep long enough you will consume essentially no cpu when idle.
>
> Eric
More information about the OLUG
mailing list