免费注册 查看新帖 |

Chinaunix

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

sendfile issue [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-24 14:52 |只看该作者 |倒序浏览
大家先看看code吧~!

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sendfile.h>

int main()
{
        int s = open("tux", O_RDONLY);
        int d = open("output", O_WRONLY | O_CREAT, 0600);
        off_t off = 0;
        ssize_t size = sendfile(d, s, &off, 10000);
                if(size <= 0)
                {
                        perror("sendfile");
                }
        printf("size = %d, off = %d\n", size, off);
}


编辑运行之后出错.
sendfile: Invalid argument
size = -1, off = 0

tux文件确实存在,而且是450M的文件.open应该没有错的.请大家帮忙看看!谢谢

论坛徽章:
0
2 [报告]
发表于 2009-04-24 15:42 |只看该作者
咋就没有人理我么~????

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
3 [报告]
发表于 2009-04-24 16:38 |只看该作者
sendfile 从磁盘文件 到网络

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
4 [报告]
发表于 2009-04-24 16:39 |只看该作者
也可以使用splice完成你的要求, 需要Linux内核版本 2.6.28 以上

论坛徽章:
0
5 [报告]
发表于 2009-04-24 16:51 |只看该作者
那具体说说怎么实现呢~~

sendfile()是必须的

论坛徽章:
0
6 [报告]
发表于 2009-04-24 16:55 |只看该作者
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations  (i.e.,
   it cannot be a socket); and out_fd must refer to a socket.

In  Linux  2.4  and  earlier,  out_fd  could refer to a regular file, and sendfile() changed the current
       offset of that file.

论坛徽章:
0
7 [报告]
发表于 2009-04-24 17:10 |只看该作者
原帖由 zhoubug 于 2009-4-24 16:55 发表
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations  (i.e.,
   it cannot be  ...



这个我知道啊~~那在2.6的kernel里面,怎么样让out_fd为一个socket呢?

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
8 [报告]
发表于 2009-04-25 06:49 |只看该作者
你就是复制文件吧, 直接读取写入新文件, 还可以用splice() 需要一个中间管道, 也和sendfile()一样是零拷贝的,
sendfile()内部也是用splice()实现的.
内核版本 2.6.28以上

论坛徽章:
0
9 [报告]
发表于 2009-04-25 07:59 |只看该作者
请问你想做什么?

你不能在 2.6 的内核环境下进行从常规文件到常规文件的 sendfile()。
out_fd 可以是 socket,这个需要你使用 BSD sockets 接口。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
10 [报告]
发表于 2009-04-25 11:11 |只看该作者

回复 #9 langue 的帖子

很明显是复制文件
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP