From: Nikola <root.admin1@zg.t-com.hr>
To: linux-c-programming@vger.kernel.org
Subject: select() problem/feature
Date: Mon, 24 Jul 2006 15:49:48 +0200 [thread overview]
Message-ID: <44C4CFFC.8040909@zg.t-com.hr> (raw)
[-- Attachment #1: Type: text/plain, Size: 1124 bytes --]
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.
[-- Attachment #2: daemon.c --]
[-- Type: text/x-csrc, Size: 879 bytes --]
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *fp1,*fp2;
fd_set fdst;
char buffer[100];
int i;
if((fp1 = fopen("test1.txt","r")) == NULL){
return 0;
}
if((fp2 = fopen("test2.txt","r")) == NULL){
return 0;
}
/* Daemon-specific initialization goes here */
/* The Big Loop */
while (1) {
/* Do some task here ... */
FD_ZERO(&fdst);
FD_SET(fileno(fp1),&fdst);
FD_SET(fileno(fp2),&fdst);
select(fileno(fp2)+1,&fdst,NULL,NULL,NULL);
if(FD_ISSET(fileno(fp1),&fdst)){
while(fgets(buffer,100,fp1) != NULL){
puts(buffer);
}
}
if(FD_ISSET(fileno(fp2),&fdst)){
while(fgets(buffer,100,fp2) != NULL){
puts(buffer);
}
}
//fprintf(stderr,"%d\n",i++);
}
exit(EXIT_SUCCESS);
}
next reply other threads:[~2006-07-24 13:49 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-07-24 13:49 Nikola [this message]
2006-07-24 16:35 ` select() problem/feature Glynn Clements
-- strict thread matches above, loose matches on Subject: below --
2006-07-24 14:15 Mihai Dontu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44C4CFFC.8040909@zg.t-com.hr \
--to=root.admin1@zg.t-com.hr \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).