I have two programs, one of which is called a generator, which prints text per second
int main(int argc, char** argv) { for(int i = 0; i < 10 ; i++) { printf("something\n"); sleep(1); } return (EXIT_SUCCESS); }
then a has a second program that calls it a consumer, which should read from standard input, add some other text to it and retype it. Let it look like this:
int main(int argc, char** argv) { char line[BUFSIZ]; while(scanf("%s",line) != -1){ printf("you entered %s \n",line); } return (EXIT_SUCCESS); }
when I compile them and try to run only a type generator. / generator, it works as I expected. every second something prints to the console. but when I try to run them like. / generator | ./consumer it did not work as I expected. I wait 10 seconds, and after that I get 10 lines that you entered something. I want to print "you entered something" every second.
Can you explain to me why this is so, or is it even better to tell me how to achieve the expected behavior?
source share