title: How to synchronize input and output in pipes linux?
body: <p>I am creating a shell command from the custom shell to do the ssh from one terminal to another terminal.
  In order to do the ssh, I am using the inbuilt ssh command of the linux. Here is my code that does the ssh login.
  However, I am seeing that the I/O buffers are not in sync.</p>
  
  <p>This is what I am seeing on the terminal. After SSH to the other terminal. I did the following in the terminal.</p>
  
  <pre><code>PRT# ssh 192.168.10.42
  PRT# Could not create directory '/root/.ssh'.
  root@192.168.10.42's password: 
  # screen -r
  -sh: cen-: not found
  # hello
  -sh: el: not found
  # 
  </code></pre>
  
  <p>I don't what's the reason here. Here is the code.</p>
  
  <pre><code>int sshLogin(chr *destIp)
  {
      char cmd[CMD_LEN];
      char readbuff[CMD_LEN];
      pid_t pid;
      int ret = 0;
      int fd[2];
      int result;
      memset(cmd,'\0',sizeof(cmd));
      int status = 0;
  
      /** --tt required to force pseudowire allocation because we are behind screen app **/
      sprintf(cmd,"/usr/bin/ssh -tt %s",destIp);
  
      /** create a pipe this will be shared on fork() **/
      pipe(fd);
  
      if((pid = fork()) == -1)
      {
          perror("fork:");
          return -1;
      }
      if( pid == 0 )
      {
          /** Child Process of Main APP --Make this parent process for the command**/
  
          if((pid = fork()) == -1)
          {
              perror("fork:");
              return -1;
          }
  
          if( pid == 0)
          {
              /** basically Main APP grand child - this is where we running the command **/
              ret = execlp("ssh", "ssh", "-tt", destIp, NULL);
              printf("done execlp\r\n");
          }
          else
          {
             /** child of Main APP -- keep this blocked until the Main APP grand child is done with the job **/
              while( (read(fd[0], readbuff, sizeof(readbuff))))
              {
                  printf("%s",readbuff);
              }
  
              waitpid(0,&amp;status,0);
              LOG_STRING("SSH CONNC CLOSED");
              exit(0);
          }
      }
      else
      {
          /** Parent process APP MAIN--  **/
          /** no need to wait let APP MAIN run -- **/
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Based on Patrick Ideas.</p>
  
  <p>POST 2# - It seems that it works when we close the stdin in the parent process. However, it becomes very slugguish, I feel like I am typing the keyboard too slow. The system becomes too sluggish. Also, I have a web-server from this terminal. I see that I can no longer access the web. 
  So, the solution is somewhere around stdin but I am not sure.</p>
  
  <pre><code>int sshLogin(chr *destIp)
      {
          char cmd[CMD_LEN];
          char readbuff[CMD_LEN];
          pid_t pid;
          int ret = 0;
          int fd[2];
          int result;
          memset(cmd,'\0',sizeof(cmd));
          int status = 0;
  
          /** --tt required to force pseudowire allocation because we are behind screen app **/
          sprintf(cmd,"/usr/bin/ssh -tt %s",destIp);
  
          /** create a pipe this will be shared on fork() **/
          pipe(fd);
  
          if((pid = fork()) == -1)
          {
              perror("fork:");
              return -1;
          }
          if( pid == 0 )
          {
              /** Child Process of Main APP --Make this parent process for the command**/
  
              if((pid = fork()) == -1)
              {
                  perror("fork:");
                  return -1;
              }
  
              if( pid == 0)
              {
                  /** basically Main APP grand child - this is where we running the command **/
                  ret = execlp("ssh", "ssh", "-tt", destIp, NULL);
                  printf("done execlp\r\n");
              }
              else
              {
                 /** child of Main APP -- keep this blocked until the Main APP grand child is done with the job **/
                  while( (read(fd[0], readbuff, sizeof(readbuff))))
                  {
                      printf("%s",readbuff);
                  }
  
                  waitpid(0,&amp;status,0);
                  LOG_STRING("SSH CONNC CLOSED");
                  exit(0);
              }
          }
          else
          {
              /** Parent process APP MAIN--  **/
              /** no need to wait let APP MAIN run -- **/
              close(stdin);
          }
  
          return 0;
      }
  </code></pre>
  
  <p>Basically, I have added - close(stdin);</p>