あるコースで、先生は私たちに(チョークボードで)いくつかのコードを教えてくれましたが、彼はひどい手書きをしていて、私はいくつかの部分を作ることができません。また、パイプとフォークの両方で新しいので、それは役に立ちません。
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void main () {
int a[] = {1,2,3,4}, f[2]; // ok so we initialize an array and a "pipe folder" (pipefd in manual) ?
pipe (f); // not 100% sure what this does ?
if (fork () == 0) { // if child
close (f[0]); // close pipe read-end
a[0] += a[1];
write (f[1], &a[0], sizeof (int)) // fixed
close (f[1]); // close pipe write-end
exit(0); // closes child and sends status update to parent ?
}
else { // if parent
close (f[1]) // close write-end of pipe
a[2]+=a[3];
read (f[0], &a, sizeof(int)) // fixed
wait (0); // waits for child to ... close ? or just finish ? or is it the same thing
a[0]+= a[2]; close (f[0]);
printf ("%d\n, "a[0]);
}
}
子供と親は特定の順序で行きますか?親は子が閉じるのを待っていると思いclose (f[1])
ますが、エラーが返されない場合は続行しますか?(ところで、「0」はで何を表しwait(0)
ますか)そしてそれから続くのですか?
私は何を誤解していますか?私は何か正しいことをしていますか?
私はいくつかの研究を使用して行ったことに言及する必要があると思いましたman
が、それは非常に混乱していると思います。私に関する限り、彼らは自分が何をしているのかをすでに知っているが、いくつかの詳細(何を含めるか、何を-pするかなど)を忘れているユーザー、または基本的な理解以上のものを持っているユーザー向けです。