For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > December 2004 > Linux: RedHat enterprise: C, read() & select() not blocking









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 Linux: RedHat enterprise: C, read() & select() not blocking
karres@uiuc.edu

2004-12-16, 3:57 am

Hi,

I think this should be simple. I thaink I have done something similar
a long time ago but the magic is hiding from me.

I have a system log file that grows and is rotated once a month. I
need to read the whole log file once then "hang" on the bottom of the
file waiting for more input.

Eventually the file will be rotated. I want to have read every spec of
data in the now old log; close it and then open the new log and start
from the top.

I figured that setting up a select() then doing the read() would work.
However, the select is not blocking at all. My test file is zero
length in this test. I open the file, do the select and it returnes
immediately saying that the descriptor on that zero length file is
ready for reading... huh?

I then read zero bytes form the file and repeat... forever.

I assumed that the select would block on the zero length file until
something was sent to it.

I am FD_ZEROing, FD_SETing and reseting the time struct before each
select call

while (1)
{
FD_ZERO (&read_FDs);
FD_SET (file_no, &read_FDs);

select_timeout.tv_sec = 60; /* 1 minute, why not? */
select_timeout.tv_usec = 0;

if ((select_result = select ((file_no + 1), &read_FDs,
NULL, NULL, &select_timeout)) > 0)
{
if (FD_ISSET (WTMP_file_no, &read_FDs))
{
if ((read_result = read (file_no, &wtmp,
size_of_buf)) > 0)
{
....process read...
}
else if (read_result == 0)
{
/*
* read zero bytes on what should have been a
* prepared stream.
*/
}
else
{
exit (__LINE__);
}
}
else
{
/*
* select says there is pending data but the FD is not
* set. what gives?
*/
exit (__LINE__);
}
}
else if (select_result == -1)
{
exit (__LINE__);
}
}

I am opening the file read-only

file_no = open (file, O_CREAT | O_RDONLY, 0664)

Obviously I am missing something in the select() man page. What am I
missing?

Dean...K...

Barry Margolin

2004-12-16, 4:05 pm

In article <1103177363.287358.58530@c13g2000cwb.googlegroups.com>,
karres@uiuc.edu wrote:
> I assumed that the select would block on the zero length file until
> something was sent to it.


No, select() will never block on a normal file. Select() tells you
whether read() would have blocked. Since read() will always return
immediately (ignoring NFS delays), either with data or an EOF
indication, select() should always report it being readable.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
karres@uiuc.edu

2004-12-16, 4:05 pm

Ah! ok. thank you

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com