Write a programl that creates two child processes one to exe

Write a programl that creates two child processes: one to execute \'ls\' and the other to execute \'sort\'. After the forks, the original parent process waits for both child processes to finish before it terminates. The standard output of \'ls\' process should be piped to the input to the \'sort\' process. Make sure you close the unnecessary open files for the three processes. In C.


I have:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <fcntl.h>
// I certify this is my own work Joshua Posada
int main()
{
   pid_t pid1,pid2;
   pid1 = fork();
   if(pid1 < 0)
   {
       /* an error occurred */
       perror(\"fork failed\");
       exit(-1);
   }
   else if(pid1 == 0)
   {
       /* child process1 */
      system(\"ls > input.txt\");
       return 0;
   }

   else
   {

       /* parent process */      
       wait(&pid1);
       pid2 = fork();

       if(pid2 < 0)
      {
           perror(\"fork failed\");
           exit(-1);
       }

       else if(pid2 == 0)
       {
           /* child process2 */
           system(\"sort -d input.txt\");  
           return 0;
       }
  
      else
       {
           /*Parent Process*/
           wait(&pid2);
          
           return 0;
       }
   }

   return 0;
}

but i would like it to work with no system calls. Any ideas?

Solution

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <fcntl.h>
// I certify this is my own work Joshua Posada
int main()
{
   pid_t pid1,pid2;
   pid1 = fork();
   if(pid1 < 0)
   {
       /* an error occurred */
       perror(\"fork failed\");
       exit(-1);
   }
   else if(pid1 == 0)
   {
       /* child process1 */
      system(\"ls > input.txt\");
       return 0;
   }

   else
   {

       /* parent process */      
       wait(&pid1);
       pid2 = fork();

       if(pid2 < 0)
      {
           perror(\"fork failed\");
           exit(-1);
       }

       else if(pid2 == 0)
       {
           /* child process2 */
           system(\"sort -d input.txt\");  
           return 0;
       }
  
      else
       {
           /*Parent Process*/
           wait(&pid2);
          
           return 0;
       }
   }

   return 0;
}

Write a programl that creates two child processes: one to execute \'ls\' and the other to execute \'sort\'. After the forks, the original parent process waits f
Write a programl that creates two child processes: one to execute \'ls\' and the other to execute \'sort\'. After the forks, the original parent process waits f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site