Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby IO.popen with "-" , what happens under the hood?
    primarykey
    data
    text
    <p>I'm trying to understand IO.popen when its command is <code>"-"</code> which starts a new Ruby interpreter.</p> <p>There is not much material about this subject, and I'm getting slowly through them, mainly because of me as I only code for fun.</p> <p>As far as I have understood when <code>IO.popen("-", "w+") {|f| ...}</code> is invoked - that's with a block - that block will be run by <em>both</em> the parent and the child process. The difference is that the parent process will get an IO object as a result, but the child gets only a Nil. That's easy, I need to check <code>|f|</code> in the block and when it is Nil, execution is in the child process, when it is not nil, execution is in the parent. So I have to write both code for parent and child separated by <code>if</code>. </p> <p>This time it helps me to understand the problem, that <em>the block is part of the IO.popen command</em>.</p> <p>I have this code:</p> <pre><code>pipe = IO.popen("-","w+") # puts "This line will break functionality if uncommented" if pipe != nil then pipe.puts "PID: #{Process.pid}" $stderr.puts "Parent from child: #{pipe.gets.chomp}" else $stderr.puts "Child PID: #{Process.pid} and Parent #{gets.chomp}" puts "M'kay" end </code></pre> <h2>Questions:</h2> <ul> <li>What decides which process runs first? If they were to append a file would it be vulnerable to race condition?</li> <li>Why the 2nd line breaks the code? The <code>pipe = IO.popen...</code> command shouldn't be related to the <code>if..else..end</code> block, yet they are. For me <code>pipe</code> is a file handle (like in old Turbo Pascal) which is first definded somewhere, then manipulated elsewhere.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload