Socket Programming making use of "select()" and "writefds"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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"?
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
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; \
} \
}
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.
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":
Create a list of file descriptors to monitor (an array works fine).
Fill an fd_set with the descriptors from the list and call select.
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.
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.