CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Can Demonstrate simple fork()

 



auximage
New User

Jan 22, 2003, 2:30 AM

Post #1 of 3 (1666 views)
Can Demonstrate simple fork() Can't Post

I've been programming in Perl for a while now, but just recently have had the reason to use fork(). I have gone to quiet a few sites, and poured through the multiple books I have on Perl, but just can't get this into my thick skull. I learn by seeing and reversing code (usually).

Anyway, my request. A simple program that would do basically this:

Start a parent, print [Parent]

fork/start child process print [Child]

the child process would sleep for 10 seconds then assign $i =1;

while the child is sleeping, the parent would print "waiting\n";sleep 1; (so i would get 10 "waiting" messages

once $i is assigned 1, child would print [Child End]

parent would print [Parent End]



I apologize if this comes off as a little trivial, but I have been banging my head, fighting zombies, and trying to understand significances of waitpid for about 3 days now. Anyway, any help would be greatly appreciated as I would be able to work through how fork goes through the motions with a simple program like listed above.


davorg
Thaumaturge

Jan 24, 2003, 8:50 AM

Post #2 of 3 (1659 views)
Re: [auximage] Can Demonstrate simple fork() [In reply to] Can't Post

Something like this perhaps:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use POSIX ':sys_wait_h';

if (fork) {
# parent
print "[Parent]\n";
until (waitpid(-1, &WNOHANG)) {
print "waiting...\n";
sleep 1;
}
print "[Parent End]\n";
} else {
# child
print "[Child]\n";
sleep 10;
my $i = 1;
print "[Child End]\n";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


auximage
New User

Jan 24, 2003, 11:18 AM

Post #3 of 3 (1657 views)
Re: [davorg] Can Demonstrate simple fork() [In reply to] Can't Post

A million + 1 thanks to you.

I see that I am going to have to dig a little deeper to understand signal handling.



Again, I can not thank you enough..!!!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives