我正在尝试读取大文件并将此文件映射到内存,然后通过将不同的启动和停止地址传递给不同的进程来分配工作负载,然后计算所有进程文件中所有单词的出现。下面的代码检查它是否是一个文件,打开要读取的文件并获取文件的大小。我不确定如何使用这个mmap()函数。具体来说,mmap()作为参数。偏移量应该是多少?我从哪里得到这个值?如何在c中使用mmap()函数
此外,如何使用fork()来创建子进程?
stat(argv[1], &fileStat);
// Check if the INPUT is a file.
if (S_ISREG(fileStat.st_mode)) {
type = "file";
file = fopen(argv[1], "r");
if (!file)
perror("ERROR: Failed to open the file!");
// Check the total size of the file
stat(file, &fileStat);
size = fileStat.st_size;
// map file to memory and divide workload by passing different
// starting address and stopping address to different processes
addr = mmap(0, size, PROT_READ , MAP_SHARED , file, off_set);
// Count word occurrences
//count_words(file, words);
fclose(file);
} else {
perror("ERROR: Not a file!");
exit(1);
}
阅读手册页,这是很清楚的:http://man7.org/linux/man-pages/man2/ mmap.2.html – Deduplicator 2014-11-21 19:30:05