RE: fork() and wait().

2004-05-20 Thread Carter Thompson

Mike, et al;

Thanks for all your help and special thanks to Bill for helping
me offline.  I've got exactly what I want here in this small 
example using CVS.

Cheers,

Carter.

=-=-=-=-=-= Code =-=-=-=-=-=

use strict;
use POSIX ":sys_wait_h";

$| = 1; # Important to turn off autoflush.

my $pid = fork ();
if ($pid) { # parent
while (1) {
my $ret = waitpid ($pid,&WNOHANG);
if ($ret <= 0) {
print ".";
  sleep 5;
  next;
}
last;
}
} else {
system("cvs -Q co module");
exit;
}

=-=-=-=-= End Code =-=-=-=-=

> -Original Message-
> From: Mike Jackson [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, May 20, 2004 11:23 AM
> To: Carter Thompson; Perl-Win32-Users
> Subject: Re: fork() and wait().
> 
> 
> the below is created as an example and hasn't been tested - 
> you will need to fill in some code where I've typed things 
> similar to a_descriptive_function_name_for_what_happens_here();
> 
> the perlipc and perlfork manpages have some good examples :)
> 
> hope it helps
> 
> #!/usr/bin/perl
> # perl fork/waitpid example...
> 
> use strict;
> 
> # program hopefully forks here - fork() returns the process 
> id of the child process to the parent process, and zero to 
> the child process # so $pid = 0 (child process), $pid =  of child> (parent process) after the fork() call, or returns 
> undef if it failed (no child process if it fails!)
> 
> defined(my $pid = fork) or die "Can't fork: $!";
> 
> if ($pid != 0) {
># parent process goes here
> 
>some_things_that_the_parent_does_before_waiting_for_the_child();
> 
># as far as I can tell from perlfunc, wait() waits for ALL 
> child processes to finish. may or may not be preferable to 
> waitpid, which only waits for the specified process unless 
> you pass -1 as the PID, in which case it behaves the same as 
> wait() except that you can pass flags (though the flags 
> aren't discussed at all in perldocs - &WNOHANG is given in an 
> example...)
># i'm using the select() call as a 250ms delay here - 
> sleep() can only sleep for whole numbers of seconds which 
> isn't elegant in my opinion.
> 
>while (waitpid($pid,&WNOHANG) != -1) { print '.'; select 
> undef,undef,undef,0.25 };
> 
>things_that_happen_after_the_child_finishes();
> 
>exit;
> }
> else {
># child process goes here
> 
>something_that_takes_a_long_time();
> 
># exit() only exits the current thread/process.
>exit;
> }
> 
> On Thu, 20 May 2004 09:57:25 -0700, Carter Thompson 
> <[EMAIL PROTECTED]> wrote:
> 
> >
> > Thanks, Tony.
> >
> > I'm using 5.6.1 primarily on Solaris, however the work that 
> I'm doing 
> > should eventually end up in a Perl Module that I will use on Win32 
> > platforms and Unix platforms.  I understand that 
> Win32::Process will 
> > handle this on Win32 platforms but that doesn't much help 
> me with Unix 
> > implementations.  Therefore, I could *really* use a good example of 
> > how to implement wait and waitpid.
> >
> > Thanks again.
> >
> > Carter.
> >
> >
> >> -Original Message-
> >> From: Tony White [mailto:[EMAIL PROTECTED]
> >> Sent: Thursday, May 20, 2004 9:14 AM
> >> To: Carter Thompson; Perl-Win32-Users
> >> Subject: RE: fork() and wait().
> >>
> >>
> >> What version of Perl are you using?  If you're using 5.8.0 
> or above, 
> >> fork() works like the threads pragma does.  Take a look at 
> perlfork 
> >> and/or the threads and threads::shared Pragmas.  Oh yeah, and 
> >> especially perlthrtut and perlothrtut.  All these can be 
> found in the 
> >> ActivePerl User Guide. They're very good and will help you get 
> >> started (although you'll spend a couple of hours reading, maybe).
> >>
> >>
> >> > -Original Message-
> >> > From: [EMAIL PROTECTED]
> >> >
> >> [mailto:[EMAIL PROTECTED]
> >> Behalf Of
> >> > Carter Thompson
> >> > Sent: Wednesday, May 19, 2004 6:14 PM
> >> > To: Perl-Win32-Users
> >> > Subject: fork() and wait().
> >> >
> >> >
> >> >
> >> >
> >> > I checked the archives on how to implement a fork() 
> where I can run 
> >> > a child process (system call) and wait until the child 
> ends while 
> &

Re: fork() and wait().

2004-05-20 Thread Mike Jackson
the below is created as an example and hasn't been tested - you will need to fill in 
some code where I've typed things similar to 
a_descriptive_function_name_for_what_happens_here();
the perlipc and perlfork manpages have some good examples :)
hope it helps
#!/usr/bin/perl
# perl fork/waitpid example...
use strict;
# program hopefully forks here - fork() returns the process id of the child process to 
the parent process, and zero to the child process
# so $pid = 0 (child process), $pid =  (parent process) after the fork() 
call, or returns undef if it failed (no child process if it fails!)
defined(my $pid = fork) or die "Can't fork: $!";
if ($pid != 0) {
  # parent process goes here
  some_things_that_the_parent_does_before_waiting_for_the_child();
  # as far as I can tell from perlfunc, wait() waits for ALL child processes to finish. 
may or may not be preferable to waitpid, which only waits for the specified process unless 
you pass -1 as the PID, in which case it behaves the same as wait() except that you can 
pass flags (though the flags aren't discussed at all in perldocs - &WNOHANG is given 
in an example...)
  # i'm using the select() call as a 250ms delay here - sleep() can only sleep for 
whole numbers of seconds which isn't elegant in my opinion.
  while (waitpid($pid,&WNOHANG) != -1) { print '.'; select undef,undef,undef,0.25 };
  things_that_happen_after_the_child_finishes();
  exit;
}
else {
  # child process goes here
  something_that_takes_a_long_time();
  # exit() only exits the current thread/process.
  exit;
}
On Thu, 20 May 2004 09:57:25 -0700, Carter Thompson <[EMAIL PROTECTED]> wrote:
Thanks, Tony.
I'm using 5.6.1 primarily on Solaris, however the work that I'm
doing should eventually end up in a Perl Module that I will use
on Win32 platforms and Unix platforms.  I understand that
Win32::Process will handle this on Win32 platforms but that doesn't
much help me with Unix implementations.  Therefore, I could *really*
use a good example of how to implement wait and waitpid.
Thanks again.
Carter.

-Original Message-
From: Tony White [mailto:[EMAIL PROTECTED]
Sent: Thursday, May 20, 2004 9:14 AM
To: Carter Thompson; Perl-Win32-Users
Subject: RE: fork() and wait().
What version of Perl are you using?  If you're using 5.8.0 or
above, fork() works like the threads pragma does.  Take a
look at perlfork and/or the threads and threads::shared
Pragmas.  Oh yeah, and especially perlthrtut and perlothrtut.
 All these can be found in the ActivePerl User Guide.
They're very good and will help you get started (although
you'll spend a couple of hours reading, maybe).
> -Original Message-
> From: [EMAIL PROTECTED]
>
[mailto:[EMAIL PROTECTED]
Behalf Of
> Carter Thompson
> Sent: Wednesday, May 19, 2004 6:14 PM
> To: Perl-Win32-Users
> Subject: fork() and wait().
>
>
>
>
> I checked the archives on how to implement a fork() where
> I can run a child process (system call) and wait until the
> child ends while processing other stuff and came up with
> this gem:
>
> > Depending on your exact requirements, you may be able to
> achieve what
> you
> > want by simply starting the application with a system call. Perl
> > will then wait for that system command to run to completion
> before
> > continuing on with the rest of the script.
> > Which is all very well if you simply want the script to start a
> process and
> > then wait for that process to finish before continuing on.
> >
> > But if you want the script to fork a process, then do some
> more stuff
> (while
> > the process is running), and then wait for the forked process to
> complete
> > before it continues, you can do this with a 'wait();' call at the
> point at
> > which you want the parent script to wait.
> >
> > (Check out the 'wait' and 'waitpid' functions in the docs).
> >
> > Cheers,
> > Rob
>
> This is exactly what I want to do, however I'm still not sure
> how to use
> wait()
> or waitpid even after checking the docs.
>
> == psuedo code ==
>
> $child = fork() || die "Can't fork: $!" unless defined $child;
>
> if($child > 0) { #parent process
># print dots until command finishes.
>do {
>print ".";
>waitpid();
>}
> } else {   # child process
> my $ppid = getppid();
> $result = system("command");
> }
>
> == end psuedo code ==
>
> OK, I know that doesn't work but it should give you a
pretty good idea
> of what I'm trying to achieve.
>
> Thanks,
>
> Carter.
>
>
> ___
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: fork() and wait().

2004-05-20 Thread Carter Thompson

Thanks, Tony.

I'm using 5.6.1 primarily on Solaris, however the work that I'm
doing should eventually end up in a Perl Module that I will use
on Win32 platforms and Unix platforms.  I understand that 
Win32::Process will handle this on Win32 platforms but that doesn't
much help me with Unix implementations.  Therefore, I could *really*
use a good example of how to implement wait and waitpid.

Thanks again.

Carter.


> -Original Message-
> From: Tony White [mailto:[EMAIL PROTECTED] 
> Sent: Thursday, May 20, 2004 9:14 AM
> To: Carter Thompson; Perl-Win32-Users
> Subject: RE: fork() and wait().
> 
> 
> What version of Perl are you using?  If you're using 5.8.0 or 
> above, fork() works like the threads pragma does.  Take a 
> look at perlfork and/or the threads and threads::shared 
> Pragmas.  Oh yeah, and especially perlthrtut and perlothrtut. 
>  All these can be found in the ActivePerl User Guide.  
> They're very good and will help you get started (although 
> you'll spend a couple of hours reading, maybe).
> 
> 
> > -Original Message-
> > From: [EMAIL PROTECTED]
> > 
> [mailto:[EMAIL PROTECTED] 
> Behalf Of 
> > Carter Thompson
> > Sent: Wednesday, May 19, 2004 6:14 PM
> > To: Perl-Win32-Users
> > Subject: fork() and wait().
> > 
> > 
> > 
> > 
> > I checked the archives on how to implement a fork() where
> > I can run a child process (system call) and wait until the
> > child ends while processing other stuff and came up with 
> > this gem:
> > 
> > > Depending on your exact requirements, you may be able to
> > achieve what
> > you
> > > want by simply starting the application with a system call. Perl 
> > > will then wait for that system command to run to completion
> > before
> > > continuing on with the rest of the script.
> > > Which is all very well if you simply want the script to start a
> > process and
> > > then wait for that process to finish before continuing on.
> > >
> > > But if you want the script to fork a process, then do some
> > more stuff
> > (while
> > > the process is running), and then wait for the forked process to
> > complete
> > > before it continues, you can do this with a 'wait();' call at the
> > point at
> > > which you want the parent script to wait.
> > > 
> > > (Check out the 'wait' and 'waitpid' functions in the docs).
> > >
> > > Cheers,
> > > Rob
> > 
> > This is exactly what I want to do, however I'm still not sure
> > how to use
> > wait()
> > or waitpid even after checking the docs.
> > 
> > == psuedo code ==
> > 
> > $child = fork() || die "Can't fork: $!" unless defined $child;
> > 
> > if($child > 0) { #parent process
> > # print dots until command finishes.
> > do {
> > print ".";
> > waitpid();
> > }
> > } else {# child process
> > my $ppid = getppid();
> > $result = system("command");
> > }
> > 
> > == end psuedo code ==
> > 
> > OK, I know that doesn't work but it should give you a 
> pretty good idea 
> > of what I'm trying to achieve.
> > 
> > Thanks,
> > 
> > Carter.
> > 
> > 
> > ___
> > Perl-Win32-Users mailing list 
> > [EMAIL PROTECTED]
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> > 
> 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: fork() and wait().

2004-05-20 Thread Tony White
What version of Perl are you using?  If you're using 5.8.0 or above, fork() works like 
the threads pragma does.  Take a look at perlfork and/or the threads and 
threads::shared Pragmas.  Oh yeah, and especially perlthrtut and perlothrtut.  All 
these can be found in the ActivePerl User Guide.  They're very good and will help you 
get started (although you'll spend a couple of hours reading, maybe).


> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of
> Carter Thompson
> Sent: Wednesday, May 19, 2004 6:14 PM
> To: Perl-Win32-Users
> Subject: fork() and wait().
> 
> 
> 
> 
> I checked the archives on how to implement a fork() where 
> I can run a child process (system call) and wait until the
> child ends while processing other stuff and came up with 
> this gem:
> 
> > Depending on your exact requirements, you may be able to 
> achieve what
> you
> > want by simply starting the application with a system call.
> > Perl will then wait for that system command to run to completion
> before
> > continuing on with the rest of the script.
> > Which is all very well if you simply want the script to start a
> process and
> > then wait for that process to finish before continuing on.
> >
> > But if you want the script to fork a process, then do some 
> more stuff
> (while
> > the process is running), and then wait for the forked process to
> complete
> > before it continues, you can do this with a 'wait();' call at the
> point at
> > which you want the parent script to wait.
> > 
> > (Check out the 'wait' and 'waitpid' functions in the docs).
> >
> > Cheers,
> > Rob
> 
> This is exactly what I want to do, however I'm still not sure 
> how to use
> wait()
> or waitpid even after checking the docs.
> 
> == psuedo code ==
> 
> $child = fork() || die "Can't fork: $!" unless defined $child;
> 
> if($child > 0) {   #parent process
>   # print dots until command finishes.
>   do {
>   print ".";
>   waitpid();
>   }
> } else {  # child process
> my $ppid = getppid();
> $result = system("command");
> }
> 
> == end psuedo code ==
> 
> OK, I know that doesn't work but it should give you a pretty good idea
> of what I'm
> trying to achieve.
> 
> Thanks,
> 
> Carter.
> 
> 
> ___
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

___
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs