Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!uokmax!munnari.oz.au!kaukau.comp.vuw.ac.nz!comp.vuw.ac.nz!massey!ARaman From: ARaman@massey.ac.nz (A.V. Raman) Newsgroups: comp.unix.questions Subject: pipe() doesn't seem to work. (Pyramid OSx 4.4) Message-ID: <611@massey.ac.nz> Date: 22 Mar 90 03:18:06 GMT Reply-To: ARaman@massey.ac.nz (A.V. Raman) Organization: Massey University, Palmerston North, New Zealand Lines: 61 When I tried to set up a pipe in one of my programs, I found that it failed. When I put in some debug printfs in the code, I found that it worked whenever there was a printf done just before the dup() in the parent. I also found that write() does not work in place of printf(). I'm posting a reduced version of the code that caused the problem. I've also removed all error checks at dups() and closes(). However, I must mention that no errors were encountered even in the code that included the checking. Any help (by email please) would be greatly appreciated. ---- Code begins ---- #include setup() { int fd[2]; if (pipe(fd) < 0) { /* write to fd[1] and read fd[0] */ perror("pipe"); exit(1); } if (fork() == 0) { /* set up child to read pipe as stdin */ close(0); dup(fd[0]); close(fd[1]); execl("/bin/cat","cat","-n",NULL); /* ucb cat -n to number lines */ perror("exec"); } else { /* set up parent to write to pipe as stdout */ /**/ printf (" "); /* program fails if this line is removed */ close(1); dup(fd[1]); close(fd[0]); } } main(argc,argv) char **argv; { FILE *fp; char buf [BUFSIZ]; if (argc == 1) exit(1); setup(); /* setup pipe with cat reading other end */ if ((fp = fopen(argv[1],"r")) == NULL) { perror(argv[1]); exit(1); } while (fgets(buf,BUFSIZ,fp) != NULL) printf("%s", buf); /* write into pipe */ fclose(fp); close(1); return 0; } ---- Code ends ---- Thanks. &