List Info

Thread: select() problem/feature




select() problem/feature
user name
2006-07-24 13:49:48
Hi all,

i am writing small daemon.....and when i was seeing the end
......that 
was just the beginning
of my problems.

General idea was to monitor list of files that was given on
start.

For this example i am using test1.txt and test2.txt because
I've 
succeeded to replicate my problem
,at least i think so....))))

so the files are....

test1.txt
-----8<-----8<-----8<-----8<-----8<-----8<
-----8<
Testing from test1
-----8<-----8<-----8<-----8<-----8<-----8<
-----8<



test2.txt
-----8<-----8<-----8<-----8<-----8<-----8<
-----8<
Testing from test1
-----8<-----8<-----8<-----8<-----8<-----8<
-----8<

and daemon.c


Now the problem is following:

when i compile (gcc daemon.c => a.out) and run binary in
this version 
all works fine....and
if I do
#echo "some new line" >> test1.txt

i would see on my screen that select figured it out and i am
having new 
data for processing and it
would print it out.

but when i uncomment last fprintf my while and select goes 
ballistic.....it prints  numbers forever...not
blocking......

So why is that this line
"fprintf(stderr,"%d\n",i++);"
causes this behavior.

tnx for help.
select() problem/feature
user name
2006-07-24 16:35:04
Nikola wrote:

> i am writing small daemon.....and when i was seeing the
end ......that 
> was just the beginning
> of my problems.
> 
> General idea was to monitor list of files that was
given on start.

> if I do
> #echo "some new line" >> test1.txt
> 
> i would see on my screen that select figured it out and
i am having new 
> data for processing and it
> would print it out.

select() is of no use here; files are always
"ready" for I/O. select()
is intended for use on descriptors where I/O calls may block
indefinitely: pipes, sockets, terminals and other
"slow" character
devices.

You can either poll the file at intervals (like "tail
-f ..." does),
or use the linux-specific "dnotify" ioctl()s
(see the file
Documentation/dnotify.txt in the kernel source tree for
details).

> but when i uncomment last fprintf my while and select
goes 
> ballistic.....it prints  numbers forever...not
> blocking......

None of the syscalls in your code will ever block; your
program will
be running continuously, using up to 100% CPU. Uncommenting
the
fprintf() call simply makes this obvious; it isn't changing
anything.

-- 
Glynn Clements <glynngclements.plus.com>
-
To unsubscribe from this list: send the line
"unsubscribe linux-c-programming" in
the body of a message to majordomovger.kernel.org
More majordomo info at  http://vge
r.kernel.org/majordomo-info.html
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )