如何设定旅游网站seo核心关键词,ps最好用的素材网站,汕头网站建设方案优化,手机版网站开发实例Linux笔记之使用系统调用sendfile高速拷贝文件
code review! 文章目录 Linux笔记之使用系统调用sendfile高速拷贝文件sendfile 性能优势sendfile 系统调用优点#xff1a;缺点#xff1a; cp 命令优点#xff1a;缺点#xff1a; 实际测试#xff1a;拷贝5.8个G的文件sendfile 性能优势sendfile 系统调用优点缺点 cp 命令优点缺点 实际测试拷贝5.8个G的文件cp使用25ssendfile只用7.49s快了四倍 sendfile 系统调用是 Linux 特有的函数用于在两个文件描述符之间直接传输数据。这可以减少用户态和内核态之间的数据拷贝从而提高文件拷贝的性能。sendfile 通常比使用普通的 read 和 write 系统调用更快尤其是在处理大文件时。
sendfile 性能优势
减少数据拷贝sendfile 在内核内直接把数据从一个文件描述符传输到另一个文件描述符而不需要将数据从内核态拷贝到用户态再拷贝回内核态。减少系统调用的开销对于大文件拷贝sendfile 只需要一次系统调用而传统方法可能需要多次 read 和 write 调用。更少的上下文切换由于减少了系统调用次数sendfile 也减少了用户态和内核态之间的上下文切换。
sendfile 系统调用
sendfile 它主要用于网络传输但也可以用于文件复制。sendfile 的设计目的是减少数据复制的次数从而提高性能。
优点
减少上下文切换 sendfile 可以直接在内核空间处理数据传输避免了用户空间和内核空间之间的上下文切换。高效 它可以减少内存拷贝的次数因为数据不需要从内核空间拷贝到用户空间再回到内核空间。适合大文件传输 对于大文件传输sendfile 可以显著减少 CPU 占用和提高传输速度。
缺点
灵活性较低 sendfile 主要用于文件描述符之间的传输不如一些用户空间的工具灵活。依赖于内核版本 sendfile 的性能和功能可能会受到内核版本的影响。
cp 命令
cp 是一个用户空间工具用于复制文件和目录。它使用标准的文件 I/O 操作来实现文件复制。
优点
易用性 cp 命令简单易用支持各种选项可以处理复杂的文件复制需求。广泛支持 几乎所有的 Unix/Linux 系统都支持 cp 命令且不依赖于特定的内核版本。灵活 cp 命令支持递归复制、保留文件属性等多种操作。
缺点
性能较低 由于 cp 命令在用户空间运行涉及多次上下文切换和内存拷贝性能可能不如 sendfile。高 CPU 使用率 对于大文件复制cp 命令可能会占用较高的 CPU 资源。
实际测试拷贝5.8个G的文件cp使用25ssendfile只用7.49s快了四倍 代码
#include sys/sendfile.h
#include fcntl.h
#include unistd.h
#include iostream
#include chrono
#include sys/stat.hbool copyFileSendfile(const std::string src, const std::string dest) {int source open(src.c_str(), O_RDONLY);int destination open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);if (source 0 || destination 0) {std::cerr Error opening files! std::endl;return false;}struct stat stat_buf;fstat(source, stat_buf);off_t offset 0;auto start std::chrono::high_resolution_clock::now();if (sendfile(destination, source, offset, stat_buf.st_size) -1) {std::cerr Error using sendfile! std::endl;close(source);close(destination);return false;}auto end std::chrono::high_resolution_clock::now();close(source);close(destination);std::chrono::durationdouble duration end - start;std::cout Sendfile copy took duration.count() seconds std::endl;return true;
}int main(int argc, char* argv[]) {if (argc ! 3) {std::cerr Usage: argv[0] source_file destination_file std::endl;return 1;}std::string sourceFile argv[1];std::string destinationFile argv[2];if (copyFileSendfile(sourceFile, destinationFile)) {std::cout File copied successfully! std::endl;} else {std::cerr File copy failed! std::endl;return 1;}return 0;
}编译
g main.cpp -o main
文章转载自: http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.xqnzn.cn.gov.cn.xqnzn.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.c7629.cn.gov.cn.c7629.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.kztts.cn.gov.cn.kztts.cn http://www.morning.wngpq.cn.gov.cn.wngpq.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.tyrlk.cn.gov.cn.tyrlk.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.nlgyq.cn.gov.cn.nlgyq.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.gcrlb.cn.gov.cn.gcrlb.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn http://www.morning.zycll.cn.gov.cn.zycll.cn