UNIX select()调用:如何结合fd_sets的? [英] unix select() call: how to combine fd_sets?

查看:88
本文介绍了UNIX select()调用:如何结合fd_sets的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我C语言编写的Linux应用程序,它使用2个独立的第三方库。这两个库是异步的,使用select()。他们还提供返回他们等待的文件描述符的API。我的目的是通过这些到我自己的选择(),然后控制返回到那个库时设置自己的FD值。

I am writing an application in C for linux, which uses 2 separate third-party libraries. Both libraries are asynchronous and use select(). They also provide an API which returns the file descriptors they wait on. My intention is to pass these into my own select() and then return control back to whichever library when their own fd values are set.

我想我已经得到了大部分写的,但我在那里的select()的参数有关的点有问题:这两个库不给个人的文件描述符,但指向其读取和写入的fd_sets。我需要返回从fd_sets的这些库合并成一个FD_SET读一FD_SET写等。

I think I've got most of it written, but I have trouble at the point where the select() parameters are concerned: Both libraries do not give individual file descriptors, but pointers to their read and write fd_sets. I need to combine the returned fd_sets from these libraries into one fd_set for read, one fd_set for write, etc.

我如何能2的fd_sets合并成一个造成FD_SET有什么建议?

Any suggestions on how I can combine 2 fd_sets into one resulting fd_set?

附录对不起!我本来应该更清楚。这些库只返回的fd_sets ......我不知道文件描述符的数量,每一套这样我可以循环做,并单独设置每个FD ..有确定的简单方法这给出只是一个FD_SET?

Addendum Sorry! I should have been clearer.. these libraries only return the fd_sets... I don't know the number of FDs in each set so that I can do a for loop and set each FD individually.. is there a simple way of determining this given just an fd_set?

推荐答案

您可以设置自己的 FD_SET 通过遍历所有可能的文件描述符,每个库可能可能有公开,他们的每一个回台,并相应设置的。这是一个有点蛮力而是选择界面不幸相当原始。例如:

You can set up your own fd_set by looping over all possible file descriptors that each library could possibly have open and in each of their returned sets and set your's accordingly. It's a bit brute-force but the select interface is unfortunately quite primitive. For example

fd_set *lib1_read_fds, *lib2_read_fds;
int fd;
fdset my_fd_set;
FD_CLR(&my_fd_set);
for (fd = 0; fd < FD_SETSIZE; fd++) {
    if (FD_ISSET(fd, lib1_read_fds) || FD_ISSET(fd, lib2_read_fds)) {
        FD_SET(fd, &my_fd_set);
    }
}

这篇关于UNIX select()调用:如何结合fd_sets的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆