select() 不等待

发布于 2024-09-17 00:35:07 字数 1294 浏览 5 评论 0原文

我必须读取程序日志文件,为此我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-09-24 00:35:19

select 告诉您 fd 有东西在等待。这不一定是一行,它可以是单个字节,或者已到达文件末尾的信息。在外部 while 循环末尾添加 puts("####"); 以查看代码如何处理输入文件。

要了解如何继续读取其他进程附加到的文件,请查看 unix tail 实用程序的免费实现之一的源代码(特别是它的 -f选项)。传统的实现是读取整个文件,休眠一小段时间(tail 为 1 秒),然后从上次读取后停止的位置重复。一种更现代、更具反应性、资源消耗更少但可移植性较差的方法使用文件更改通知 API(Linux 下的 inotify、*BSD 下的 kqueue,...)。

select tells you that there is something waiting on fd. 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. Add puts("####"); at the end of the outer while 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 in tail), 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, ...).

九命猫 2024-09-24 00:35:17

select() 告诉您 read() 不会阻塞。

这包括它将返回 0 来指示文件结束的情况,这可能就是您所得到的。

select() tells you that a read() won't block.

That includes the case where it will return 0 to indicate end-of-file, which presumably is what you're getting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文