Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
mkfifo("test",0777);
int i = open("test",O_RDONLY);
char* msg = malloc(PIPE_BUF*sizeof(char));
while(1) {
read(i,msg,PIPE_BUF);
printf("%s\n",msg);
}
return 0;
}
I build this small program on Ubuntu Linux and successfully compiled it with gcc and executed it. However, if I execute the following command:
Code:
echo "This is a sample test" > test
the program keeps printing "This is a sample test" indefinitely. But what I want is to change the program so that it blocks each time it reads, until a new message arrives.
How do I do it? Thank you in advance.