[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ProgSoc] I can't get smoke out of my pipe
Can anybody explain why the following simple program does not work:
#include <stdio.h>
#include <unistd.h>
void main(void)
{
int p[2];
FILE* a;
FILE* b;
char s[100];
pipe(p);
a=fdopen(p[0],"r");
b=fdopen(p[1],"w");
fprintf(b,"foo foo foo \n");
fgets(s,99,a);
printf("%s\n",s);
}
I think this should work, but with some experimentation, discovered that
pipes usually need their write-ends closed before the read end receives
anything (I think). Thus if close(p[1]); is placed after fprintf, then it
should not hang. However, having done this, s is empty. Further
experimenting led me to replace fprintf(..) with :
write(p[1],"ZXCZXCZXCZXC", 10);
This time, lo and behold, it worked. Can someone explain why the original
program does not work?
Cheers,
\/|<
Victor Rajewski
vik@nospam.progsoc.uts.edu.au
http://www.progsoc.uts.edu.au
`Man will occasionally stumble over the truth, but most of the time he
will pick himself up and continue on'
- Winston Churchill, British statesman and writer (1874-1965)
--
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.
This list is archived at <http://www.progsoc.uts.edu.au/lists/progsoc/>