Unix Programming - Full sample for select()

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > Full sample for select()





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Full sample for select()
Alex Vinokur

2006-04-02, 7:42 pm

------------------------------------------
int select(int nfds, int* readfds, int* writefds, int* exceptfds, struc
timeval* timeout);
------------------------------------------

Usage of select()'s samples are usually for this case:
select(nfds, readfds, 0, 0, timeout).

Are there any good samples for usage of
select(nfds, readfds, writefds, exceptfds, timeout) ?


Thanks,

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mark

2006-04-02, 7:42 pm

On Wed, 29 Mar 2006 04:42:22 -0800, Alex Vinokur wrote:

> ------------------------------------------
> int select(int nfds, int* readfds, int* writefds, int* exceptfds, struc
> timeval* timeout);
> ------------------------------------------
>
> Usage of select()'s samples are usually for this case:
> select(nfds, readfds, 0, 0, timeout).
>
> Are there any good samples for usage of
> select(nfds, readfds, writefds, exceptfds, timeout) ?
>


If you find one be sure to reply here... I've been looking too,
and they are hard to find indeed!

Andrei Voropaev

2006-04-02, 7:42 pm

On 2006-03-29, Mark <markdv@tiscali.nl> wrote:
> On Wed, 29 Mar 2006 04:42:22 -0800, Alex Vinokur wrote:
>
>
> If you find one be sure to reply here... I've been looking too,
> and they are hard to find indeed!
>


Well, the usage of writefds is obvious. I'm really surprised to see it
as part of the question. I use it all the time with sockets, since the
peer may stop reading for whatever reason and then I have to wait till
it starts reading again.

As to exceptfds... man select_tut on linux gives this

exceptfds

This set is watched for exceptions or errors on any of the
file descriptors. However, that is actually just a rumor.
How you use exceptfds is to watch for out-of-band
(OOB) data. OOB data is data sent on a socket using the
MSG_OOB flag, and hence exceptfds only really applies
to sockets. See recv(2) and send(2) about this. After
select has returned, exceptfds will be cleared of all file
descriptors except for those descriptors that are avail-
able for reading OOB data. You can only ever read
one byte of OOB data though (which is done with recv()),
and writing OOB data (done with send) can be done at
any time and will not block. Hence there is no need for a
fourth set to check if a socket is available for writing
OOB data.

Since OOB is not that often used, you don't see the actual usage very
often as well.

--
Minds, like parachutes, function best when open
Alex Vinokur

2006-04-02, 7:42 pm


Andrei Voropaev wrote:
[snip]
> Well, the usage of writefds is obvious. I'm really surprised to see it
> as part of the question. I use it all the time with sockets, since the
> peer may stop reading for whatever reason and then I have to wait till
> it starts reading again.

[snip]

-----------------------------------
num = select (nfds, 0, writefds, 0, 0);

if (num > 0)
{
// Do something
}
-----------------------------------


It seems that the piece above works as

while (1)
{
// Do something
}

If it is truth what do we reach here?


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mark

2006-04-02, 7:42 pm

On Thu, 30 Mar 2006 10:12:29 -0800, Alex Vinokur wrote:

>
> Andrei Voropaev wrote:
> [snip]
> [snip]
>
> -----------------------------------
> num = select (nfds, 0, writefds, 0, 0);
>
> if (num > 0)
> {
> // Do something
> }
> -----------------------------------
>
>
> It seems that the piece above works as
>
> while (1)
> {
> // Do something
> }
>
> If it is truth what do we reach here?
>


Unless "Do something" includes writing to the fd it will prolly always be
ready to consume more data. If you are writing to it then it will be ready
as long as whatever consumes the data at 'the other end' can keep up.

Regards,
Mark.


Alex Vinokur

2006-04-02, 7:42 pm

Mark wrote:
> On Thu, 30 Mar 2006 10:12:29 -0800, Alex Vinokur wrote:

[snip]
>
> Unless "Do something" includes writing to the fd it will prolly always be
> ready to consume more data. If you are writing to it then it will be ready
> as long as whatever consumes the data at 'the other end' can keep up.
>

[snip]

------ select & read ------

Let readfds contain the only fd

int count = 0;
while (true)
{
// Resetting readfds
numr = select (nfds, readfds, 0, 0, 0);

count++;
// count will be equal to number of messages received

if (numr > 0)
{
// Do recvfrom() etc
}
}

---------------------------


------ select & write ------

Let readfds contain the only fd

int count = 0;
while (true)
{
// Resetting writefds
numw = select (nfds, 0, writefds, 0, 0);

count++;
// It seems that count will not be equal to number of messages sent,
// count will be equal to number of enterings after select()
// Here it works as
// count = 0;
// while (true)
// {
// count++
// }
// ?

if (numw > 0)
{
// Do something
}
}

----------------------------



Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Andrei Voropaev

2006-04-02, 7:42 pm

On 2006-03-30, Alex Vinokur <alexvn@users.sourceforge.net> wrote:
> num = select (nfds, 0, writefds, 0, 0);
> if (num > 0) { // Do something }
>
> It seems that the piece above works as
> while (1) { // Do something }
>
> If it is truth what do we reach here?


So, I guess you've never done networking programming with TCP sockets.
If you did, then your programs are broken, because if your peer for
whatever reason stops reading from the socket while your are actively
writing at your end, then after filling up your TCP buffers you won't be
able to write anything else and your program will either block, or won't
notice the problem. Even simple network congestion may result in zero
data transfer rate and your buffers will fill up.

--
Minds, like parachutes, function best when open
Alex Vinokur

2006-04-02, 7:42 pm


"Andrei Voropaev" <avorop@mail.ru> wrote in message news:e0it6a$bum$2@chessie.cirr.com...
> On 2006-03-30, Alex Vinokur <alexvn@users.sourceforge.net> wrote:
>
> So, I guess you've never done networking programming with TCP sockets.
> If you did, then your programs are broken, because if your peer for
> whatever reason stops reading from the socket while your are actively
> writing at your end, then after filling up your TCP buffers you won't be
> able to write anything else and your program will either block, or won't
> notice the problem. Even simple network congestion may result in zero
> data transfer rate and your buffers will fill up.
>

[snip]

What about UDP sockets in that context?


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Alex Vinokur

2006-04-02, 7:42 pm

Alex Vinokur wrote:
> "Andrei Voropaev" <avorop@mail.ru> wrote in message news:e0it6a$bum$2@chessie.cirr.com...
> [snip]
>
> What about UDP sockets in that context?


In other words, is it worth using select (nfds, 0, writefds, 0, 0)
for UDP sockets (and how to use? any sample? Thanks)?

[snip]

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Andrei Voropaev

2006-04-11, 9:59 am

On 2006-04-02, Alex Vinokur <alexvn@users.sourceforge.net> wrote:
[...]
>
> In other words, is it worth using select (nfds, 0, writefds, 0, 0)
> for UDP sockets (and how to use? any sample? Thanks)?


I didn't use UDP so far, so, I can be wrong. But man sendto implies that
EAGAIN can be returned with non-blocking sockets. Which in turn means
that in certain situation you may need to use select to check for write
readinnes. After all, it's not that hard. Try to sendto, check the
return value, if an EAGAIN error has happened, then use the select. When
it never happens, then it is never called

--
Minds, like parachutes, function best when open
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com