我有2个程序,我希望1的输出是另一个的输入。 像这样:
user@local:~/work/example$ ./feeder | parser
我的第一个程序如下:
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
const char *buff[] = {
"$GPRMC,173843,A,3349.896,N,11808.521,W,000.0,360.0,230108,013.4,E*69\r\n",
"$GPGGA,111609.14,5001.27,N,3613.06,E,3,08,0.0,10.2,M,0.0,M,0.0,0000*70\r\n",
"$GPGSV,2,1,08,01,05,005,80,02,05,050,80,03,05,095,80,04,05,140,80*7f\r\n",
"$GPGSV,2,2,08,05,05,185,80,06,05,230,80,07,05,275,80,08,05,320,80*71\r\n",
"$GPGSA,A,3,01,02,03,04,05,06,07,08,00,00,00,00,0.0,0.0,0.0*3a\r\n",
"$GPRMC,111609.14,A,5001.27,N,3613.06,E,11.2,0.0,261206,0.0,E*50\r\n",
"$GPVTG,217.5,T,208.8,M,000.00,N,000.01,K*4C\r\n"
};
int it;
for(it = 0; it < 6; ++it)
{
fprintf(stdout, buff[it]);
fflush(stdout);
sleep(1);
}
return 0;
}
当我输入
user@local:~/work/example$ ./feeder | grep $
我确实看到输出每秒都会出现。
在我的解析器程序中,我有以下内容:
int main()
{
while (1)
{
check_input()
}
}
void check_input
{
fd_set rfds;
struct timeval tv;
int retval;
int n;
char buff[MAX_BUFF];
int descriptor=0;
/* Watch cb to see when it has input. */
FD_ZERO(&rfds);
FD_SET(descriptor, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(descriptor + 1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1)
{
perror("select()");
}
else if (retval)
{
// printf("Data is available now.\n");
if (FD_ISSET(0, &rfds))
{
n = read(0, buff, MAX_BUFF - 1);
...
}
}
else
printf("No data within five seconds.\n");
}
问题是我的解析器程序有超时。似乎没有看到任何东西。 有什么建议吗?
编辑1: 描述符的值是0,例如STDIN_FILENO的值