multiple forks, pipes?
What this code does is, parent process forks a child and then child forks a child. Parent writes a string to the pipe, child reads it and then writes it to the next pipe, the one connecting child and child's child. Child's child reads it and prints it in stdout
my problem is I tried to change what the last forked child does. Instead of reading from pipe and print (which works fine), I tried to run a sort which I suppose reads from stdin, which has been "dupped" so should read from the pipe and then print to stdout. Anyway, this is the last block,the commented out one. So commenting out read, printf and uncomment the sort is not working, meaning it doesnt print a thing just hangs there, like sort gets no input. If someone could explain...Thanks.
#define BSIZ 20
printf("read from pipe:\n%s",buf2);
Thats because you are just sending the sort command, thats it. But sort command need some paramter to work up on. Or in more general what do u need to sort. In the your case u are just ending up with an NULL. which is not right. Look at some sort command examples here Sort command Examples.
What do you mean sort needs a parameter? works without a parameter for sort, no parameter means read from stdin, no? This is the case here, just instead of an ls output, there is another string, which is send in the pipe and sort should read it...