0

I’m new to C and I’m trying to figure out how to send the output of one program to another for processing.… or PIPE it.

Eg. ./foo | ./boo

foo - reads data from an external source every 100ms and writes it to stdout.

boo - reads data from stdin, does some simple calculations and writes it to stdout,

(I don't have access to foo, but it writes a float value every 100ms on a new line.)

I have it working, however after every couple of hundred entries… there is a 2 second pause...

so it spits out about 200 lines, 2 second pause, 200 lines, 2 second pause. etc..

Is there a buffer I need to be aware off?

Here is the code for boo;

#include <stdio.h>
float input;
float value;

while (1)
    {
    input = scanf("%f", &value);

    {do stuff here}

    printf("result %f \n ", input);
    fflush(stdout);

    }
    return  0;
} 

EDIT I found the issue, foo had a loop for the 100ms delay. when piping this into boo it was somehow getting grouped together. Removing this loop fixed the issue

4

1 回答 1

0

编辑我发现了问题, foo 有一个 100 毫秒延迟的循环。当将它输送到嘘声中时,它以某种方式组合在一起。删除此循环解决了问题

我删除了这个循环并解决了这个问题;

int millis()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (tv.tv_sec) * 1000 + (tv.tv_usec)/1000;
}
start = millis()
while(millis() - start < 20)
 {
  usleep(1000);
 }
于 2013-02-05T01:32:48.790 回答