This relates to a previous post regarding the behavior of gr_file_sink
when it writes to a named pipe and the other side of the pipe stops reading.

Currently the bahavior of the block is that it does not catch the
"errno=32=EPIPE"
(or any errno!=0 for that matter)
and thus the work function returns a false number of consumed items
(for some reason even with a broken pipe it always returns 512 items
consumed...)

As a result the remaing graph keeps producing samples that are somehow falsely
consumed into the file sink.

I added a couple of lines to check for the errno!=0

Please check and comment.
I attach 2 python files to check the bahavior of the file sink.
Firtst do " mkfifo fifo_rx" and then run tx.py and rx.py in different shells.
You can stop start rx.py any number of times and tx.py pauses and
restarts as well.

Observe though that tx.py consumes cpu even when the pipe is broken
because it continuosly calls
the gr_file_sink work function...

Achilleas

==========

diff --git a/gnuradio-core/src/lib/io/gr_file_sink.cc
b/gnuradio-core/src/lib/io/gr_file_sink.cc
index aab0158..52d4c84 100644
--- a/gnuradio-core/src/lib/io/gr_file_sink.cc
+++ b/gnuradio-core/src/lib/io/gr_file_sink.cc
@@ -27,6 +27,8 @@
 #include <gr_file_sink.h>
 #include <gr_io_signature.h>
 #include <stdexcept>
+#include <cstdio>
+#include <cerrno>


 gr_file_sink_sptr
@@ -64,9 +66,16 @@ gr_file_sink::work (int noutput_items,
     return noutput_items;              // drop output on the floor

   while (nwritten < noutput_items){
+    errno=0;
     int count = fwrite (inbuf, d_itemsize, noutput_items - nwritten, d_fp);
-    if (count == 0)    // FIXME add error handling
+    if (count == EOF){ // FIXME add error handling
+      //printf("fwrite() returned EOF. Requested %d and wrote
%d\n",noutput_items - nwritten,count);
       break;
+    }
+    if (errno!=0){     // FIXME add error handling
+      //printf("fwrite() raised error no = %d. Requested %d and wrote
%d\n",errno,noutput_items - nwritten,count);
+      break;
+    }
     nwritten += count;
     inbuf += count * d_itemsize;
   }
diff --git a/gnuradio-core/src/lib/io/gr_file_sink.cc 
b/gnuradio-core/src/lib/io/gr_file_sink.cc
index aab0158..52d4c84 100644
--- a/gnuradio-core/src/lib/io/gr_file_sink.cc
+++ b/gnuradio-core/src/lib/io/gr_file_sink.cc
@@ -27,6 +27,8 @@
 #include <gr_file_sink.h>
 #include <gr_io_signature.h>
 #include <stdexcept>
+#include <cstdio>
+#include <cerrno>
 
 
 gr_file_sink_sptr
@@ -64,9 +66,16 @@ gr_file_sink::work (int noutput_items,
     return noutput_items;              // drop output on the floor
 
   while (nwritten < noutput_items){
+    errno=0;
     int count = fwrite (inbuf, d_itemsize, noutput_items - nwritten, d_fp);
-    if (count == 0)    // FIXME add error handling
+    if (count == EOF){ // FIXME add error handling
+      //printf("fwrite() returned EOF. Requested %d and wrote 
%d\n",noutput_items - nwritten,count);
       break;
+    }
+    if (errno!=0){     // FIXME add error handling
+      //printf("fwrite() raised error no = %d. Requested %d and wrote 
%d\n",errno,noutput_items - nwritten,count);
+      break;
+    }
     nwritten += count;
     inbuf += count * d_itemsize;
   }
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Tx
# Generated: Thu Oct 13 23:55:47 2011
##################################################

from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import window
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from gnuradio.wxgui import fftsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx

class tx(grc_wxgui.top_block_gui):

	def __init__(self):
		grc_wxgui.top_block_gui.__init__(self, title="Tx")

		##################################################
		# Variables
		##################################################
		self.samp_rate = samp_rate = 100e3

		##################################################
		# Blocks
		##################################################
		self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
			self.GetWin(),
			baseband_freq=0,
			y_per_div=10,
			y_divs=5,
			ref_level=20,
			ref_scale=2.0,
			sample_rate=samp_rate,
			fft_size=1024,
			fft_rate=30,
			average=True,
			avg_alpha=0.1,
			title="FFT Plot",
			peak_hold=False,
		)
		self.Add(self.wxgui_fftsink2_0.win)
		self.gr_sig_source_x_0 = gr.sig_source_c(samp_rate, gr.GR_COS_WAVE, 5000, 1, 0)
		self.gr_noise_source_x_0 = gr.noise_source_c(gr.GR_GAUSSIAN, 0.1, 42)
		self.gr_file_sink_0 = gr.file_sink(gr.sizeof_gr_complex*1, "fifo_rx")
		self.gr_file_sink_0.set_unbuffered(True)
		self.gr_add_xx_0 = gr.add_vcc(1)

		##################################################
		# Connections
		##################################################
		self.connect((self.gr_noise_source_x_0, 0), (self.gr_add_xx_0, 1))
		self.connect((self.gr_sig_source_x_0, 0), (self.gr_add_xx_0, 0))
		self.connect((self.gr_add_xx_0, 0), (self.wxgui_fftsink2_0, 0))
		self.connect((self.gr_add_xx_0, 0), (self.gr_file_sink_0, 0))

	def get_samp_rate(self):
		return self.samp_rate

	def set_samp_rate(self, samp_rate):
		self.samp_rate = samp_rate
		self.gr_sig_source_x_0.set_sampling_freq(self.samp_rate)
		self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate)

if __name__ == '__main__':
	parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
	(options, args) = parser.parse_args()
	tb = tx()
	tb.Run(True)

#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Rx
# Generated: Thu Oct 13 23:55:42 2011
##################################################

from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import window
from gnuradio.eng_option import eng_option
from gnuradio.gr import firdes
from gnuradio.wxgui import fftsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx

class rx(grc_wxgui.top_block_gui):

	def __init__(self):
		grc_wxgui.top_block_gui.__init__(self, title="Rx")

		##################################################
		# Variables
		##################################################
		self.samp_rate = samp_rate = 100e3

		##################################################
		# Blocks
		##################################################
		self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
			self.GetWin(),
			baseband_freq=0,
			y_per_div=10,
			y_divs=5,
			ref_level=20,
			ref_scale=2.0,
			sample_rate=samp_rate,
			fft_size=1024,
			fft_rate=30,
			average=True,
			avg_alpha=0.1,
			title="FFT Plot",
			peak_hold=False,
		)
		self.Add(self.wxgui_fftsink2_0.win)
		self.gr_throttle_0 = gr.throttle(gr.sizeof_gr_complex*1, samp_rate)
		self.gr_file_source_0 = gr.file_source(gr.sizeof_gr_complex*1, "fifo_rx", False)

		##################################################
		# Connections
		##################################################
		self.connect((self.gr_file_source_0, 0), (self.gr_throttle_0, 0))
		self.connect((self.gr_throttle_0, 0), (self.wxgui_fftsink2_0, 0))

	def get_samp_rate(self):
		return self.samp_rate

	def set_samp_rate(self, samp_rate):
		self.samp_rate = samp_rate
		self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate)

if __name__ == '__main__':
	parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
	(options, args) = parser.parse_args()
	tb = rx()
	tb.Run(True)

_______________________________________________
Patch-gnuradio mailing list
Patch-gnuradio@gnu.org
https://lists.gnu.org/mailman/listinfo/patch-gnuradio

Reply via email to