Since Linux 2.6.33, sendfile supports regular file as output file.
That is, sendfile can be used as way for fast file copy.
(Faster than normal read -> write operation because it is done inside kernel! )
Please refer follow example code ...
...
#include <stdio.h> #include <sys/sendfile.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main(int argc, const char *argv[]) { int ifd, ofd, r; struct stat st; r = stat(argv[2], &st); r |= (ofd = open(argv[1], O_WRONLY | O_CREAT)); r |= (ifd = open(argv[2], O_RDONLY)); if (r < 0) return 1; if (-1 == sendfile(ofd, ifd, 0, st.st_size)) return 1; fchmod(ofd, 0664); return 0; }
...
'Language > C&C++' 카테고리의 다른 글
[GCC] Initialize array... (0) | 2014.07.25 |
---|---|
[Linux] Using named pipe in linux (주의할 점) (0) | 2014.01.09 |
일반 file에 대한 select/poll... (0) | 2013.11.14 |
Using allocated memory like 2-D array. (0) | 2013.07.10 |
[C/C++][Linux] Easy mistake regarding detaching when using pthread_create() without pthread_join(). (0) | 2012.10.05 |