10-23-04 01:47 AM
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 ***
[ Post a follow-up to this message ]
|