select() 不等待
我必须读取程序日志文件,为此我想使用 select() 和 read()
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = -1;
fd_set fds;
struct timeval tv;
int rc;
char buffer[4096];
char * log_path = "/home/mich/a.txt";
if((fd = open(log_path,O_RDONLY) ) == -1 )
{
printf("error\n");
return -1;
}
while(1)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0) {
printf("failed\n");
continue;
} else if (rc > 0 && FD_ISSET(fd,&fds)) {
printf("read\n");
} else {
printf("timeout\n");
continue;
}
while ((my_getline(buffer,sizeof(buffer),fd)) > 0)
{
printf("%s",buffer);
}
}
close(fd);
}
my_getline 是一个使用 read() 的函数。
输出:
read
aaa
read
read
read
read
read
bbb
read
read
read
read
...
其中 aaa 和 bbb 是读取文件中的行。
这个程序有什么问题吗?
I've to read program log file and to do that I wanted to use select() and read()
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = -1;
fd_set fds;
struct timeval tv;
int rc;
char buffer[4096];
char * log_path = "/home/mich/a.txt";
if((fd = open(log_path,O_RDONLY) ) == -1 )
{
printf("error\n");
return -1;
}
while(1)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0) {
printf("failed\n");
continue;
} else if (rc > 0 && FD_ISSET(fd,&fds)) {
printf("read\n");
} else {
printf("timeout\n");
continue;
}
while ((my_getline(buffer,sizeof(buffer),fd)) > 0)
{
printf("%s",buffer);
}
}
close(fd);
}
my_getline is a function which uses read().
Output:
read
aaa
read
read
read
read
read
bbb
read
read
read
read
...
where aaa and bbb are lines from read file.
What is wrong in this program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
select
告诉您fd
有东西在等待。这不一定是一行,它可以是单个字节,或者已到达文件末尾的信息。在外部while
循环末尾添加puts("####");
以查看代码如何处理输入文件。要了解如何继续读取其他进程附加到的文件,请查看 unix
tail
实用程序的免费实现之一的源代码(特别是它的-f
选项)。传统的实现是读取整个文件,休眠一小段时间(tail
为 1 秒),然后从上次读取后停止的位置重复。一种更现代、更具反应性、资源消耗更少但可移植性较差的方法使用文件更改通知 API(Linux 下的 inotify、*BSD 下的 kqueue,...)。select
tells you that there is something waiting onfd
. This doesn't have to be a line, it could be a single byte, or the information that the end of the file has been reached. Addputs("####");
at the end of the outerwhile
loop to see how your code processes the input file.To see how to keep reading a file that other processes are appending to, look at the source code for one of the free implementations of the unix
tail
utility (specifically its-f
option). The traditional implementation is to read the whole file, sleep for a small interval (1 second intail
), and repeat from the position where you left off after the last read. A more modern, more reactive, less resource-consuming, but less portable approach uses a file change notification API (inotify under Linux, kqueue under *BSD, ...).select()
告诉您read()
不会阻塞。这包括它将返回
0
来指示文件结束的情况,这可能就是您所得到的。select()
tells you that aread()
won't block.That includes the case where it will return
0
to indicate end-of-file, which presumably is what you're getting.