Advertisement
Combreal

forkedChildLinux01.cpp

Jun 10th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5.  
  6. #define PIPE_READ 0
  7. #define PIPE_WRITE 1
  8.  
  9. int createChild(const char* szCommand, char* const aArguments[], char* const aEnvironment[], const char* szMessage)
  10. {
  11.     int aStdinPipe[2];
  12.     int aStdoutPipe[2];
  13.     int nChild;
  14.     char nChar;
  15.     int nResult;
  16.  
  17.     if(pipe(aStdinPipe) < 0)
  18.     {
  19.         perror("allocating pipe for child input redirect");
  20.         return -1;
  21.     }
  22.     if(pipe(aStdoutPipe) < 0)
  23.     {
  24.         close(aStdinPipe[PIPE_READ]);
  25.         close(aStdinPipe[PIPE_WRITE]);
  26.         perror("allocating pipe for child output redirect");
  27.         return -1;
  28.     }
  29.  
  30.     nChild = fork();
  31.     if (0 == nChild)
  32.     {
  33.         // child continues here
  34.  
  35.         // redirect stdin
  36.         if(dup2(aStdinPipe[PIPE_READ], STDIN_FILENO) == -1)
  37.         {
  38.             exit(errno);
  39.         }
  40.  
  41.         // redirect stdout
  42.         if(dup2(aStdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1)
  43.         {
  44.             exit(errno);
  45.         }
  46.  
  47.         // redirect stderr
  48.         if(dup2(aStdoutPipe[PIPE_WRITE], STDERR_FILENO) == -1)
  49.         {
  50.             exit(errno);
  51.         }
  52.  
  53.         // all these are for use by parent only
  54.         close(aStdinPipe[PIPE_READ]);
  55.         close(aStdinPipe[PIPE_WRITE]);
  56.         close(aStdoutPipe[PIPE_READ]);
  57.         close(aStdoutPipe[PIPE_WRITE]);
  58.  
  59.         // run child process image
  60.         // replace this with any exec* function find easier to use ("man exec")
  61.         nResult = execve(szCommand, aArguments, aEnvironment);
  62.  
  63.         // if we get here at all, an error occurred, but we are in the child
  64.         // process, so just exit
  65.         exit(nResult);
  66.     }
  67.     else if(nChild > 0)
  68.     {
  69.         // parent continues here
  70.  
  71.         // close unused file descriptors, these are for child only
  72.         close(aStdinPipe[PIPE_READ]);
  73.         close(aStdoutPipe[PIPE_WRITE]);
  74.  
  75.         // Include error check here
  76.         if(NULL != szMessage)
  77.         {
  78.             write(aStdinPipe[PIPE_WRITE], szMessage, strlen(szMessage));
  79.         }
  80.  
  81.         // Just a char by char read here, you can change it accordingly
  82.         while(read(aStdoutPipe[PIPE_READ], &nChar, 1) == 1)
  83.         {
  84.             write(STDOUT_FILENO, &nChar, 1);
  85.         }
  86.  
  87.         // done with these in this example program, you would normally keep these
  88.         // open of course as long as you want to talk to the child
  89.         close(aStdinPipe[PIPE_WRITE]);
  90.         close(aStdoutPipe[PIPE_READ]);
  91.     } else
  92.     {
  93.         // failed to create child
  94.         close(aStdinPipe[PIPE_READ]);
  95.         close(aStdinPipe[PIPE_WRITE]);
  96.         close(aStdoutPipe[PIPE_READ]);
  97.         close(aStdoutPipe[PIPE_WRITE]);
  98.     }
  99.     return nChild;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement