octave-maintainers
[Top][All Lists]
Advanced

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

popen2 strange behaviour within a function


From: John W. Eaton
Subject: popen2 strange behaviour within a function
Date: Wed, 12 May 1999 13:37:29 -0500 (CDT)

On 14-Apr-1999, Daniel Calvelo <address@hidden> wrote:

| To: address@hidden
| Cc: dcalvelo
| Subject: popen2 strange behaviour within a function
| 
| Bug report for Octave 2.0.13.95 configured for i386-pc-linux-gnu
| 
| Description:
| -----------
| 
|       When used insude a function, popen2 is unable to fecth output from 
| child process if it is requested immediately after input feeding (at least on 
| buffered input processes, like 'sort').
| 
|       In the interactive interpreter, it behaves as expected.

As far as I know, this is the result of a race condition.  If you
write:

  function e=ess(m)
    [in,out,pid] = popen2("sort","-n");
    fprintf(in,"%e\n",m)
    fclose(in) 
    sleep(seconds_to_sleep)
    e=fscanf(out,"%e")
    fclose(out)
    waitpid(pid)
  endfunction

for a sufficiently large value of `seconds_to_sleep', I think you will
see the results you expect.  The reason it works when you type the
commands at the command line is that there is a sufficient delay to
allow the subprocess to run.

Note that it may work in some cases to call waitpid instead of
sleeping, but only if the input to and output from the subprocess are
sufficiently small amounts of data.  For example, try the following

  function e=ess(m)
    [in,out,pid] = popen2("sort","-n");
    fprintf(in,"%e\n",m)
    fclose(in) 
    waitpid(pid)
    e=fscanf(out,"%e")
    fclose(out)
  endfunction

for larger and larger vectors.  Given enough data (not really all that
much, either), this solution will hang on the waitpid call.

If anyone knows the correct solution for avoiding this race condition,
it would be great if you could clue the rest of us in on the secret.

Thanks,

jwe


| Repeat-By:
| ---------
| 
| Session transcript:
| 
-------------------------------------------------------------------------------
| Octave, version 2.0.13.95 (i386-pc-linux-gnu).
| Copyright (C) 1996, 1997, 1998 John W. Eaton.
| This is free software with ABSOLUTELY NO WARRANTY.
| For details, type `warranty'.
| 
| octave:1> type ess
| ess is the function defined from: /home/dcalvelo/lang/objc/ess.m
| 
| function e=ess(m)
|    [in,out,pid] = popen2("sort","-n");
|    fprintf(in,"%e\n",m)
|    fclose(in)
|    e=fscanf(out,"%e")
|    fclose(out)
|    waitpid(pid)
| endfunction
| octave:2> m=rand(10,1);
| octave:3> ess(m)
| ans = 19
| ans = 0
| e = [](0x1)
| ans = 0
| ans = 2920
| ans = [](0x1)
| octave:4>    [in,out,pid] = popen2("sort","-n");
| octave:5>    fprintf(in,"%e\n",m)
| ans = 19
| octave:6>    fclose(in)
| ans = 0
| octave:7>    e=fscanf(out,"%e")
| e =
| 
|   0.21444
|   0.21812
|   0.24042
|   0.30588
|   0.39884
|   0.49816
|   0.57548
|   0.66786
|   0.84487
|   0.94233
| 
| octave:8>    fclose(out)
| ans = 0
| octave:9>    waitpid(pid)
| ans = -1
| octave:10> 



reply via email to

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