Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4270944
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:22:31+00:00 2026-05-21T07:22:31+00:00

I’m trying to understand IO.popen when its command is – which starts a new

  • 0

I’m trying to understand IO.popen when its command is "-" which starts a new Ruby interpreter.

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.

As far as I have understood when IO.popen("-", "w+") {|f| ...} is invoked – that’s with a block – that block will be run by both 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 |f| 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 if.

This time it helps me to understand the problem, that the block is part of the IO.popen command.

I have this 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

Questions:

  • What decides which process runs first? If they were to append a file would it be vulnerable to race condition?
  • Why the 2nd line breaks the code? The pipe = IO.popen... command shouldn’t be related to the if..else..end block, yet they are. For me pipe is a file handle (like in old Turbo Pascal) which is first definded somewhere, then manipulated elsewhere.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:22:31+00:00Added an answer on May 21, 2026 at 7:22 am

    No one decides which process runs first. The child process may run first—or the parent process may run first—OS may schedule them either way.

    This means that parent process may finish before the child process finishes. When parent process finishes, the pipe to it is closed, and when the child process writes to it, it gets an exception. That’s what happens in your code.

    Why doesn’t it happen without the commented line? When you invoke gets in the parent process, it waits until the child writes a line to the pipe. It means that the parent won’t finish until the child writes a line to the pipe, and this neglects the issue. However, when you print two lines, the odds that the parent process terminates before the child executes the second puts "M'kay" increase.

    Try the following code:

    pipe = IO.popen("-","w+")
    puts "This line will not break functionality"
    puts "This line will not break functionality"
    puts "This line will not break functionality"
      if pipe != nil then
        pipe.puts "PID: #{Process.pid}"
        while line = pipe.gets
          $stderr.puts "Parent from child: #{line.chomp}"
        end
      else
        $stderr.puts "Child PID: #{Process.pid} and Parent #{gets.chomp}"
        puts "M'kay"
      end
    

    It waits until the child closes the pipe (then the pipe.gets will return nil), which happens then it terminates, and it ensures that it won’t try to write there anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.