memory map program --------uses mmap function to copy file fd to stdout

来源:互联网 发布:dedecms 帝国 cms 编辑:程序博客网 时间:2024/05/16 08:28
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/mman.h>//PROT_READ header file
#include<string.h>
#include<fcntl.h>//O_RDONLY header file
void mmapcopy(int fd, int size)
{
char *bufp;
bufp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
write(1, bufp, size);
return ;
}
int main(int argc, char **argv)
{
struct stat stat1;
int fd;
if(argc != 2)
{
printf("usage: %s <filename>\n", argv[0]);
exit(0);
}
fd = open(argv[1], O_RDONLY, 0);
fstat(fd, &stat1);
mmapcopy(fd, stat1.st_size);
exit(0);
}
原创粉丝点击