pipe() doesn't seem to work. (Pyramid OSx 4.4)
A.V. Raman
ARaman at massey.ac.nz
Thu Mar 22 13:18:06 AEST 1990
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 <stdio.h>
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.
&
More information about the Comp.unix.questions
mailing list