作者bombilla (地板别打我!>"<)
看板LinuxDev
标题[问题] select 与 eventfd
时间Fri May 17 13:44:02 2013
Hi all,
我有点问题想请教一下,就是我想弄一个consumer和producer的测试~
所以我先弄eventfd弄了一个fd然後再开一个thread,在这个thread中
使用select去听这个fd。
但问题是select只能block住第一次,接着就再也不会block住了,
如果我有keyin东西的话就是拼命印出buffer的值,
不然就是时间一到就拼命印出no data…
感谢各位~
我测试的程式码如下:
static char buffer[128] = { 0 };
static void *read_handler(void *arg)
{
int fd = *((int *)arg);
int ret = 0;
fd_set rfds;
struct timeval tv;
eventfd_t r;
//tv.tv_sec = 10;
//tv.tv_usec = 0;
while(1) {
tv.tv_sec = 10;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
ret = select(fd + 1, &rfds, NULL, NULL, &tv);
if (ret == -1)
printf("select error...%d\n", ret);
else if (ret) {
printf("[%s]\n", buffer);
if ((strncmp(buffer, "quit", 4) == 0)) break;
eventfd_read(fd, &r);
} else
printf("no data\n");
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t read_thd;
int fd = eventfd(0, 0);
char ch = 0;
void *res = NULL;
unsigned long long no = 1;
if (pthread_create(&read_thd, NULL, &read_handler, &fd)) {
printf("pthread_create failed...\n");
return -1;
}
do {
printf("Enter string: ");
scanf("%s", buffer);
eventfd_write(fd, no);
} while (strncmp(buffer, "quit", 4));
pthread_join(read_thd, &res);
if (res) free(res);
close(fd);
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 60.251.182.145
1F:推 AceIan:read_handler 被没有把东西 read 走啊 05/17 13:49
2F:→ bombilla:啊?所以我一定要read走喔? 05/17 13:50
3F:→ bombilla:我去试试~ =.=a 多谢~ 05/17 13:50
4F:→ bombilla:等一下~~可是我不输入任何值,只是在那边等10秒钟~ 05/17 13:52
5F:→ bombilla:它也是会拼了命的印出 no data 说~ 05/17 13:52
我现在多加了 eventfd_read ,现在的动作:
1.我输入一些字元,这些字元会被printf出来没错…每次我一输入完,
select不就应该是要重算10秒吗?但…就算我一直有输入,
10秒一到还是一直喷no data…
2.程式执行起来10秒一到,就一直喷no data…
※ 编辑: bombilla 来自: 60.251.182.145 (05/17 15:11)
6F:→ bombilla:好,我试了一下~~回圈内也得每次重设tv~XD 05/17 15:23
每次select跳出来之後tv的值会被清成零~XD
※ 编辑: bombilla 来自: 60.251.182.145 (05/17 15:25)
7F:→ okgogogo:FD_ISSET 05/24 21:19