免费注册 查看新帖 |

Chinaunix

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

[C] fsync调用完之后为什么fd就废了? [复制链接]

论坛徽章:
3
亥猪
日期:2013-08-28 12:50:23白羊座
日期:2013-11-25 12:55:50酉鸡
日期:2014-02-12 10:46:13
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-08-07 16:54 |只看该作者 |倒序浏览
fsync调用完之后为什么fd就废了?
fsync 之后 调read 时把fd传进去 返回-1 错误打印“Bad file number”
不知道fsync对fd做了什么。。。。。。
代码如下:
  1. int main()

  2. {
  3.         int fd,byteswrite,bytesread;
  4.         char buf[256];
  5.         int length;
  6.         memset(buf,0,256);
  7.         strcpy(buf,"hello ,this is a test");
  8.         length = strlen(buf);
  9.         fd = creat("test.txt", O_RDWR|0777);
  10.         byteswrite = write (fd,buf,length);
  11.         printf("byteswrite is %d \n",byteswrite);
  12.         if(fsync(fd)==-1)
  13.                 printf("fsync error\n");
  14.         struct stat stat_buf;
  15.         int ret;
  16.         ret = fstat(fd,&stat_buf);
  17.         if(ret == -1){
  18.             printf("%s \n",strerror(errno));
  19.             exit(1);
  20.         }
  21.         memset(buf,0,256);
  22.         printf(" fd : %d \n",fd);
  23.         //close(fd);
  24.         //fd = open("test.txt",O_RDWR,0);
  25.         bytesread = read(fd,buf,length);
  26.         if(bytesread == -1){
  27.             printf("%s \n",strerror(errno));
  28.             exit(1);
  29.         }
  30.         printf("bytesread is %d \n",bytesread);
  31.         printf("read result is %s\n",buf);

  32. }
复制代码
执行结果:
  1. byteswrite is 21
  2. fd : 3
  3. Bad file number
复制代码
该问题源自一老帖子http://bbs.chinaunix.net/thread-617996-1-1.html,看了之后就郁闷了{:3_190:} 。。。今儿旧帖重贴,大神们指点指点 {:3_193:}

论坛徽章:
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
2 [报告]
发表于 2013-08-07 17:14 |只看该作者
fd = creat("test.txt", O_RDWR|0777);

至少creat第二个参数是不能填O_RDWR的.

另外, creat打开是不可读的.
creat is equivalent to open with flags equal to O_CREAT|O_WRONLY|O_TRUNC.

论坛徽章:
3
亥猪
日期:2013-08-28 12:50:23白羊座
日期:2013-11-25 12:55:50酉鸡
日期:2014-02-12 10:46:13
3 [报告]
发表于 2013-08-08 09:13 |只看该作者
linux_c_py_php

朋友真是一针见血啊   {:3_182:}
第一点 
至少creat第二个参数是不能填O_RDWR的.
 的确是我疏忽了,这个老接口被后来的OPEN给兼容了,我以为参数也是一致的^_^
第二点 
另外, creat打开是不可读的.
 我更没留意了,嘿 
原因找出来了,应该就是因为creat返回的FD是只写的,对于读接口来说是非法的,所以如果为读专门open一次,返回一个供read的FD,应该就没问题了。总之是由于读写共用一个FD,搞混乱了。。。如果在之前的基础上稍微改一下,应该还是行得通的  嘿
        int fd,byteswrite,bytesread;
        char buf[256];
        int length;
        memset(buf,0,256);
        strcpy(buf,"hello ,this is a test";
        length = strlen(buf);
//        fd = creat("test.txt", S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        fd = open("test.txt",O_CREAT|O_TRUNC|O_RDWR,0666);
        byteswrite = write (fd,buf,length);
        printf("byteswrite is %d \n",byteswrite);
        if(fsync(fd)==-1)
                printf("fsync error\n";
        struct stat stat_buf;
        int ret;
        ret = fstat(fd,&stat_buf);
        if(ret == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        memset(buf,0,256);
        printf(" fd : %d \n",fd);
        //close(fd);
        //fd = open("test.txt",O_RDWR,0);
        int offset;
        offset = lseek(fd,0,SEEK_CUR);
        printf(" offset : %d \n",offset);
        offset = lseek(fd,(-1)*offset,SEEK_CUR);
        if(offset == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        bytesread = read(fd,buf,length);
        if(bytesread == -1){
            printf("%s \n",strerror(errno));
            exit(1);
        }
        printf("bytesread is %d \n",bytesread);
        printf("read result is %s\n",buf);

多谢朋友指正

论坛徽章:
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
4 [报告]
发表于 2013-08-08 12:44 |只看该作者
解决了就好啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP