LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2007, 05:21 AM   #1
johncsl82
Member
 
Registered: Nov 2003
Location: Planet Earth
Distribution: Redhat, Gentoo, Mandrake, FreeBSD
Posts: 72

Rep: Reputation: 18
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
 
Old 04-24-2007, 06:31 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 04-25-2007, 12:40 AM   #3
johncsl82
Member
 
Registered: Nov 2003
Location: Planet Earth
Distribution: Redhat, Gentoo, Mandrake, FreeBSD
Posts: 72

Original Poster
Rep: Reputation: 18
Well do you guys have any working examples that teaches me how to utilize the select() function with writefds?
 
Old 08-05-2011, 05:50 PM   #4
errigour
Member
 
Registered: May 2009
Posts: 271

Rep: Reputation: 5
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;                   \
        }                               \
    }

Last edited by errigour; 08-05-2011 at 06:12 PM.
 
Old 08-06-2011, 11:13 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 08-06-2011, 12:12 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by johncsl82 View Post
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
 
Old 08-06-2011, 12:23 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by errigour View Post
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
 
Old 08-06-2011, 12:24 PM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
I wonder if johncsl82 still needs it after 4 years

Might be useful for others in future
 
Old 08-06-2011, 04:52 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Wim Sturkenboom View Post
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.

Last edited by ta0kira; 08-06-2011 at 04:53 PM.
 
Old 08-07-2011, 12:35 AM   #10
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by ta0kira View Post
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
 
Old 11-13-2011, 12:27 PM   #11
mastercross
LQ Newbie
 
Registered: Nov 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 01:34 PM


All times are GMT -5. The time now is 09:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration