免费注册 查看新帖 |

Chinaunix

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

[函数] 下面的code为什么select每次都返回1 ?我只执行一次写入操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-30 18:32 |只看该作者 |倒序浏览
code 如下,fifo的另一端我只执行了一次写入操作,然后下面的select就每次都返回1,为什么呢?:

#define FIFO "/tmp/myfifo"

main(int argc,char** argv)
{
    char buf_r[100];
    int fd, retval;     
    int  nread;                                                         
    fd_set rfds;        
    struct timeval tv;                                                  
               
    if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))               
        printf("cannot create fifoserver\n");                           
                                                                        
    printf("Preparing for reading bytes...\n");                        
            
    memset(buf_r,0,sizeof(buf_r));                                      
            
    fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);                                
    if(fd==-1)
    {           
        perror("open");
        exit(1);                                                        
    }                                                                  
    while(1)
    {           
        memset(buf_r,0,sizeof(buf_r));                                 
        tv.tv_sec = 60;
        tv.tv_usec = 0;
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);
        retval = select(fd + 1, &rfds, NULL, NULL, &tv);
        printf("retval:%d\n",retval);

        if (retval == 0)
        {
            printf("timeout\n");
        }
        else
        {
            if((nread=read(fd,buf_r,100))==-1)
            {
                if(errno==EAGAIN)
                    printf("no data yet\n");
            }
            printf("read %s from FIFO\n",buf_r);
            //sleep(1);
        }
    }
    pause();
    unlink(FIFO);
}

论坛徽章:
4
白羊座
日期:2013-09-17 21:59:30技术图书徽章
日期:2013-10-12 22:16:03白羊座
日期:2013-10-14 11:01:40双子座
日期:2013-12-17 18:26:39
2 [报告]
发表于 2013-06-30 19:19 |只看该作者
本帖最后由 井蛙夏虫 于 2013-06-30 19:21 编辑

回复 1# cpu0018
man -s 3 read或者man -s 3p read
  1. When attempting to read from an empty pipe or FIFO:

  2.         * If  no  process has the pipe open for writing, read() shall return 0
  3.           to indicate end-of-file.

  4.         * If some process has the pipe open for writing and O_NONBLOCK is set,
  5.           read() shall return -1 and set errno to [EAGAIN].

  6.         * If  some  process  has  the  pipe open for writing and O_NONBLOCK is
  7.           clear, read() shall block the calling  thread  until  some  data  is
  8.           written  or  the  pipe  is closed by all processes that had the pipe
  9.           open for writing.
复制代码
或者
unp卷2图4.21


   

论坛徽章:
0
3 [报告]
发表于 2013-06-30 19:36 |只看该作者
回复 2# 井蛙夏虫


    您好,我问的是select的返回值,而不是read的返回值。谢谢。

论坛徽章:
0
4 [报告]
发表于 2013-07-01 10:55 |只看该作者
有谁知道吗?

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
5 [报告]
发表于 2013-07-01 11:22 |只看该作者
写端关闭, 读端EOF了, read返回0
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP