0

こんにちは、Linux でシェルを構築しようとしていて、パイプラインの部分で行き詰まっています。最初に、「ls | sort」のようなユーザーからの入力を取得してから、プログラムを実行しようとすると、コマンド ls と ls のように見えます。ソートが機能しない すべてを正しく行ったように見えますが、まだ機能していないようです。助けてください。前もって感謝します

include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <fcntl.h>
#include <sys/stat.h>
#define CREATE_FLAGS (O_WRONLY | O_CREAT | O_APPEND)
#define CREATE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int setup();

int main(int argc, const char * argv[])
{

    while(1)

    {
        printf("333sh: ");
        if(setup())
            break;
    }

    return 0;
}
int setup(){




    char  input [128];
    char *arg[32];
    int i = 1;
    while(fgets(input,128,stdin)!=NULL)
    {
        arg[0] = strtok(input," \n");
        while((arg[i]=strtok(NULL," \n")) != NULL){
            i++;
            }



           if (arg[1]!=NULL && strcmp(arg[1],"|")==0 && arg[2]!=NULL ){
            pid_t pid;

            int fd[3];

        pipe(fd);

    pid=fork();
        if(pid<0){
            printf("fork");
        }
        else if(pid==0){
            pid_t cpid;

            cpid=fork();
                if(cpid==0){

                    dup2(fd[2], 1); // Replace stdin with the read end of the pipe
                    close(fd[0]); // Don't need another copy of the pipe read end hanging about
                    close(fd[2]);
                    execvp(arg[0],arg);
        }
                else if(pid>0){

                    dup2(fd[0], 0); // Replace stdout with the write end of the pipe
                    close(fd[0]); //close read from pipe, in parent
                    close(fd[2]); // Don't need another copy of the pipe write end hanging about
                    execvp(arg[2], arg);
        }
    }
        else if(pid>0){
            waitpid(pid, NULL,0);
  }

        }


            }





    }
4

3 に答える 3

1

fd[0]andを使用していますfd[2]pipe(fd)、セットのみfd[0]andfd[1]です。

于 2013-12-06T23:00:20.060 に答える
1

あなたの最大の問題は、コマンドの引数リストの形式が正しくないことです ( Ben Jackson回答で診断したパイプファイル記述子でインデックス 2 とインデックス 1 の問題を解決した後)。

関数を追加しました:

static void dump_args(int pid, char **argv)
{
    int i = 0;
    fprintf(stderr, "args for %d:\n", pid);
    while (*argv != 0)
        fprintf(stderr, "%d: [%s]\n", i++, *argv++);
}

への呼び出しの直前にそれを呼び出しexecvp()、得られた出力は次のとおりです。

$ ./ns
333sh: ls | sort
args for 29780:
0: [ls]
1: [|]
2: [sort]
ls: sort: No such file or directory
ls: |: No such file or directory
^C
$

control-C は私がプログラムを中断することでした。各コマンドの引数は、'コマンド名' (通常は実行可能ファイルの名前) でなければならず、その後に残りの引数とヌル ポインターが続きます。

トークン化コードが 2 つの正しいコマンドを提供していません。

また、見ている PID にも問題があります。

                cpid = fork();
                if (cpid == 0)
                {
                    dup2(fd[1], 1);
                    close(fd[0]);
                    close(fd[1]);
                    dump_args(getpid(), arg);
                    execvp(arg[0], arg);
                    fprintf(stderr, "Failed to exec %s\n", arg[0]);
                    exit(1);
                }
                else if (pid > 0)  // should be cpid!
                {
                    dup2(fd[0], 0);
                    close(fd[0]);
                    close(fd[1]);
                    dump_args(pid, arg);
                    execvp(arg[1], arg);
                    fprintf(stderr, "Failed to exec %s\n", arg[1]);
                    exit(1);
                }

待機する前に、親プロセスのパイプ ファイル記述子を閉じる必要もあります。

このコードは、またはx | yなどの単純なコマンド シーケンスに対してコンパイルおよび「機能」します。ただし、これは一般的な解決策にはほど遠いものです。一般的な解決策に到達する前に、引数の解析コードをかなり修正する必要があります。ls | sortls | sort -r

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int setup(void);

int main(void)
{
    while (1)
    {
        printf("333sh: ");
        if (setup())
            break;
    }
    return 0;
}

static void dump_args(int pid, char **argv)
{
    int i = 0;
    fprintf(stderr, "args for %d:\n", pid);
    while (*argv != 0)
        fprintf(stderr, "%d: [%s]\n", i++, *argv++);
}

int setup(void)
{
    char input[128];
    char *arg[32];
    int i = 1;
    while (fgets(input, sizeof(input), stdin) != NULL)
    {
        arg[0] = strtok(input, " \n");
        while ((arg[i] = strtok(NULL, " \n")) != NULL)
        {
            i++;
        }
        if (arg[1] != NULL && strcmp(arg[1], "|") == 0 && arg[2] != NULL)
        {
            pid_t pid;
            int fd[2];
            arg[1] = NULL;

            pipe(fd);

            pid = fork();
            if (pid < 0)
            {
                fprintf(stderr, "fork failed\n");
                return 1;
            }
            else if (pid == 0)
            {
                pid_t cpid = fork();
                if (cpid < 0)
                {
                    fprintf(stderr, "fork failed\n");
                    return 1;
                }
                else if (cpid == 0)
                {
                    printf("Writer: [%s]\n", arg[0]);
                    dup2(fd[1], 1);
                    close(fd[0]);
                    close(fd[1]);
                    dump_args(getpid(), arg);
                    execvp(arg[0], arg);
                    fprintf(stderr, "Failed to exec %s\n", arg[0]);
                    exit(1);
                }
                else
                {
                    printf("Reader: [%s]\n", arg[2]);
                    assert(cpid > 0);
                    dup2(fd[0], 0);
                    close(fd[0]);
                    close(fd[1]);
                    dump_args(getpid(), &arg[2]);
                    execvp(arg[2], &arg[2]);
                    fprintf(stderr, "Failed to exec %s\n", arg[2]);
                    exit(1);
                }
            }
            else
            {
                close(fd[0]);
                close(fd[1]);
                assert(pid > 0);
                while (waitpid(pid, NULL, 0) != -1)
                    ;
            }
        }
    }
    return 1;
}
于 2013-12-07T02:01:34.477 に答える