[问题] select 与 eventfd

楼主: bombilla (地板别打我!>"<)   2013-05-17 13:44:02
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;
}
作者: AceIan (ccc)   2013-05-17 13:49:00
read_handler 被没有把东西 read 走啊
楼主: bombilla (地板别打我!>"<)   2013-05-17 13:50:00
啊?所以我一定要read走喔?我去试试~ =.=a 多谢~等一下~~可是我不输入任何值,只是在那边等10秒钟~它也是会拼了命的印出 no data 说~好,我试了一下~~循环内也得每次重设tv~XD
作者: okgogogo ( )   2013-05-24 21:19:00
FD_ISSET

Links booklink

Contact Us: admin [ a t ] ucptt.com