Cでシェルを実装しようとしています。単純なexecvp()で単純なコマンドをうまく実行できますが、要件の1つは次のようなコマンドを管理することです: "ls -l | head | tail -4" for 'for 'ループと1つの' pipe() 'ステートメントでstdinとstdoutをリダイレクトします。数日後、私は少し迷っています。
N =単純なコマンドの数(例では3つ:ls、head、tail)commands =次のようなコマンドを持つ構造体のリスト:
commands[0].argv[0]: ls
commands[0].argv[1]: -l
commands[1].argv[0]: head
commands[2].argv[0]: tail
commands[2].argv[1]: -4
そこで、forループを作成し、すべてのコマンドをパイプに接続するためにstdinとstdoutをリダイレクトし始めましたが、...どうしてうまくいかないのかわかりません。
for (i=0; i < n; i++){
pipe(pipe);
if(fork()==0){ // CHILD
close(pipe[0]);
close(1);
dup(pipe[1]);
close(pipe[1]);
execvp(commands[i].argv[0], &commands[i].argv[0]);
perror("ERROR: ");
exit(-1);
}else{ // FATHER
close(pipe[1]);
close(0);
dup(pipe[0]);
close(pipe[0]);
}
}
私が作成したいのは、子プロセスの「行」です:
[ls -l] ---- pipe ----> [head] ---- pipe ----> [tail -4]
このすべてのプロセスにはルート(シェルを実行するプロセス)があるため、最初の父親はシェルプロセスの子でもあります。もう少し疲れています。誰か助けてください。
子がコマンドを実行するものであるかどうかもわかりません。
みんなありがとう !!
ここでは複雑なことはありません。最後のコマンドは元のプロセスのファイル記述子1に出力し、最初のコマンドは元のプロセスファイル記述子0から読み取る必要があることに留意してください。 previois pipe
呼び出し。
そのため、次のタイプがあります。
#include <unistd.h>
struct command
{
const char **argv;
};
単純に明確に定義されたセマンティクスでヘルパー関数を作成します。
int
spawn_proc (int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
そして、これがメインのフォークルーチンです。
int
fork_pipes (int n, struct command *cmd)
{
int i;
pid_t pid;
int in, fd [2];
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
for (i = 0; i < n - 1; ++i)
{
pipe (fd);
/* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */
spawn_proc (in, fd [1], cmd + i);
/* No need for the write end of the pipe, the child will write here. */
close (fd [1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = fd [0];
}
/* Last stage of the pipeline - set stdin be the read end of the previous pipe
and output to the original file descriptor 1. */
if (in != 0)
dup2 (in, 0);
/* Execute the last stage with the current process. */
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
そして小さなテスト:
int
main ()
{
const char *ls[] = { "ls", "-l", 0 };
const char *awk[] = { "awk", "{print $1}", 0 };
const char *sort[] = { "sort", 0 };
const char *uniq[] = { "uniq", 0 };
struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} };
return fork_pipes (4, cmd);
}
動作するように見えます。 :)
まず、あなたは時期尚早にパイプを閉じています。現在のプロセスで必要のない端だけを閉じ、子のstdin/stdoutを忘れずに閉じてください。
次に、前のコマンドのfdを覚えておく必要があります。したがって、2つのプロセスの場合、これは次のようになります。
int pipe[2];
pipe(pipe);
if ( fork() == 0 ) {
/* Redirect output of process into pipe */
close(stdout);
close(pipe[0]);
dup2( pipe[1], stdout );
execvp(commands[0].argv[0], &commands[0].argv[0]);
}
if ( fork() == 0 ) {
/* Redirect input of process out of pipe */
close(stdin);
close(pipe[1]);
dup2( pipe[0], stdin );
execvp(commands[1].argv[0], &commands[1].argv[0]);
}
/* Main process */
close( pipe[0] );
close( pipe[1] );
waitpid();
あなたの仕事はこれにエラー処理を追加し、n個のプロセスが開始するためのn-1個のパイプを生成することです。最初のfork()ブロックのコードは、プロセス1..n-1の適切なパイプに対して実行する必要があり、2番目のfork()ブロックのコードはプロセス2..nに対して実行する必要があります。