Select not blocking on FIFO pipe
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Select not blocking on FIFO pipe




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Select not blocking on FIFO pipe  
Sachin Doshi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-22-04 10: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





[ Post a follow-up to this message ]



    Re: Select not blocking on FIFO pipe  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:57 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register