LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Socket Programming making use of "select()" and "writefds" (http://www.linuxquestions.org/questions/programming-9/socket-programming-making-use-of-select-and-writefds-548552/)

johncsl82 04-24-2007 06:21 AM

Socket Programming making use of "select()" and "writefds"
 
Hello Linux professionals,

Most of the Unix socket tutorial available out there, which I found regrading the select() function emphasis on readfds (read file description set) and they did not mention much about writefds (write file description set) and exceptfds (exception file description set).

So here is my question:

What makes select() function to return a "write" file description set value?, basically it means that the select() function will enter a description set value into writefds variable.

I did find some tutorials:

http://rhoden.id.au/doc/sockets2.html

http://olympus.het.brown.edu/cgi-bin...l?2+select_tut

based on the tutorials and from my point of understanding, usually the "writefds" is triggered by the programmer rather than "select()" function. This means that the programmer have to manually trigger the "writefds" rather than the OS or "select()" function triggering the "writefds".

So, is there any simple program in C programming language, such chat, telnet, web server or etc, that utilize "select()" and "writefds" that can help me to understand better about logic or structure of "selec()" function and "writefds"?

Thanks

dmail 04-24-2007 07:31 AM

I have a little difficulty understanding you post but...

What makes select() function to return a "write" file description set value?, basically it means that the select() function will enter a description set value into writefds variable.

writefds is simply a socket which can be written to. So if you had 10 handles and five needed to be written to you may enter the five into a fdset, check them getting the number which can be written to and a set back. Then you would check each of the five handles with the set, if its present then write to the socket.

This means that the programmer have to manually trigger the "writefds" rather than the OS or "select()" function triggering the "writefds".

I'm not sure I fully understand you, it really doesn't make much sense to check all sockets to see if they are writeable unless you have data to write to the socket. Personally I have not had much need to use the write functionality of select

johncsl82 04-25-2007 01:40 AM

Well do you guys have any working examples that teaches me how to utilize the select() function with writefds?

errigour 08-05-2011 06:50 PM

I just wanted to post this page.
http://linux.die.net/man/2/select_tut
It looks as if they used FD_ISSET to check if select
has done anything with the fd sets presented.
Code:

for (i > 0; i < fdmax+1; i++)      // Iterate through all available sockets
if (FD_ISSET (i, &writefd)) {      // If not blocked do stuff
r =
write (i,
    buf1 + buf1_written,
    buf1_avail -
    buf1_written);
if (r < 1) {
    SHUT_FD;
} else
    buf1_written += r;
}
#define SHUT_FD1 {                      \
        if (fd1 >= 0) {                \
            shutdown (fd1, SHUT_RDWR);  \
            close (fd1);                \
            fd1 = -1;                  \
        }                              \
    }


theNbomr 08-06-2011 12:13 PM

I think your question can be re-phrased as 'What makes a socket have some readiness for writing?'. In most cases, such as sockets open for writing to IP connections, you can write arbitrarily, and the buffering in the network stack will prevent 'overflow' or any condition that would prevent more data from being written. Some devices may have a limit to how much data can be written before they become 'full'.
I cannot think of any example of such a case, but I feel certain that something must exist. I can give an analogous description, though: message queues. These have a finite size, and once full, cannot accept any more data. The analogy breaks down in that one does not use sockets to write or read them; they have their own API for access. Another analogy would be the Tx buffer in a UART (serial port hardware). Most UARTs have a finite number of bytes which they are capable of queuing for transmission, sometimes only one or two. They signal the controlling software that they can accept more data when the Tx buffer is either non-full or is empty. Again, the analogy breaks down, as the device driver handles buffering for userspace applications.
In my experience, it is not uncommon for userspace applications to completely ignore the use of select() to signal readiness to write to sockets. select() is used to manage handling of asynchronous input, and output is simply done whenever the need arises.

--- rod.

ta0kira 08-06-2011 01:12 PM

Quote:

Originally Posted by johncsl82 (Post 2724374)
Well do you guys have any working examples that teaches me how to utilize the select() function with writefds?

Basically, select should only block in this case if all fds are in a state that would cause a write block. This happens if the "read" end doesn't read quickly enough; all descriptors have some sort of limitation on how much can be written before a read happens, unless of course it's an option for the protocol to discard data.

select for "write" should be the same as that for "read":
  1. Create a list of file descriptors to monitor (an array works fine).
  2. Fill an fd_set with the descriptors from the list and call select.
  3. When select returns, start again at 2 if the return is -1 and errno==EINTR. Otherwise, check each descriptor in the list against the fd_set with FD_ISSET. If FD_ISSET is "true":
    • If the return of select is -1 and errno==EBADF, remove the descriptor being checked from the list.
    • If the return of select is > 0 the descriptor being checked can be written to without blocking. I don't know if writing too much data will cause a block or if it will write what there's room for and then return, however.
    • Otherwise, you did something wrong.
  4. Repeat from 2.
Remember: you must fill the fd_set again every time! It's both the input and the output for normal and error returns.
Kevin Barry

ta0kira 08-06-2011 01:23 PM

Quote:

Originally Posted by errigour (Post 4434878)
I just wanted to post this page.
http://linux.die.net/man/2/select_tut
It looks as if they used FD_ISSET to check if select
has done anything with the fd sets presented.
Code:

for (i > 0; i < fdmax+1; i++)      // Iterate through all available sockets
if (FD_ISSET (i, &writefd)) {      // If not blocked do stuff
r =
write (i,
    buf1 + buf1_written,
    buf1_avail -
    buf1_written);
if (r < 1) {
    SHUT_FD;
} else
    buf1_written += r;
}
#define SHUT_FD1 {                      \
        if (fd1 >= 0) {                \
            shutdown (fd1, SHUT_RDWR);  \
            close (fd1);                \
            fd1 = -1;                  \
        }                              \
    }


Where did this code come from? It isn't on the page you linked to. Where's the rest of it?
Kevin Barry

Wim Sturkenboom 08-06-2011 01:24 PM

I wonder if johncsl82 still needs it after 4 years ;)

Might be useful for others in future :)

ta0kira 08-06-2011 05:52 PM

Quote:

Originally Posted by Wim Sturkenboom (Post 4435393)
I wonder if johncsl82 still needs it after 4 years ;)

Might be useful for others in future :)

Nooooooo!!! I'm such a sucker. I even started a thread about zombie-thread prevention in the Suggestions forum because all of the recent "incidents" in this forum...
Kevin Barry

PS Maybe johncsl82 has been diligently waiting, like the child-robot in the movie A.I.

Wim Sturkenboom 08-07-2011 01:35 AM

Quote:

Originally Posted by ta0kira (Post 4435554)
I even started a thread about zombie-thread prevention in the Suggestions forum because all of the recent "incidents" in this forum...

Don't worry, so did I a while ago ;) Maybe time for some captcha with a 64 character string if people want to revive an old thread :D

mastercross 11-13-2011 01:27 PM

ta0kira: Your answer was not in vain, it helped me clear up the meaning of writefds. Furthermore is this post the top result on Google when searching for "select writefds", so I don't think I am the only one searching for this and benefitting of a nice explanation.


All times are GMT -5. The time now is 11:54 PM.