免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1398 | 回复: 8
打印 上一主题 下一主题

GNU C Library 里的一句话理解不了,请高手指教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-10-10 10:33 |只看该作者 |倒序浏览
在select()函数中,解释select的机制时,有这么一句话:
A file descriptor is considered ready for reading if it is at end of file.
file descriptor就是select的参数,我的理解应该就是一个句柄.里面先说过select来监查代进来的file descriptor有没有activity,如果有的话就开始处理数据,但是在讲如何判有没有activity的时候,就是上面的一句话.请问哪位高手知道如何理解?file descriptor 怎么可能在文件里嘛?!

论坛徽章:
0
2 [报告]
发表于 2003-10-10 10:35 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

上下文如下:


Next, here is the description of the select function itself.

Function: int select (int nfds, fd_set *read-fds, fd_set *write-fds, fd_set *except-fds, struct timeval *timeout)
    The select function blocks the calling process until there is activity on any of the specified sets of file descriptors, or until the timeout period has expired.

    The file descriptors specified by the read-fds argument are checked to see if they are ready for reading; the write-fds file descriptors are checked to see if they are ready for writing; and the except-fds file descriptors are checked for exceptional conditions. You can pass a null pointer for any of these arguments if you are not interested in checking for that kind of condition.

    A file descriptor is considered ready for reading if it is at end of file. A server socket is considered ready for reading if there is a pending connection which can be accepted with accept; see section Accepting Connections. A client socket is ready for writing when its connection is fully established; see section Making a Connection.
tommy 该用户已被删除
3 [报告]
发表于 2003-10-10 10:50 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2003-10-10 10:59 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

嗯,好像是这个意思,多谢了!不过你怎么想到at end of file.就是socket被关闭或者shutdown的意思的呢?

论坛徽章:
0
5 [报告]
发表于 2003-10-10 12:01 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

select用来判断普通文件的读写时

论坛徽章:
0
6 [报告]
发表于 2003-10-10 12:29 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

相当于:
A file descriptor is considered ready for reading if the file position pointer is at end of file.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-10-10 12:54 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

这个问题的核心在于操作系统的对于描述符的处理方式。

一般的,对于socket描述符操作系统是通过一个特殊的文件系统来管理的,这个文件系统在系统boot的过程中mount上的,每个进程的PCB块中有相应的数据结构对应于这个描述符。类似的机制有管道,和随进程持续的IPC对象。

对于文件系统的描述符,能不能处理取决与这个描述符有没有在PCB中做相应的activity处理的函数指针,如果这个指针为空,那么可能是操作系统的开发者预留的功能,只是当时候由于某些原因没有添加。如果不为空,那么这个描述符可以用select处理。

更详细的描述,可参看操作系统相关资料。

论坛徽章:
0
8 [报告]
发表于 2003-10-10 16:10 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

when i test theses functions in the code below, it report an error like :
Transport endpoint is not connected
who can tell me why ? and how to correct it? thanks!

  1. #define PATH "/tmp/DC/data"
  2. #ifdef HAVE_CONFIG_H
  3. #include <config.h>;
  4. #endif

  5. #include <stdio.h>;
  6. #include <stdlib.h>;
  7. #include <stddef.h>;
  8. #include <errno.h>;
  9. #include <stdlib.h>;
  10. #include <sys/socket.h>;
  11. #include <sys/un.h>;

  12. void  write_to_server (int filedes)
  13. {
  14.   int nbytes;
  15.   char *message = "Hey Guy!";
  16.   nbytes = write (filedes,message , 2);
  17.   if (nbytes < 0)
  18.     {
  19.       perror ("write:::::");
  20.       unlink(PATH);
  21.       exit (EXIT_FAILURE);
  22.     }
  23. }



  24. int make_named_socket (const char *filename)
  25. {
  26.   struct sockaddr_un name;
  27.   int sock;
  28.   size_t size;

  29.   /* Create the socket. */

  30.   sock = socket (PF_UNIX, SOCK_DGRAM, 0);
  31.   if (sock < 0)
  32.     {
  33.       perror ("socket");
  34.       unlink(PATH);
  35.       exit (EXIT_FAILURE);
  36.     }

  37.   /* Bind a name to the socket. */
  38. //  unlink(PATH);
  39.   name.sun_family = AF_FILE;
  40.   strcpy (name.sun_path, filename);

  41.   /* The size of the address is
  42.      the offset of the start of the filename,
  43.      plus its length,
  44.      plus one for the terminating null byte. */
  45.   size = (offsetof (struct sockaddr_un, sun_path)
  46.           + strlen (name.sun_path) + 1);

  47.   if (bind (sock, (struct sockaddr *) &name, size) < 0)
  48.     {
  49.       perror ("bind");
  50.       unlink(PATH);      
  51.       exit (EXIT_FAILURE);
  52.     }

  53.   return sock;
  54. }



  55. int main(int argc, char *argv[])
  56. {
  57.   int sock_fd;

  58.   static char *path = PATH;

  59.   sock_fd = make_named_socket(path);
  60.   write_to_server (sock_fd);

  61.   return EXIT_SUCCESS;
  62. }

复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2003-10-10 22:59 |只看该作者

GNU C Library 里的一句话理解不了,请高手指教

你这个代码有点问题。make_named_socket只是个监听端口的socket,只能算半连接。write_to_server的参数必须是建立了连接的socket,即accept客户端connect后返回的描述字。
unix domain socket和tcp/ip的socket的交互过程是一样的,例程很多。参考一下吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP