将 select() 与管道一起使用

发布于 2024-11-28 03:57:55 字数 497 浏览 0 评论 0原文

我正在读取/写入pipe(pipe_fds)创建的管道。因此,基本上使用以下代码,我从该管道中读取:

fp = fdopen(pipe_fds[0], "r"); 

每当我得到一些东西时,我都会通过以下方式打印出来:

while (fgets(buf, 200, fp)) {
    printf("%s", buf);
}

我想要的是,当在一定时间内管道上没有出现任何内容时, 阅读,我想了解它并执行以下操作:

printf("dummy");

这可以通过 选择()?任何关于如何做到这一点的指示都会很棒。

I am reading/writing to a pipe created by pipe(pipe_fds). So basically with following code, I am reading from that pipe:

fp = fdopen(pipe_fds[0], "r"); 

And when ever I get something, I print it out by:

while (fgets(buf, 200, fp)) {
    printf("%s", buf);
}

What I want is, when for certain amount of time nothing appears on the pipe to read from, I want to know about it and do:

printf("dummy");

Can this be achieved by select() ? Any pointers on how to do that will be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

标点 2024-12-05 03:57:55

假设您想等待 5 秒钟,然后如果没有任何内容写入管道,则打印出“dummy”。

fd_set set;
struct timeval timeout;

/* Initialize the file descriptor set. */
FD_ZERO(&set);
FD_SET(pipe_fds[0], &set);

/* Initialize the timeout data structure. */
timeout.tv_sec = 5;
timeout.tv_usec = 0;

/* In the interest of brevity, I'm using the constant FD_SETSIZE, but a more
   efficient implementation would use the highest fd + 1 instead. In your case
   since you only have a single fd, you can replace FD_SETSIZE with
   pipe_fds[0] + 1 thereby limiting the number of fds the system has to
   iterate over. */
int ret = select(FD_SETSIZE, &set, NULL, NULL, &timeout);

// a return value of 0 means that the time expired
// without any acitivity on the file descriptor
if (ret == 0)
{
    printf("dummy");
}
else if (ret < 0)
{
    // error occurred
}
else
{
    // there was activity on the file descripor
}

Let's say you wanted to wait 5 seconds and then if nothing was written to the pipe, you print out "dummy."

fd_set set;
struct timeval timeout;

/* Initialize the file descriptor set. */
FD_ZERO(&set);
FD_SET(pipe_fds[0], &set);

/* Initialize the timeout data structure. */
timeout.tv_sec = 5;
timeout.tv_usec = 0;

/* In the interest of brevity, I'm using the constant FD_SETSIZE, but a more
   efficient implementation would use the highest fd + 1 instead. In your case
   since you only have a single fd, you can replace FD_SETSIZE with
   pipe_fds[0] + 1 thereby limiting the number of fds the system has to
   iterate over. */
int ret = select(FD_SETSIZE, &set, NULL, NULL, &timeout);

// a return value of 0 means that the time expired
// without any acitivity on the file descriptor
if (ret == 0)
{
    printf("dummy");
}
else if (ret < 0)
{
    // error occurred
}
else
{
    // there was activity on the file descripor
}
枯叶蝶 2024-12-05 03:57:55

IIRC,select 有一个超时,然后您可以使用 FD_ISSET 检查它是否是 I/O 返回。

IIRC, select has a timeout that you then check with FD_ISSET to tell if it was I/O or not that returned.

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