help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

strange behavior reading lines from a pipe


From: Mario Storti
Subject: strange behavior reading lines from a pipe
Date: Sun, 7 Jul 2013 18:00:06 -0300

Hi all,

I need to synchronize Octave with a C++ program. The program sends to
Octave (which is running a script) a double, Octave computes some
information, saves to disk, and sends back an OK to the C++ program. The
problem I have is that it seems that Octave is one line lagged with
respect to what I send.

These are the complete instructions for reproducing the `bug' (if it
is a bug...)

1) Create a FIFO file with "$ mkfifo mypipe"

2) Run this script in Octave

------------
fid = fopen("mypipe","r");
while 1
  line = fgetl(fid);
  if !ischar(line); break; endif
  printf("Received line %s\n",line);
endwhile
fclose(fid);
------------

3) In a shell simulate sending lines to the Octave script

$ cat > mypipe
1
2
3
4
5
6
^D
$

4) What happens is that when I write the first line "1" to the pipe, the script
prints nothing. Then I write "2" an the script prints "Received line
1", and so on, i.e. when I write line "n" the script prints "Received line
n-1".

5) When I close the pipe by pressing ^D, the last line entered (in the
case above 6) is printed, and the code exits the loop.

Some additional info

* I'm using Octave 3.6.4 on Fedora Linux 19. Tried also on Octave
  3.0.3 (Fedora 9) with the same result.

* Equivalent code in Perl and C++ work fine. (Find below the Perl
  code). In C/C++ I use the `getline' function.

* Perhaps reading files this is not a problem, because the lines are
  not lost. All the lines are passed to the Octave script, but the
  problem is that it seems that Octave `buffers' one line.

* As a workaround, my C++ program is sending a double EOL:

  fprintf(fid,"%f\n\n",time);

  at the end of each request and the Octave script is discarding empty
  lines. In this way the communication works fine. I'm just curious
  about this behaviour being a bug, and it seems that it should have
  been detected before.  Perhaps there is another way to communicate
  to the C++ program? I would be willing to give a try!!

Regards, Mario

--------- Perl script -----------
#! /usr/bin/perl -w
use strict;

open PIPE,"<mypipe";

while (my $line = <PIPE>) {
    print "Read $line";
}
---------------------------------

-------------------------
Mario Alberto Storti
CIMEC (CONICET-UNL)
Predio CONICET-Santa Fe
Colect. Ruta Nac. 168 Km 472, Paraje El Pozo
3000 Santa Fe, Argentina
Tel: +54-342-4511594 (ext 1015), Tel/Fax: +54-342-4511169
Home: +54-342-4550193, e-mail: mario.storti at gmail.com
http://www.cimec.org.ar/mstorti
-------------------------


reply via email to

[Prev in Thread] Current Thread [Next in Thread]