我如何使用它的文件描述符获取设备的设备节点 [英] How can I get the devnode of a device using its file descriptor

查看:402
本文介绍了我如何使用它的文件描述符获取设备的设备节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我设备阵列开辟了2台设备。

For example I opened up 2 devices in an array of devices..

节点的/ dev / ttyUSB0中,/ dev / ttyUSB1等。

NODES are /dev/ttyUSB0, /dev/ttyUSB1 etc..

#define MAXDEV 4
devlist[MAXDEV];
const char *devices[] = {"/dev/ttyUSB0","/dev/ttyUSB1");


for(loop =0; loop<sizeof(devices); loop++){

    fd= open(devices[loop]);

}

现在我把它们添加到FDS名单;

Now I add them to the list of fds;

for(i=0; i<MAXDEV; i++){

if(devlist[i] != 0){
devlist[i] = fd;
fd = -1;
}

}

现在我读数据的设备。

    for(iter=0; iter<MAXDEV; iter++){

if(FD_ISSET(devlist[iter],&fds)){

if ((nbytes = read(devlist[iter], buf, sizeof(buf)-1)) > 0 && nbytes != 0)
                    {

                 buf[nbytes] = '\0';

                     printf("Data Received on Node ???");

                    }
                if(nbytes < 0){
                            printf("connection reset\n");
                            FD_CLR(devlist[iter], &fds);
                            close(devlist[iter]);
                            devlist[iter] = 0;

                        }
                        if(nbytes ==0){
                            printf("Device Removed on Node ???\n");


                        FD_CLR(devlist[iter], &fds);
                            close(devlist[iter]);
                            devlist[iter] = 0;

                        }
    }
}

现在我怎么使用它的FD获得设备节点?..谢谢。

Now how do I get the device node using its fd?.. Thanks.

推荐答案

正确的方式做到这一点是做你自己记账。这将允许你准确记录设备节点名作为由用户提供的,而不是提供一个等效的,但令人混淆不同的一个。

The proper way to do this is to do your own book-keeping. That would allow you to log the device node name exactly as supplied by the user, rather than provide an equivalent, yet confusingly different one.

例如,你可以使用哈希表,到文件描述符号码<$ C关联$ C>字符与用于设备名称阵列对应的的open()电话。

For example you could use a hash table, to associate file descriptor numbers to char arrays with the device name used for the corresponding open() call.

有一个简单,但更脆弱,绝对的的建议,解决方案可能涉及使用指针的简单数组字符用量过大,大小,因为你可能遇到的文件描述符值可以用来为指标,相应的字符串没有超出数组边界的希望。这是code比一个哈希表稍微容易些,但它的将会的会导致程序可怕的死,如果一个文件描述符值超过你的字符串数组中的最大允许指数。​​

A simpler, but far more fragile and definitely not recommended, solution might involve using a simple array of pointers to char with an inordinately large size, in the hopes that any file descriptor value that you may encounter can be used as an index to the appropriate string without going beyond the array bounds. This is slightly easier to code than a hash table, but it will cause your program to die horribly if a file descriptor value exceeds the maximum allowed index in your string array.

如果你的程序绑定到Linux平台,无论如何,你也许可以,嗯,的作弊的使用的/ dev / FD 目录或的/ proc 文件系统(更具体的的/ proc /自/ FD 目录到的/ dev / FD 通常是一个符号链接)。两者都包含符号链接,副文件描述符值,凡用于打开相应的文件路径的规范版本。例如,考虑下面的成绩单:

If your program is bound to the Linux platform anyway, you might be able to, uh, cheat by using the /dev/fd directory or the /proc filesystem (more specifically the /proc/self/fd directory to which /dev/fd is usually a symbolic link). Both contain symbolic links that associate file descriptor values to canonical versions of the paths that where used to open the corresponding files. For example consider the following transcript:

$ ls -l /proc/self/fd
total 0
lrwx------ 1 user user 64 Nov  9 23:21 0 -> /dev/pts/10
l-wx------ 1 user user 64 Nov  9 23:21 1 -> /dev/pts/10
lrwx------ 1 user user 64 Nov  9 23:21 2 -> /dev/pts/10
lr-x------ 1 user user 64 Nov  9 23:21 3 -> /proc/16437/fd/

您可以使用 的readlink() 系统调用以检索对应于所关注的文件描述符的链接的目标

You can use the readlink() system call to retrieve the target of the link that corresponds to a file descriptor of interest.

这篇关于我如何使用它的文件描述符获取设备的设备节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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