|
Home > Archive > Unix Programming > October 2004 > Select not blocking on FIFO pipe
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 |
Select not blocking on FIFO pipe
|
|
| Sachin Doshi 2004-10-22, 5:53 pm |
| I am trying to continuously read from a FIFO pipe and just print the
data to a file. To do this, I am using the select() function to block
until the program
sees some data on the pipe. But for some odd reason, the select is
constantly returning - even when there is no new data in the FIFO
pipe. Here's my code if it helps.
fd_set rfds;
int retval;
char *buf;
size_t n = 0;
struct timeval tv;
FILE *output = fopen("out", "a");
FILE *log = fopen("fifo", "r");
FD_ZERO(&rfds);
FD_SET(fileno(log), &rfds);
while(1) {
tv.tv_sec = 300;
tv.tv_usec = 0;
retval = select(fileno(log) + 1, &rfds, NULL, NULL, &tv);
if (retval) {
while(getline(&buf, &n, log) != -1) {
fprintf(out, "%s", buf);
}
}
}
Any suggestions appreciated.
--Sachin
| |
| Barry Margolin 2004-10-22, 8:47 pm |
| In article <beb6d765.0410221354.645c6910@posting.google.com>,
ssdas4eva@yahoo.com (Sachin Doshi) wrote:
> I am trying to continuously read from a FIFO pipe and just print the
> data to a file. To do this, I am using the select() function to block
> until the program
> sees some data on the pipe. But for some odd reason, the select is
> constantly returning - even when there is no new data in the FIFO
> pipe. Here's my code if it helps.
>
>
>
> fd_set rfds;
> int retval;
> char *buf;
> size_t n = 0;
> struct timeval tv;
> FILE *output = fopen("out", "a");
> FILE *log = fopen("fifo", "r");
>
> FD_ZERO(&rfds);
> FD_SET(fileno(log), &rfds);
>
> while(1) {
> tv.tv_sec = 300;
> tv.tv_usec = 0;
> retval = select(fileno(log) + 1, &rfds, NULL, NULL, &tv);
>
> if (retval) {
> while(getline(&buf, &n, log) != -1) {
> fprintf(out, "%s", buf);
> }
> }
> }
>
> Any suggestions appreciated.
Several problems:
1) You're trying to use select() and also stdio on the same stream.
This is very tricky because of stdio's buffering.
2) You have to do FD_SET() each time through the loop, since select()
modifies rfds.
3) You're not checking the return value of select() for errors. -1 will
be treated as true, so you'll go into the getline() loop.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
|
|
|