Unix and Linux SQL Sessions

Under Unix and Linux, the SQL filter program can run as a background process and other programs can attach to it through the use of pipes. A code fragment that shows how this can be achieved is:

int pipefd2[2];
int pipefd1[2];

pipe(pipefd1);            /* make 2 pipes */
pipe(pipefd2);

if (fork() != 0)          /* parent process */ 
{
  close(pipefd1[0]);      /* close reading side of 1st pipe */
  close(pipefd2[1]);      /* close writing side of 2nd pipe */
  close(0);               /* close std in and std out */
  close(1);

  dup2(pipefd2[0],0);        /* reading end of 2nd pipe - stdin */
  dup2(pipefd1[1],1);        /* writing end of 1st pipe - stdout */

  close(pipefd2[0]);      /* close duplicates .. */
  close(pipefd1[1]);      /* close duplicates .. */

  /* At this point, stdin in from pipe 2, and stdout is pipe 1 */

  /* Do whatever is required , i/o will be to/from SQL */

}
else                      /* child process */
{
  close(pipefd2[0]);      /* close reading side of 2nd pipe */
  close(pipefd1[1]);      /* close writing side of 1st pipe */
  close(0);               /* close std in and std out */
  close(1); 
  dup2(pipefd1[0],0);     /* reading end of 1st pipe - stdin */
  dup2(pipefd2[1],1);     /* writing end of 2nd pipe - stdout */
  close(pipefd1[0]);      /* close duplicates .. */
  close(pipefd2[1]);      /* close duplicates .. */

  /* at this point, stdin from pipe 1, and stdout is pipe 2 */

  /* SQL reads and writes are now via these pipes */

  execl("sqlexe", "sqlexe", "-noheadings", "-e", "37", 0);

}