I have a process master that spawns N child processes that communicate with the parent through unnamed pipes. I must be able to:
- make the father open the file and then send, to each child, a struct telling that it has to read from min to max line;
- this is going to happen at the same time, so I don't know:
- 1st how to divide total_lines for N maps and
- 2nd how do I make each child read just the lines it is supposed to?
My problem does not concern the O.S. concepts, only the file operations :S
Perhaps fseek? I can't mmap the log file (some have more than 1GB).
I would appreciate some ideas. Thank you in advance
EDIT: I'm trying to make the children read the respective lines without using fseek and the value of chunks, so, could someone please tell me if this is valid? :
//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++;
}
//(...)
}
I don't know if it's going to work, I just to make it work! Even like this, is it possible to outperform a single process reading the whole file and printing the total number of ips found in the log file?