You say your code "like" this:

$pid=fork(); exec("notepad"); $SIG{INT}=\&handler; sub handler { kill 1,$pid; }
...I would expect an if (or something similar) to do the exec in the child and the rest in the parent... surely ?

So I tried:

my $pid = fork() ; die "fork failed '$!'" if !defined($pid) ; if ($pid == 0) { exec("notepad") ; } ; print "Started Notepad \$pid = $pid\n" ; my $r = 1 ; sub handler { kill 'INT', $pid ; $r = 0 ; } ; $SIG{INT}=\&handler; while ($r) { sleep(1) } ; print "And we're done\n" ;
and I discovered (on ActivePerl 5.10.0) that: (a) the $pid returned was -ve, and related to a thread within the perl process; and (b) that the $pid was not the pid of the notepad process. I imagine I should have known that.

I could not find a way of discovering the pid of the process created by the exec -- and system doesn't do the trick either....

But I did find Win32::Process, and the following appears to work:

use strict ; use warnings ; use Win32::Process ; use Win32 ; my $SystemRoot = $ENV{SystemRoot} ; print "\$SystemRoot = '$SystemRoot'\n" ; sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } my $ProcessObj ; Win32::Process::Create($ProcessObj, "$SystemRoot\\notepad.exe", "notepad", 0, NORMAL_PRIORITY_CLASS, "." ) or die ErrorReport() ; my $pid = $ProcessObj->GetProcessID() ; print "Started Notepad \$pid = $pid\n" ; my $run = 1 ; sub handler { $ProcessObj->Kill(1234) ; $run = 0 ; } ; $SIG{INT}=\&handler; while ($run) { sleep(1) } ; print "And we're done\n" ;
... so I've learned something today :-) But I'm not sure if this is what you wanted (in particular I've assumed that the reference to "notepad" means you're running Windows....


In reply to Re: closing notepad using perl by gone2015
in thread closing notepad using perl by ashok13123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.