I've created a FIFO (with mkfifo()) and I need to wait till some process writes data several times into it. I've written the following code: (ignore the bad style, it's to keep the example concise)
int count = 0, read = 0;
while(count < 2) {
read = read(fd, buf, BUFFER_SIZE);
if(read > 0) {
//do work...
count++;
}
}
In an attempt to avoid busy waiting, I've added the following line:
int count = 0, read = 0;
while(count < 2) {
sleep(2);
read = read(fd, buf, BUFFER_SIZE);
if(read > 0) {
//do work...
count++;
}
}
(fd is the FIFO's descriptor)
However I really don't like it, it looks artificial. Is there a better way doing it?