[olug] questions about socket programming
Lou Duchez
lou at paprikash.com
Mon Aug 3 04:52:52 CDT 2020
I had another architectural issue, but I came up with a design to solve
it, so I'll share. Part of my problem is, my grand loop needs to look
at data coming in from a couple different connections, and I didn't go
into that earlier, but that was part of my thinking as to why I needed
to do a loop with a sleep step as a separate operation. Basically I
needed a loop that would check each connection and then go back to the
first after a brief delay.
HOWEVER, if I instead take the different connections and move each to
its own process, which then communicate to my socket server only when
they have data, that detangles the whole mess. Each process can then
wait in blocking mode without impeding the others, and the socket server
will receive updates just as quickly as the other processes can report
them. Much cleaner design that doesn't bog the whole system down. Now
I just need to adapt my code.
On 8/2/2020 10:02 PM, Lou Duchez wrote:
> ... 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
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> https://www.olug.org/mailman/listinfo/olug
More information about the OLUG
mailing list