Wednesday 29 June 2016

Simple Linux Pipe Example | Pipe Command Linux (dup2)

Simple Linux Pipe Example | Pipe Command Linux
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <unistd.h>

int main(void)
{
   int pfds[2];
   pipe(pfds);
  
   pid_t pid;
   pid=fork();

   if (pid==0) {
        close(1);          /* close normal stdout */
        dup2(pfds[1],1);   /* make stdout same as pfds[1] */
        close(pfds[0]);    /* we don't need this */
        execlp("ls", "ls", NULL);
   }
   else {
        close(0);          /* close normal stdin */
        dup2(pfds[0],0);   /* make stdin same as pfds[0] */
        close(pfds[1]);    /* we don't need this */
        execlp("more", "more", NULL, NULL);
    }

    return 0;
}


No comments

Post a Comment

Recent Posts