我有一个进程主进程,它产生N 个通过未命名管道与父进程通信的子进程。我必须能够:
- 让父亲打开文件,然后向每个孩子发送一个结构,告诉它必须从min到max行读取;
- 这将同时发生,所以我不知道:
- 第一个如何为 N 个地图划分 total_lines 和
- 第二,我如何让每个孩子只读它应该读的行?
我的问题不涉及操作系统概念,只涉及文件操作:S
也许fseek?我无法映射日志文件(有些超过 1GB)。
我会很感激一些想法。先感谢您
编辑:我试图让孩子们在不使用 fseek 和块的值的情况下阅读相应的行,所以,有人可以告诉我这是否有效吗?:
//somewhere in the parent process:
FILE* logFile = fopen(filename, "r");
while (fgets(line, 1024, logFile) != NULL) {
num_lines++;
}
rewind(logFile);
int prev = 0;
for (i = 0; i < maps_nr; i++) {
struct send_to_Map request;
request.fp = logFile;
request.lower = lowLimit;
request.upper = highLimit;
if (i == 0)
request.minLine = 0;
else
request.minLine = 1 + prev;
if(i!=maps_nr-1)
request.maxLine = (request.minLine + num_lines / maps_nr) - 1;
else
request.maxLine = (request.minLine + num_lines / maps_nr)+(num_lines%maps_nr);
prev = request.maxLine;
}
//write this structure to respective pipe
//child process:
while(1) {
...
//reads the structure to pipe (and knows which lines to read)
int n=0, counter=0;
while (fgets(line, 1024, logFile) != NULL){
if (n>=minLine and n<=maxLine)
counter+= process(Line);//returns 1 if IP was found, in that line, between the low and high limit
n++;
}
//(...)
}
我不知道它是否会起作用,我只是让它起作用!即使这样,是否有可能优于读取整个文件并打印在日志文件中找到的 ips 总数的单个进程?