help!

help!

Post by DANNY BO » Sat, 25 Jan 1997 04:00:00



New to unix programming and having problems with pipes,
why does the following/attached code do nothing but hang around?

        Dave

[ MAINPRG2.C < 1K ]
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define  STDIN_FILENO   0

int
main (void)
{
        int     n,pipe1[2],pipe2[2],m;
        pid_t   pid;
        char    line[20];
        FILE    *p1,*p0;

        if(pipe(pipe1) < 0){
                fprintf(stderr,"pipe error\n");
                exit(1);
                }

        if(pipe(pipe2) < 0){
                fprintf(stderr,"pipe error\n");
                exit(1);
                }

        if((pid = fork()) < 0){
                fprintf(stderr,"fork error\n");
                exit(1);
                }

        if(pid == 0) {
                close(pipe1[0]);                
                close(pipe2[1]);
                if((p0 = fdopen(pipe2[0],"r")) == NULL){
                        fprintf(stderr,"failed fdopen");
                        exit(1);}
                p1 = fdopen(pipe1[1],"w");
                fputc(47,p1);
                n = fgetc(p0);
                fprintf(stderr,"%d\n",n);
                fprintf(stderr,"Hello Child\n");

        }
        else {
                close(pipe1[1]);
                close(pipe2[0]);
                p0 = fdopen(pipe1[0],"r");
                if((p1 = fdopen(pipe2[1],"w")) == NULL){
                        fprintf(stderr,"failed fdopen parent");
                        exit(1);}
                m = fgetc(p0);
                fprintf(stderr,"%d ",m);
                fputc(89,p1);
                fprintf(stderr,"Hello parent\n");
        }

        exit(0);

Quote:}

 
 
 

help!

Post by Aamer Sachedi » Sat, 25 Jan 1997 04:00:00


I've only browsed your code for a second but here are a few
thoughts.

1. When you submit a pipe() call to the API, you are returned
the read and write descriptors for the pipe.  You don't need
to call open() (or fdopen) to get any more descriptors for this
pipe.  This is not a named pipe that you are creating, it is a
kernel construct that is avaliable only to the creator of the
pipe and its descendants.

2. When using kernel constructs like pipes and named pipes,
I find that it is usually better to submit the UNIX API calls
to use them.  Once you have called pipe(), your program won't
compile on several other platforms anyway, so why not use read()
and write().  This is a personal opinion.

Good Luck,
-Aamer

: This is a multi-part message in MIME format.
:
: --------------20322F5E1045
: Content-Type: text/plain; charset=us-ascii
: Content-Transfer-Encoding: 7bit
:
: New to unix programming and having problems with pipes,
: why does the following/attached code do nothing but hang around?
:
:
:       Dave
:
: --------------20322F5E1045
: Content-Type: text/plain; charset=us-ascii; name="MAINPRG2.C"
: Content-Transfer-Encoding: 7bit
: Content-Disposition: inline; filename="MAINPRG2.C"
:
: #include <stdio.h>
: #include <stdlib.h>
: #include <signal.h>
: #define  STDIN_FILENO 0
:
: int
: main (void)
: {
:       int     n,pipe1[2],pipe2[2],m;
:       pid_t   pid;
:       char    line[20];
:       FILE    *p1,*p0;
:      
:       if(pipe(pipe1) < 0){
:               fprintf(stderr,"pipe error\n");
:               exit(1);
:               }
:
:       if(pipe(pipe2) < 0){
:               fprintf(stderr,"pipe error\n");
:               exit(1);
:               }
:                              
:       if((pid = fork()) < 0){
:               fprintf(stderr,"fork error\n");
:               exit(1);
:               }
:              
:       if(pid == 0) {
:               close(pipe1[0]);                
:               close(pipe2[1]);
:               if((p0 = fdopen(pipe2[0],"r")) == NULL){
:                       fprintf(stderr,"failed fdopen");
:                       exit(1);}
:               p1 = fdopen(pipe1[1],"w");
:               fputc(47,p1);
:               n = fgetc(p0);
:               fprintf(stderr,"%d\n",n);
:               fprintf(stderr,"Hello Child\n");
:
:       }
:       else {
:               close(pipe1[1]);
:               close(pipe2[0]);
:               p0 = fdopen(pipe1[0],"r");
:               if((p1 = fdopen(pipe2[1],"w")) == NULL){
:                       fprintf(stderr,"failed fdopen parent");
:                       exit(1);}
:               m = fgetc(p0);
:               fprintf(stderr,"%d ",m);
:               fputc(89,p1);
:               fprintf(stderr,"Hello parent\n");
:       }
:      
:       exit(0);
: }
:
: --------------20322F5E1045--
: