私はCで複数のパイプを実装しようとしています
ls - al | less | wc
パイプラインの作成に問題があります。プロセスを作成し、それらをパイプで接続することになっているループがあります。
for(i=0;i<num_cmds;i++){
create_commands(cmds[i]);
}
私のcreate_commands()
関数は次のようになります
void create_commands (char cmd[MAX_CMD_LENGTH]) // Command be processed
{
int pipeid[2];
pipe(pipeid);
if (childpid = fork())
{
/* this is the parent process */
dup2(pipeid[1], 1); // dup2() the write end of the pipe to standard output.
close(pipeid[1]); // close() the write end of the pipe
//parse the command
parse_command(cmd, argvector);
// execute the command
execvp(argvector[0], argvector);
close(1); // close standard output
}
else
{
/* child process */
dup2( pipeid[0], 0); // the read end of the pipe to standard input
close( pipeid[0] ); // close() the read end of the pipe
}
}
しかし、これは機能しません。stdinとstdoutが台無しになっています。誰かが私が間違っていることを私に指摘してもらえますか?
前もって感謝します!