Using pipe in linux using parent and child process

匆匆过客 提交于 2019-12-02 17:11:06

问题


i am trying to implement this linux command using C. ls -l | cut -b 1

the way i am trying to do it is

calling ls -l in parent process putting output of ls -l in a file(writing to a file)

calling cut in child process reading the file (the one written in the parent process) applying cut to the file printing the output

this is by far what i have done

/* 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;
   }
}

this piece of code works perfectly fine. and prints at stand output the test statement. as well as the ls -l output. but the file is empty. what am i doing wrong. I also tried freopen as follow.. still empty file. :/

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

回答1:


You didn't called the cut in the child and also the file descriptors are messed up here.

For performing the task you have to close the stdout of parent and make the write end stdout in parent before execvp. In child you have to close stdin of child and make read end as stdin to your child before execvp. In that way your parent's stdout is stdin of your child(creating the pipe b/w two).

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;
   }
}


来源:https://stackoverflow.com/questions/16406777/using-pipe-in-linux-using-parent-and-child-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!