win32/process question

Discussion in 'Ruby' started by J-Van, Nov 10, 2006.

  1. J-Van

    J-Van Guest

    On Windows XP, Ruby 1.8.5:

    # start notepad
    irb(main):003:0> info = Process.create :app_name => "notepad"
    => #<struct Struct::processInfo process_handle=1916,
    thread_handle=1912, process_id=3536, thread_id=3088>

    # notepad running
    irb(main):004:0> Process.kill 0, info.process_id
    => [3536]

    # kill notepad
    irb(main):005:0> Process.kill 9, info.process_id
    => [3536]

    # notepad's dead, but why doesn't this throw an exception?!
    irb(main):006:0> Process.kill 0, info.process_id
    => [3536]

    Any ideas?
    Thanks,
    Joe
    J-Van, Nov 10, 2006
    #1
    1. Advertising

  2. J-Van

    J-Van Guest

    I guess I should note that this is after I require 'win32/process'
    (which is installed via a gem).

    On 11/9/06, J-Van <> wrote:
    > On Windows XP, Ruby 1.8.5:
    >
    > # start notepad
    > irb(main):003:0> info = Process.create :app_name => "notepad"
    > => #<struct Struct::processInfo process_handle=1916,
    > thread_handle=1912, process_id=3536, thread_id=3088>
    >
    > # notepad running
    > irb(main):004:0> Process.kill 0, info.process_id
    > => [3536]
    >
    > # kill notepad
    > irb(main):005:0> Process.kill 9, info.process_id
    > => [3536]
    >
    > # notepad's dead, but why doesn't this throw an exception?!
    > irb(main):006:0> Process.kill 0, info.process_id
    > => [3536]
    >
    > Any ideas?
    > Thanks,
    > Joe
    >
    J-Van, Nov 10, 2006
    #2
    1. Advertising

  3. J-Van

    Park Heesob Guest

    Hi,

    >From: J-Van <>
    >Reply-To:
    >To: (ruby-talk ML)
    >Subject: Re: win32/process question
    >Date: Fri, 10 Nov 2006 15:23:58 +0900
    >
    >I guess I should note that this is after I require 'win32/process'
    >(which is installed via a gem).
    >
    >On 11/9/06, J-Van <> wrote:
    >>On Windows XP, Ruby 1.8.5:
    >>
    >># start notepad
    >>irb(main):003:0> info = Process.create :app_name => "notepad"
    >>=> #<struct Struct::processInfo process_handle=1916,
    >>thread_handle=1912, process_id=3536, thread_id=3088>
    >>
    >># notepad running
    >>irb(main):004:0> Process.kill 0, info.process_id
    >>=> [3536]
    >>
    >># kill notepad
    >>irb(main):005:0> Process.kill 9, info.process_id
    >>=> [3536]
    >>
    >># notepad's dead, but why doesn't this throw an exception?!
    >>irb(main):006:0> Process.kill 0, info.process_id
    >>=> [3536]
    >>
    >>Any ideas?
    >>Thanks,
    >>Joe
    >>

    >

    Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
    "While open handles to kernel objets are closed automatically when a process
    terminates, the objects themselves exist until all open handles to them are
    closed. Therefore, an object will remain valid after a process that is using
    it terminates if another process has an open handle to it."

    Here is the working code.

    irb(main):001:0> require 'win32/process'
    => true
    irb(main):002:0> include Windows::Handle
    => Object
    irb(main):003:0> info = Process.create :app_name=>'notepad'
    => #<struct Struct::processInfo process_handle=1916, thread_handle=1912,
    process
    _id=1748, thread_id=1108>
    irb(main):004:0> Process.kill 0,info.process_id
    => [1748]
    irb(main):005:0> Process.kill 9,info.process_id
    => [1748]
    irb(main):006:0> CloseHandle(info.process_handle)
    => true
    irb(main):007:0> CloseHandle(info.thread_handle)
    => true
    irb(main):008:0> Process.kill 0,info.process_id
    ProcessError: The parameter is incorrect.
    from
    c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    s.rb:156:in `kill'
    from
    c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    s.rb:132:in `each'
    from
    c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    s.rb:132:in `kill'
    from (irb):8


    Regards,

    Park Heesob

    _________________________________________________________________
    Don't just search. Find. Check out the new MSN Search!
    http://search.msn.click-url.com/go/onm00200636ave/direct/01/
    Park Heesob, Nov 10, 2006
    #3
  4. J-Van

    J-Van Guest

    On 11/10/06, Park Heesob <> wrote:
    >
    > Hi,
    >
    > >From: J-Van <>
    > >Reply-To:
    > >To: (ruby-talk ML)
    > >Subject: Re: win32/process question
    > >Date: Fri, 10 Nov 2006 15:23:58 +0900
    > >
    > >I guess I should note that this is after I require 'win32/process'
    > >(which is installed via a gem).
    > >
    > >On 11/9/06, J-Van <> wrote:
    > >>On Windows XP, Ruby 1.8.5:
    > >>
    > >># start notepad
    > >>irb(main):003:0> info = Process.create :app_name => "notepad"
    > >>=> #<struct Struct::processInfo process_handle=1916,
    > >>thread_handle=1912, process_id=3536, thread_id=3088>
    > >>
    > >># notepad running
    > >>irb(main):004:0> Process.kill 0, info.process_id
    > >>=> [3536]
    > >>
    > >># kill notepad
    > >>irb(main):005:0> Process.kill 9, info.process_id
    > >>=> [3536]
    > >>
    > >># notepad's dead, but why doesn't this throw an exception?!
    > >>irb(main):006:0> Process.kill 0, info.process_id
    > >>=> [3536]
    > >>
    > >>Any ideas?
    > >>Thanks,
    > >>Joe
    > >>

    > >

    > Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
    > "While open handles to kernel objets are closed automatically when a process
    > terminates, the objects themselves exist until all open handles to them are
    > closed. Therefore, an object will remain valid after a process that is using
    > it terminates if another process has an open handle to it."
    >
    > Here is the working code.
    >
    > irb(main):001:0> require 'win32/process'
    > => true
    > irb(main):002:0> include Windows::Handle
    > => Object
    > irb(main):003:0> info = Process.create :app_name=>'notepad'
    > => #<struct Struct::processInfo process_handle=1916, thread_handle=1912,
    > process
    > _id=1748, thread_id=1108>
    > irb(main):004:0> Process.kill 0,info.process_id
    > => [1748]
    > irb(main):005:0> Process.kill 9,info.process_id
    > => [1748]
    > irb(main):006:0> CloseHandle(info.process_handle)
    > => true
    > irb(main):007:0> CloseHandle(info.thread_handle)
    > => true
    > irb(main):008:0> Process.kill 0,info.process_id
    > ProcessError: The parameter is incorrect.
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:156:in `kill'
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:132:in `each'
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:132:in `kill'
    > from (irb):8


    Fantastic, thanks!

    Joe
    J-Van, Nov 10, 2006
    #4
  5. J-Van

    J-Van Guest

    On 11/10/06, Park Heesob <> wrote:
    >
    > Hi,
    >
    > >From: J-Van <>
    > >Reply-To:
    > >To: (ruby-talk ML)
    > >Subject: Re: win32/process question
    > >Date: Fri, 10 Nov 2006 15:23:58 +0900
    > >
    > >I guess I should note that this is after I require 'win32/process'
    > >(which is installed via a gem).
    > >
    > >On 11/9/06, J-Van <> wrote:
    > >>On Windows XP, Ruby 1.8.5:
    > >>
    > >># start notepad
    > >>irb(main):003:0> info = Process.create :app_name => "notepad"
    > >>=> #<struct Struct::processInfo process_handle=1916,
    > >>thread_handle=1912, process_id=3536, thread_id=3088>
    > >>
    > >># notepad running
    > >>irb(main):004:0> Process.kill 0, info.process_id
    > >>=> [3536]
    > >>
    > >># kill notepad
    > >>irb(main):005:0> Process.kill 9, info.process_id
    > >>=> [3536]
    > >>
    > >># notepad's dead, but why doesn't this throw an exception?!
    > >>irb(main):006:0> Process.kill 0, info.process_id
    > >>=> [3536]
    > >>
    > >>Any ideas?
    > >>Thanks,
    > >>Joe
    > >>

    > >

    > Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
    > "While open handles to kernel objets are closed automatically when a process
    > terminates, the objects themselves exist until all open handles to them are
    > closed. Therefore, an object will remain valid after a process that is using
    > it terminates if another process has an open handle to it."
    >
    > Here is the working code.
    >
    > irb(main):001:0> require 'win32/process'
    > => true
    > irb(main):002:0> include Windows::Handle
    > => Object
    > irb(main):003:0> info = Process.create :app_name=>'notepad'
    > => #<struct Struct::processInfo process_handle=1916, thread_handle=1912,
    > process
    > _id=1748, thread_id=1108>
    > irb(main):004:0> Process.kill 0,info.process_id
    > => [1748]
    > irb(main):005:0> Process.kill 9,info.process_id
    > => [1748]
    > irb(main):006:0> CloseHandle(info.process_handle)
    > => true
    > irb(main):007:0> CloseHandle(info.thread_handle)
    > => true
    > irb(main):008:0> Process.kill 0,info.process_id
    > ProcessError: The parameter is incorrect.
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:156:in `kill'
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:132:in `each'
    > from
    > c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > s.rb:132:in `kill'
    > from (irb):8


    So, if I use Process.create to create a new process, and I want to
    know when a user closed the program, I should close the handle to the
    process immediately after starting it? And then Process.kill(0,
    <pid>) would return an exception once the user closed the program?

    Joe
    J-Van, Nov 10, 2006
    #5
  6. J-Van

    Park Heesob Guest

    Hi,

    >From: J-Van <>
    >Reply-To:
    >To: (ruby-talk ML)
    >Subject: Re: win32/process question
    >Date: Sat, 11 Nov 2006 03:24:34 +0900
    >
    >On 11/10/06, Park Heesob <> wrote:
    >>
    >>Hi,
    >>

    ....
    >> >

    >>Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
    >>"While open handles to kernel objets are closed automatically when a
    >>process
    >>terminates, the objects themselves exist until all open handles to them
    >>are
    >>closed. Therefore, an object will remain valid after a process that is
    >>using
    >>it terminates if another process has an open handle to it."
    >>
    >>Here is the working code.
    >>
    >>irb(main):001:0> require 'win32/process'
    >>=> true
    >>irb(main):002:0> include Windows::Handle
    >>=> Object
    >>irb(main):003:0> info = Process.create :app_name=>'notepad'
    >>=> #<struct Struct::processInfo process_handle=1916, thread_handle=1912,
    >>process
    >>_id=1748, thread_id=1108>
    >>irb(main):004:0> Process.kill 0,info.process_id
    >>=> [1748]
    >>irb(main):005:0> Process.kill 9,info.process_id
    >>=> [1748]
    >>irb(main):006:0> CloseHandle(info.process_handle)
    >>=> true
    >>irb(main):007:0> CloseHandle(info.thread_handle)
    >>=> true
    >>irb(main):008:0> Process.kill 0,info.process_id
    >>ProcessError: The parameter is incorrect.
    >> from
    >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    >>s.rb:156:in `kill'
    >> from
    >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    >>s.rb:132:in `each'
    >> from
    >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    >>s.rb:132:in `kill'
    >> from (irb):8

    >
    >So, if I use Process.create to create a new process, and I want to
    >know when a user closed the program, I should close the handle to the
    >process immediately after starting it? And then Process.kill(0,
    ><pid>) would return an exception once the user closed the program?
    >
    >Joe
    >

    You cannot close the hanle if the process is still alive. You can use
    WaitForSingleObject or Process.waitpid to wait until child process exits
    like this:

    require 'win32/process'
    include Windows::Handle
    info = Process.create :app_name=>'notepad'
    Process.waitpid(info.process_id)
    CloseHandle(info.process_handle)
    CloseHandle(info.thread_handle)



    Regards,

    Park Heesob

    _________________________________________________________________
    Express yourself instantly with MSN Messenger! Download today it's FREE!
    http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
    Park Heesob, Nov 11, 2006
    #6
  7. J-Van

    J-Van Guest

    On 11/10/06, Park Heesob <> wrote:
    >
    > Hi,
    >
    > >From: J-Van <>
    > >Reply-To:
    > >To: (ruby-talk ML)
    > >Subject: Re: win32/process question
    > >Date: Sat, 11 Nov 2006 03:24:34 +0900
    > >
    > >On 11/10/06, Park Heesob <> wrote:
    > >>
    > >>Hi,
    > >>

    > ....
    > >> >
    > >>Refer to http://msdn2.microsoft.com/fr-fr/library/ms686722.aspx,
    > >>"While open handles to kernel objets are closed automatically when a
    > >>process
    > >>terminates, the objects themselves exist until all open handles to them
    > >>are
    > >>closed. Therefore, an object will remain valid after a process that is
    > >>using
    > >>it terminates if another process has an open handle to it."
    > >>
    > >>Here is the working code.
    > >>
    > >>irb(main):001:0> require 'win32/process'
    > >>=> true
    > >>irb(main):002:0> include Windows::Handle
    > >>=> Object
    > >>irb(main):003:0> info = Process.create :app_name=>'notepad'
    > >>=> #<struct Struct::processInfo process_handle=1916, thread_handle=1912,
    > >>process
    > >>_id=1748, thread_id=1108>
    > >>irb(main):004:0> Process.kill 0,info.process_id
    > >>=> [1748]
    > >>irb(main):005:0> Process.kill 9,info.process_id
    > >>=> [1748]
    > >>irb(main):006:0> CloseHandle(info.process_handle)
    > >>=> true
    > >>irb(main):007:0> CloseHandle(info.thread_handle)
    > >>=> true
    > >>irb(main):008:0> Process.kill 0,info.process_id
    > >>ProcessError: The parameter is incorrect.
    > >> from
    > >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > >>s.rb:156:in `kill'
    > >> from
    > >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > >>s.rb:132:in `each'
    > >> from
    > >>c:/ruby/lib/ruby/gems/1.8/gems/win32-process-0.5.1/lib/win32/proces
    > >>s.rb:132:in `kill'
    > >> from (irb):8

    > >
    > >So, if I use Process.create to create a new process, and I want to
    > >know when a user closed the program, I should close the handle to the
    > >process immediately after starting it? And then Process.kill(0,
    > ><pid>) would return an exception once the user closed the program?
    > >
    > >Joe
    > >

    > You cannot close the hanle if the process is still alive. You can use
    > WaitForSingleObject or Process.waitpid to wait until child process exits
    > like this:
    >
    > require 'win32/process'
    > include Windows::Handle
    > info = Process.create :app_name=>'notepad'
    > Process.waitpid(info.process_id)
    > CloseHandle(info.process_handle)
    > CloseHandle(info.thread_handle)



    If I use Process.waitpid in a thread, all the other threads seem to
    block until the process that I'm waiting for stops.

    I'm writing a program that runs on a windows machine that listens for
    requests to start and kill programs. It needs to be able to run and
    kill multiple programs and needs to be able to monitor the programs
    that it has started to make sure that they are still running. If
    Process.waitpid makes the whole Ruby process hang, that's no good.

    Joe
    J-Van, Nov 11, 2006
    #7
  8. J-Van

    Park Heesob Guest


    >From: J-Van <>
    >Reply-To:
    >To: (ruby-talk ML)
    >Subject: Re: win32/process question
    >Date: Sat, 11 Nov 2006 09:29:10 +0900
    >

    ....
    >
    >If I use Process.waitpid in a thread, all the other threads seem to
    >block until the process that I'm waiting for stops.
    >
    >I'm writing a program that runs on a windows machine that listens for
    >requests to start and kill programs. It needs to be able to run and
    >kill multiple programs and needs to be able to monitor the programs
    >that it has started to make sure that they are still running. If
    >Process.waitpid makes the whole Ruby process hang, that's no good.
    >
    >Joe
    >

    If you don't want blocking, try WaitForSingleObect like this:

    require 'win32/process'
    include Windows::Handle
    include Windows::Synchronize

    info = Process.create :app_name=>'notepad'

    begin
    i = WaitForSingleObject(info.process_handle,10)
    if i==WAIT_OBJECT_0
    CloseHandle(info.process_handle)
    CloseHandle(info.thread_handle)
    end

    # do something

    end while i==WAIT_TIMEOUT


    Regards,

    Park Heesob

    _________________________________________________________________
    FREE pop-up blocking with the new MSN Toolbar - get it now!
    http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
    Park Heesob, Nov 11, 2006
    #8
  9. J-Van

    J-Van Guest

    On 11/10/06, Park Heesob <> wrote:
    > >From: J-Van <>
    > >Reply-To:
    > >To: (ruby-talk ML)
    > >Subject: Re: win32/process question
    > >Date: Sat, 11 Nov 2006 09:29:10 +0900
    > >

    > ....
    > >
    > >If I use Process.waitpid in a thread, all the other threads seem to
    > >block until the process that I'm waiting for stops.
    > >
    > >I'm writing a program that runs on a windows machine that listens for
    > >requests to start and kill programs. It needs to be able to run and
    > >kill multiple programs and needs to be able to monitor the programs
    > >that it has started to make sure that they are still running. If
    > >Process.waitpid makes the whole Ruby process hang, that's no good.
    > >
    > >Joe
    > >

    > If you don't want blocking, try WaitForSingleObect like this:
    >
    > require 'win32/process'
    > include Windows::Handle
    > include Windows::Synchronize
    >
    > info = Process.create :app_name=>'notepad'
    >
    > begin
    > i = WaitForSingleObject(info.process_handle,10)
    > if i==WAIT_OBJECT_0
    > CloseHandle(info.process_handle)
    > CloseHandle(info.thread_handle)
    > end
    >
    > # do something
    >
    > end while i==WAIT_TIMEOUT


    Thanks -- I'll try that. I'm not exactly sure what those functions do
    though. :)

    Joe
    J-Van, Nov 11, 2006
    #9
  10. Park Heesob wrote:

    <snip>

    > >So, if I use Process.create to create a new process, and I want to
    > >know when a user closed the program, I should close the handle to the
    > >process immediately after starting it? And then Process.kill(0,
    > ><pid>) would return an exception once the user closed the program?
    > >
    > >Joe
    > >

    > You cannot close the hanle if the process is still alive. You can use
    > WaitForSingleObject or Process.waitpid to wait until child process exits
    > like this:
    >
    > require 'win32/process'
    > include Windows::Handle
    > info = Process.create :app_name=>'notepad'
    > Process.waitpid(info.process_id)
    > CloseHandle(info.process_handle)
    > CloseHandle(info.thread_handle)


    Hm, maybe we should provide a block form for Process.create that does
    this behind the scenes. What do you think?

    Regards,

    Dan
    Daniel Berger, Nov 21, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rtm
    Replies:
    0
    Views:
    788
  2. Java and Swing
    Replies:
    1
    Views:
    661
    Chris Lambacher
    Oct 24, 2005
  3. Tim Golden
    Replies:
    0
    Views:
    428
    Tim Golden
    Oct 21, 2005
  4. Qi Lu
    Replies:
    0
    Views:
    393
    Qi Lu
    Jul 14, 2008
  5. Mike Stead
    Replies:
    3
    Views:
    262
    Roger Pack
    Mar 25, 2010
Loading...

Share This Page