《linux c 编程实战 》 -我的纠错笔记

一切仅作为参考,欢迎任何人讨论

 

  [页数] 151     [位置] my_rwl.c [类型]  代码错误     

       [描述]   从书上看 __LINE__,是一个下划线,但实际上内置宏是前后各两个下滑线组成的,即 _ _LINE_ _

 

  [页数] 195     [位置] studyuid.c [类型]  代码错误     

       [描述]   strerror()的头文件是string.h ,需要添加之后才能代码才能运行

 

  [页数] 251    [位置] ctrlpocess.c [类型]  代码错误     

       [描述]   read 的函数声明 :ssize_t read(int fd, void *buf, size_t count),而 stdin 的声明是extern FILE *stdin,因此 read(stdin,buf,1024)不匹配,改成read(0,buf,1024)

 

   [页数] 299   [位置] 中部         [类型]  描述偏差          PS :待讨论

       [描述]   int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);

                  对nfds 是这么解释的 :需要监视的文件描述符数,man之后的解释是

                  nfds is the highest-numbered file descriptor in any of the three sets【 在所有的文件描述符中最高值的大小】

                   使用建议:取最大的文件描述符加一

 

   [页数] 299   [位置] 中部         [类型]  描述不足          PS :待讨论

       [描述]  对FD_ISSET( int fd,fd_set *set )是这么解释的: 测试fd是否在set里

                  这么解释是对的,但缺少铺垫容易导致误导,应该再加上一句,select函数之后,会改变 set的值,只保留有状态【可读,可写,异常】的文件描述符, 

                      

socket   s; 
..... 
fd_set   set; 
while(1) 
{     
    FD_ZERO(&set);//将你的套节字集合清空 
    FD_SET(s,   &set);//加入你感兴趣的套节字到集合,这里是一个读数据的套节字s 
    select(0,&set,NULL,NULL,NULL);//检查套节字是否可读, 
                                                      //很多情况下就是是否有数据(注意,只是说很多情况) 
                                                      //这里select是否出错没有写 
    if(FD_ISSET(s,   &set)   //检查s是否在这个集合里面, 
    {                                           //select将更新这个集合,把其中不可读的套节字去掉 
                                                //只保留符合条件的套节字在这个集合里面 
                                
            recv(s,...); 
    
    } 
    //do   something   here 
}

 


               依据[都是根据man之后描述推测的,如果能提供更加可靠的描述,要联系我呀]

             

               一  man 的 description部分

                     Three independent sets of file descriptors are watched. Those listed
                     in readfds will be watched to see if characters become available for
                      reading 【可读文件集中的元素将被检测其是否成文可读文件描述符,】

                      (more precisely, to see if a read will not block; in particu‐
                     lar, a file descriptor is also ready on end-of-file【更加精确的是,看

                     可读设置是否被阻塞,尤其是,对于文件描述符,其文件结尾是否可读】), those in

                     writefds will be watched to see if a write will not block, and those in
                     exceptfds will be watched for exceptions. On exit, the sets are modi‐
                     fied in place to indicate which file descriptors actually changed sta‐
                     tus.【select 函数在推出时,这些文件集合将被修改以便于显示那些文件描述符改变了状态】

              二  man 的 return 部分

                   On success, select() and pselect() return the number of file descrip‐
                   tors contained in the three returned descriptor sets (that is, the
                    total number of bits that are set in readfds, writefds, exceptfds)

                          【 成功的话 ,select和pselect函数将返回包含在三个文件集合中的文件描述符个数】

 

 

 

posted on 2015-01-30 22:08  独独  阅读(175)  评论(0编辑  收藏  举报

导航