0

Cを使用してこのLinuxコマンドを実装しようとしています。カット -b 1

私がやろうとしている方法は

親プロセスで ls -l を呼び出す ls -l の出力をファイルに入れる (ファイルへの書き込み)

子プロセスで cut を呼び出す ファイル (親プロセスで書き込まれたもの) を読み取る ファイルに cut を適用する 出力を印刷する

これは私がやったことです

/* pipe.c */
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

void main()
{
   int filedes[2];
   int p;
   pid_t pid, pid1;
   p=pipe(filedes);
   FILE *stream;
   char buff[20];
   
   printf("pipe command returns %d, %d ,%d\n",p, filedes[0],filedes[1]);
   
   if(pipe(filedes) == -1) /* Create the pipe */
      printf("error pipe");
      pid1=fork();
      pid=getpid();
      switch (pid1) { /* Create a child process */
      case -1:
         printf("error fork");
      case 0: /* Child */
      /* Close unused write end */
      /* Child can now read from pipe */
         if (close(filedes[1]) == -1)
            printf("error close");
         printf("I am a child process pid %d, and will read from pipe\n",pid);
         
         while (read(filedes[0], &buff, 1) > 0)
            write(STDOUT_FILENO, &buff, 1);

         write(STDOUT_FILENO, "\n", 1);
         close(filedes[0]);
         _exit(EXIT_SUCCESS);

         break;
         
         default: /* Parent */
         /* Close unused read end */
         /* Parent can now write to pipe */
         if (close(filedes[0]) == -1)
            printf("error close");
         printf("I am the parent process pid %d, and will write to pipe\n", pid );
         stream = fdopen(filedes[1], "w");
         strcpy(buff, "This is a test\n");
         write(filedes[1], buff, strlen(buff));

         char *args[80];
         args[0] = "ls";
         args[1] = "-l";
         args[2] = NULL;
         execvp(args[0],args);
  
         int bak, new;
         bak = dup(1);
         new = open("/home/urwa/abc.txt", O_WRONLY);
         dup2(new, 1);
         close(new);

           

         close(filedes[1]);          /* Reader will see EOF */
         wait(NULL);                /* Wait for child */
         exit(EXIT_SUCCESS);

         break;
   }
}

このコードは問題なく動作します。そしてスタンド出力でテストステートメントを印刷します。ls -l 出力と同様に。しかし、ファイルは空です。私は何を間違っていますか。次のようにfreopenも試しました..まだ空のファイルです。:/

 FILE *fp;
 fp = freopen ("/temp/abc.txt", "a+", stdout);
4

1 に答える 1

0

子でカットを呼び出さなかったし、ファイル記述子もここで台無しになっています。

タスクを実行するには、親の stdout を閉じて、execvp の前に親で書き込み終了 stdout を作成する必要があります。childでは、 execvp の前に、 child の stdinを閉じて、子への stdin として読み取りを終了させる必要があります。そのようにして(パイプb / w 2を作成します)。your parent's stdout is stdin of your child

int main()
{
   int filedes[2];
   int p;
   pid_t pid = 0, pid1 = 0;
   p=pipe(filedes);
   FILE *stream;
   char buff[20];
   char *args[80];

   printf("pipe command returns %d, %d ,%d\n",p, filedes[0],filedes[1]);

   if(pipe(filedes) == -1) /* Create the pipe */
      printf("error pipe");
      pid1=fork();
      pid=getpid();
      switch (pid1) { /* Create a child process */
      case -1:
         printf("error fork"); break;
      case 0: /* Child */
      /* Close unused write end */
      /* Child can now read from pipe */
         if (close(filedes[1]) == -1)
            printf("error close");
         printf("I am a child process pid %d, and will read from pipe\n",pid);

         close(0); //close stdin of child
         dup(filedes[0]); //make pipes read end stdin of child

         args[0] = "cut";
         args[1] = "-b";
         args[2] = "1";
         args[3] = NULL;
         execvp(args[0],args);
         break;

         default: /* Parent */
         /* Close unused read end */
         /* Parent can now write to pipe */
         if (close(filedes[0]) == -1)
            printf("error close");
         printf("I am the parent process pid %d, and will write to pipe\n", pid );

         close(1); //close stdout
         dup(filedes[1]); //make write end of pipe stdout of parent
         args[0] = "ls";
         args[1] = "-l";
         args[2] = NULL;
         execvp(args[0],args);
         break;
   }
}
于 2013-05-07T06:47:24.480 に答える