Fork

Revision as of 17:32, 6 February 2007 by 209.63.105.137 (talk) (New page: {{task}} In this task, the goal is to spawn a new process which can run simultaneously with, and independently of, the original parent process. ==Perl== Category:Perl '''Interpr...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this task, the goal is to spawn a new process which can run simultaneously with, and independently of, the original parent process.

Task
Fork
You are encouraged to solve this task according to the task description, using any language you may know.


Perl

Interpreter: Perl 5.x

In the child code, you may have to re-open database handles and such. If someone else would like to tweak this code to make it concurently

FORK:
if ($pid = fork()) {
    # parent code
} elsif (defined($pid)) {
    setsid; # tells apache to let go of this process and let it run solo
    # disconnect ourselves from input, output, and errors
    close(STDOUT);
    close(STDIN);
    close(STDERR);    
    # re-open to /dev/null to prevent irrelevant warn messages.
    open(STDOUT, '>/dev/null');
    open(STDIN, '>/dev/null');
    open(STDERR, '>>/home/virtual/logs/err.log');
    
    # child code
    
    exit; # important to exit
} elsif($! =~ /emporar/){
    warn '[' . localtime() . "] Failed to Fork - Will try again in 10 seconds.\n";
    sleep(10);
    goto FORK;
} else {
    warn '[' . localtime() . "] Unable to fork - $!";
    exit(0);
}