Using C or C write a program which starts the command ls la
Using C or C++, write a program which starts the command ”ls -la” using a fork() followed by an exec() (any of the exec functions will work). Use a UNIX pipe system call to send the output of ls -la back to the parent, read it using the read() function, and then write it to the console using the write() function
Solution
As per my understanding I am providing the solution, if any thing is remaining then please tell me.
Source Code:
#include<unistd.h>
 #include <sys/wait.h>
 #include <assert.h>
 #include <stdio.h>
 #include<stdio.h>
 #include<stdlib.h>
 int main( void )
 {     
         int pfildes[2];
         pid_t pid;
         char buf;
         if (pipe(pfildes) == -1)    {perror(\"demo\"); exit(1);}
             if ((pid = fork()) == -1)   {perror(\"demo\"); exit(1);}
             else if (pid == 0) {     
             printf(\"Child process\");
           close(pfildes[0]);    /* close read end of pipe               */
           execlp(\"ls\",\"ls\",\"-la\",NULL);
           close(pfildes[1]);    /* close excess fildes                  */
           perror(\"demo1\");       /* still around? exec failed           */
           _exit(1);             /* no flush                             */
         }
     else {                      /* parent: \"/usr/bin/wc\"               */
             printf(\"Parrent process\");
           close(pfildes[1]);    /* close write end of pipe              */
         while (read(pfildes[0], &buf, 1) > 0)
             write(STDOUT_FILENO, &buf, 1);
           close(pfildes[0]);    /* close excess fildes                  */
           perror(\"demo2\");       /* still around? exec failed           */
           exit(1);            /* parent flushes                       */
         }
 }

