caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Nathaniel Gray <n8gray@gmail.com>
Cc: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>,
	caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Re: Select on channels (again)
Date: Thu, 24 Aug 2006 15:37:49 +1000	[thread overview]
Message-ID: <1156397869.16998.122.camel@rosella.wigram> (raw)
In-Reply-To: <aee06c9e0608231231w26e34f8cs6e44e55ed2c236ab@mail.gmail.com>

On Wed, 2006-08-23 at 12:31 -0700, Nathaniel Gray wrote:
> On 8/22/06, skaller <skaller@users.sourceforge.net> wrote:

> > The problem would be the semantics you indicate above.
> > You're going to get non-blocking behaviour when the
> > channels are quiet, but once there is some data,
> > you get blocking.
> 
> I don't follow you.  Are you talking about input or output channels?
> Can you give an example?

Sure .. two sockets, with integers coming down them encoded as
characters .. perhaps a CSV file.

The sockets are quiet. Then some data comes down one,
and you go off and try to read an integer .. but half way
through you run out of data and block.

Now a whole stream of integers comes down the OTHER socket.
Bad luck, your thread is blocked waiting on data from the
first socket.

Select or not .. buffering or not: you might as well have just 
picked a socket and done a blocking read on it.

Unless you can *guarantee* some invariant is preserved,
you've achieved nothing.

The correct way to use select is in conjunction
with a polling loop that does non-blocking I/O. 

Polling all your channels without blocking is what you want,
that way you read or write data efficiently.

But such a polling loop wastes cycles and isn't very responsive,
so you use select() to skip over polling channels that cannot 
be ready, if that happens to be all of them, you might as well
block until the situation changes, if some channels are
ready you can go off and try I/O on them, and not bother
with the non-ready channels.

The key thing here is that select is an optimisation:
the IMPORTANT point is that the polling must use
non-blocking I/O.

Note: just because select() indicates an fd is ready
doesn't mean it is. There's no assurance even one byte
can be read or written.

Select() is just like a condition variable -- you have
to assume spurious wakeups. Both provide a way to know
when and what to check which avoids excessive polling.
Select, like condition variables, are just hints to
improve response time and resource utilisation:
they have no semantics. (More precisely the delivery
of a signal MUST wake up the client but the converse
isn't required).

The important semantics are in YOUR code: the condition 
check must not block, similarly, I/O must not block.

So basically doing blocking I/O in conjunction
with select makes no sense.

IF you had some known semantics such as that
messages were of bounded length and transmitted
in bounded time .. select would be useful, since
blocking operations would block only for bounded time,
in which case they're not blocking at all.

So you are right, a channel based select COULD be useful,
given such a constraint, however it doesn't break the
rule above, since blocking for a bounded time is 
semantically a non-blocking operation: an operation
is only blocking if it can block for an unbounded time.

For this reason most programmers consider all disk I/O
as non-blocking .. even though the OS kernel may
not agree with this interpretation :)


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2006-08-24  5:38 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-15  0:46 Nathaniel Gray
2006-08-21 22:47 ` Nathaniel Gray
2006-08-22  0:42   ` [Caml-list] " Jonathan Roewen
2006-08-22  6:27     ` Nathaniel Gray
2006-08-22  6:41       ` Jonathan Roewen
2006-08-22  8:15         ` skaller
2006-08-22 21:15           ` Mike Lin
2006-08-23  5:12         ` Nathaniel Gray
2006-08-22  8:10       ` Olivier Andrieu
2006-08-23  5:27         ` Nathaniel Gray
2006-08-22  8:21       ` Jacques Garrigue
2006-08-23  5:16         ` Nathaniel Gray
2006-08-23  6:35           ` skaller
2006-08-23 19:31             ` Nathaniel Gray
2006-08-24  5:37               ` skaller [this message]
2006-08-24 19:06                 ` Nathaniel Gray
2006-08-25  1:55                   ` skaller
2006-08-25 22:19                     ` Nathaniel Gray
2006-08-23  8:29 Christoph Bauer
2006-08-23 17:35 ` Robert Roessler
2006-08-24  8:18 ` Robert Roessler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1156397869.16998.122.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=n8gray@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).