How to do a pipe & fork?

Simon Yeh syeh at caip.rutgers.edu
Fri Nov 4 09:48:08 AEST 1988


In article <Nov.2.14.51.36.1988.8260 at zztop.rutgers.edu>, pratt at zztop.rutgers.edu (Lorien Y. Pratt) writes:
>OK, an even easier question than I posted last time, I'd really appreciate
>your help.
>Here's what I have so far, which I know is wrong.

You may try this....[Note: lines starting with /*@*/ are what I added]

 #include <netdb.h>
 #include <stdio.h>
 #define STRLEN 40
 
 main()
 {
 int pid;
 char from_sql[256];
 char *fgets();
 int i;
 /*@*/ int fd[2]; /* store file descriptors for pipe */
 
 /*@*/ pipe(fd);  /* creat pipe */
 
 pid = fork();
 
 if (pid == 0)   /* We are the children */
 {
   /*@*/ dup2(fd[1], 1); /* redirect child's stdout to fd[1] 
                            so child can talk to parent */
   /*@*/ close(df[0]); close(fd[1]);
   /* This part works.  I tested it in its own program without being a child. */
   i = execlp("rsh", "rsh", "topaz", "/u2/ingres/bin/sql", "spam", 0);
   printf("execlp didn't work, return code is %d\n", i);
   /*@*/ exit(1);
 }
 else
 {
   printf( "Child's process ID is %d\n", pid ); fflush(stdout);
   /*@*/ close(fd[1]);
 
   /* Start talking to child */
   /*@*/ fgets(from_sql, 256, fd[0] ); /* read what child says from fd[0] */
   printf("SQL says: %s\n", from_sql );
 
   /*@*/ fclose( fd[0] );
   ......
 }
 .....
}

>Lorien Y. Pratt                            Computer Science Department
>pratt at paul.rutgers.edu                     Rutgers University

Hope this will help.

--- Simon Yeh



More information about the Comp.unix.wizards mailing list