名前のないパイプを介して親と通信するN 個の子プロセスを生成するプロセス マスターがあります。私はできる必要があります:
- 父親にファイルを開かせてから、最小行から最大行まで読み取る必要があることを伝える構造体を各子に送信します。
- これは同時に起こるので、私にはわかりません:
- 最初の N マップの total_lines の分割方法と
- 2番目に、各子供に本来あるべき行だけを読ませるにはどうすればよいですか?
私の問題はOSの概念には関係なく、ファイル操作のみに関係しています:S
おそらくfseek?ログ ファイルを mmap できません (1 GB を超えるものもあります)。
いくつかのアイデアをいただければ幸いです。前もって感謝します
編集: 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 の総数を出力する単一のプロセスよりも優れたパフォーマンスを発揮できますか?