Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Select not blocking on FIFO pipe
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

Report this thread to moderator Post Follow-up to this message
Old Post
Sachin Doshi
10-23-04 01:57 AM


Re: Select not blocking on FIFO pipe
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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
10-23-04 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:25 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.