English 中文(简体)
是否有办法将档案描述与用户定义的数据联系起来?
原标题:Is there any way to associate a file descriptor with user defined data?

我正在撰写一份客户服务申请,并使用POSIXpoll功能,提供一种同时处理客户的方式。 客户也有国家数据和其他相关数据,储存在客户结构中。

My immediate problem is that when I get a hint from poll to do I/O on a socket file descriptor that is associated with a client (conceptually), I have to actually match the file descriptor to its associated client data structure. Currently I do a O(n_clients) lookup (my client data structure stores the descriptor), but I was wondering whether there exists a better alternative?

最佳回答

页: 1 如果有的话,该数据就必须由纸浆公司跟踪,因此,研究这一数据需要一个系统电话。 该系统电话的费用比在用户空间进行O(n)查询的费用要高得多。

How many clients are you dealing with at once? Unless it s on the order of hundreds or more, the cost of a lookup is going to be miniscule compared to the cost of doing any sort of I/O.

你们不使用“O(n)”的眼光,也只能使用由档案管理员编制索引的阵列,假定你们获得的口号超过一定数量的供述人。 例如:

#define MY_MAX_FD 1024  // Tune this to your needs
void *per_fd_data[MY_MAX_FD];

void *get_per_fd_data(int fd)
{
    assert(fd >= 0);
    if(fd < MY_MAX_FD)
        return per_fd_data[fd];
    else
    {
        // Look up fd in a dynamic associative array (left as an exercise to the
        // reader)
    }
}
问题回答

速度是,只建立固定的连接结构,有{状态,*context, ......, 或许有背功能} , 每一条目,, 编号为(O(1))。 记忆是廉价的,你可以支付数以百计的文字和表格条目。

EDIT:You dont need,以使其固定规模。 如果你的污染结构或烟雾固定下来:使之固定下来;否则就使用易腐化或宽松()来分配条目数目。

If you use poll() or select()/pselect() then you should keep the data yourself, e.g. in a hash table or array as others have mentioned. That is the most portable solution. Some of the alternative interfaces do have ways to associate your own user data. For example using asynchronous I/O (e.g. aio_read()), you can supply a user value sigev_value that can be passed to a signal handler or thread upon completion of the asynchronous request. The Linux epoll interface also allows user data to be specified for each file descriptor in the set.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...