の使用に関する正式な教育を受けていないという事実を前置きしたいpipes
ので、これが私の最初の冒険です。私の状況に似た質問が見つからなかったことは言うまでもありません。
注: これは学校の課題のためのより大きなプロジェクトの一部であるため、誰かに私のためにこれを行うよう依頼しているわけではありません。いくつかの指示/役立つコード セグメントが必要です。(「詐欺師」の発言を避けるために、これをできるだけ一般的なものにしようとしました。)
親プロセスがandを使用して子を生成し、 a を使用して出力を親に送り返す要素を実行しようとしていfor-loop
ます。使用しようとしている一般的なコードと、発生したエラー/問題を次に示します。int k
k
fork()
execl()
pipe()
注: helloworld
= でコンパイルされた実行可能GCC
ファイルprintf("hello world\n");
int k = 10; //The number of children to make
int fd[2]; //Used for each pipe
int readFromMe[k]; //Holds the file IDs of each pipe fd[0]
int writeToMe[k]; //Holds the file IDs of each pipe fd[1]
int processID[k]; //Holds the list of child process IDs
//Create children
int i;
for(i = 0; i < k; i++)
{
if(pipe(fd) == -1)
{
printf("Error - Pipe error.\n");
exit(EXIT_FAILURE);
}
//Store the pipe ID
readFromMe[i] = fd[0];
writeToMe[i] = fd[1];
if((processID[i] = fork()) == -1)
{
fprintf(stderr, "fork failure");
exit(EXIT_FAILURE);
}
//If it is a child, change the STDOUT to the pipe-write file descriptor, and exec
if(processID[i] == 0)
{
dup2 (writeToMe[i], STDOUT_FILENO);
close(readFromMe[i]);
execl("./helloworld", (char *)0);
}
//If it is the parent, just close the unnecessary pipe-write descriptor and continue itterating
else
{
close(writeToMe[i]);
}
}
//Buffer for output
char output[100000];
//Read from each pipe and print out the result
for(i = 0; i < k; i++)
{
int r = read(readFromMe[i], &output, (sizeof(char) * 100000));
if(r > 0)
{
printf("result = %s\n", output);
}
close(readFromMe[i]);
}
プログラムからまったく出力が得られないため、この問題が発生している理由を突き止めようとしています。