From bug-request at octave dot org Wed Dec 29 11:52:22 2004 Subject: Waiting for gnuplot to write file. From: Peter JENSEN To: bug at octave dot org Date: Wed, 29 Dec 2004 11:17:37 -0600 Hi, On my system at work (with two processors) I see a problem with legend. The problem is intermittent however I managed to track it down, and I think I understand the underlying reason. The chain of events are as follows. Firstly legend.m asks gnuplot to save the original plotting command into a temp file. This is done via : tmpfilename=tmpnam; command=["save \"",tmpfilename,"\"\n"]; graw(command); Then legend waits for file to appear and immediately after starts a shell command that processes the temp file. The code looks like. while (isempty(stat(tmpfilename))) if (++attempt > 20) error("gnuplot is not responding"); endif usleep(1e5); end plot_cmd = split(system(shell_cmd),"\n"); The problem is that the file is empty at the time when the system call is made. The problem is due to that the code only checks if the file exists. However at the time where the system call is made the file has actually not been written by gnuplot. I verified this by modifying legend.m as follows : file_test = stat(tmpfilename) ; while (isempty(file_test)) if (++attempt > 20) error("gnuplot is not responding"); endif usleep(1e5); file_test = stat(tmpfilename) ; end file_test When the code fails the size of the file is zero, and the code prints. file_test = { atime = 1104339140 blksize = 32768 blocks = 0 ctime = 1104339140 dev = 21 gid = 2100 ino = 12074898 modestr = -rw-rw-r-- mtime = 1104339140 nlink = 1 rdev = 0 size = 0 uid = 23737 } And when the code (occasionally works ) the file size is different from zero, and the code prints. file_test = { atime = 1104339203 blksize = 32768 blocks = 16 ctime = 1104339203 dev = 21 gid = 2100 ino = 12074898 modestr = -rw-rw-r-- mtime = 1104339203 nlink = 1 rdev = 0 size = 4316 uid = 23737 } On way to solve the problem is to look at the file written by gnuplot, and see if it is complete (it is not sufficient to look at the size of the file.....). Gnuplot indicated that the file has been completed by writing "# EOF" to the last line. We can therefore test this via a "tail -1 file_name". We could also find out if the file is open for writing, as I assume that gnuplot will close the file when it has been fully written, however I don't know how to do this. I have enclosed the code that I used for testing. Peter Jensen ======================================= #!/opt/sbin/octave -q more("off") ; clear closeplot level = [ 0 -1 -2 -3 -4 -5 -6 ] ; plot_legend = {} ; for i=1:length(level) signal{i} = sin(0:0.01:10)*10**(level(i)/10) ; plot_legend{i} = strcat("L = ",num2str(level(i))) ; endfor clearplot ; ## Plot signals axis([ 1 length(signal{1}) ]) title("Test") ; plot(signal{1}) ; hold on ; for k=2:length(level) plot(signal{k}); endfor legend(plot_legend,-1) ; ========================================== -- Peter Kabell Jensen E-mail : peterjensen at agere dot com Agere Systems Tel : +44 (0) 1344 86 5849 Microelectronics House Fax : +44 (0) 1344 86 5990 Kings Ride, Ascot, SL5 8AD England ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------