親プロセスが多数の子プロセスをフォークし、これらの子プロセスが計算を実行し、パイプを介して結果を親プロセスに返すシナリオを実装しています。子プロセスは外部ライブラリのデータ型(GMPのmpz_t型)を使用する必要があるため、このライブラリ独自の出力ストリーム関数を使用する必要があります。この関数は、ファイル記述子の代わりにファイルポインタを入力として許可します。したがって、パイプの書き込み終了のファイルポインタを取得し、データの書き込みに使用します。以下に、コードの子プロセス部分と親プロセス部分を示します。
pid_t ppid;
ppid = getpid();
struct sigaction sig;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
sig.sa_handler = sig_usr;
if(sigaction(SIGINT,&sig,NULL) != 0)
printf("\ncan't catch SIGINT\n");
if(sigaction(SIGUSR1,&sig,NULL) != 0)
printf("\ncan't catch SIGINT\n");
pid_t childpid;
pid_t childpids[operand1Length*operand2Length];
int childPIDInd = 0;
//Create pipe: (must do before fork() so FDs are inherited by child)
int pipefd[2]; //array to hold pipe FDs
pipe(pipefd);
for(i=operand2Length-1, k=0; i>=0; i--, k++){
for(j=operand1Length-1, l=0; j>=0; j--, l++){
childpid = fork();
switch(childpid){
case -1:
//fork error
perror("fork failed!\n");
exit(EXIT_FAILURE);
case 0:
close(pipefd[0]);
subOperandLength = subOperands[k].length;
FILE* fp = NULL;
fhe_mul(subOperands[k].operand[subOperandLength-1-k-l], num1->operand[j], num2->operand[i], pk);
while(WritePermit); // unless parent process sends a signal any child process cannot enter this critical section.
fp = fdopen(pipefd[1], "w");
if(fp == NULL)
fprintf(stderr, "Child Process #%d file pointer is NULL. Error: %s. Pipe FD: %d\n", getpid(), strerror(errno), pipefd[1]);
//Except the child process which enters the critical section first,
//for all other child processes fp is NULL.
gmp_fprintf(fp, "%Zd\n", subOperands[k].operand[subOperandLength-1-k-l]);
gmp_fprintf(fp, "%d\n", k);
gmp_fprintf(fp, "%d\n", subOperandLength-1-k-l);
fflush(fp);
fclose(fp);
kill(ppid, SIGUSR1);
exit(EXIT_SUCCESS);
default:
childpids[childPIDInd] = childpid;
childPIDInd++;
close(pipefd[1]);
if(i == 0 && j == 0){ // last child was created
kill(childpids[0], SIGINT);
mpz_t deneme;
mpz_init(deneme);
FILE* fs = fdopen(pipefd[0], "r");
int forIndex, pidIndex;
for(forIndex=0, pidIndex=1; forIndex<4; forIndex++, pidIndex++){
while(WritePermit2);
while((gmp_fscanf(fs, "%Zx\n", &deneme)) > 0){
gmp_fprintf(stdout, "Parent Process #%d: %Zd\n", getpid(), deneme);
}
kill(childpids[pidIndex], SIGINT);
WritePermit2=1;
}
fclose(fs);
int status;
int i=0;
int clean = 1;
while (i < operand1Length*operand2Length) {
wait(&status);
if(!WIFEXITED(status))
clean = 0;
i++;
}
if(!clean){
printf("I am having some problems with my children! :'(\n");
exit(EXIT_FAILURE);
}
}
}
}
子プロセスの1つに対してのみfpが機能します。その後、どういうわけか、それはNULLになるので、gmp_fprintfは他の子プロセスでエラーを発生させます。
コードの詳細が必要かどうか、遠慮なくお問い合わせください。よろしくお願いします!
コードが存在するdefault:
場合switch
、二重にネストされたfor
ループ内ののケースには、が含まれますclose(pipefd[1]);
。明らかに、これは内部ループの最初の反復でのみ機能します。その後、パイプが壊れます。後続の子は、親がファイル記述子を閉じたため、操作パイプを取得しません。
修正は、すべての子が作成されるまで、親がパイプの書き込み端を閉じないようにすることです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加